|
Help with Input, Output Files in ANSI C
01-23-2011, 04:55 AM
|
Help with Input, Output Files in ANSI C
|
Posts: 680
Name: Lashtal
|
the following program is syntactically correct, as far as I know.
"prog.c"
Code:
#include <stdio.h>
#define KMS_PER_MILE 1.609
int
main(void)
{
double miles, kms;
/* set pointers */
FILE *inp, *outp;
/* open input/output files */
inp = fopen("b:input_file.txt", "r");
outp = fopen("b:output_file.txt", "w");
/* get and echo info */
fscanf(inp, "%1f", miles);
fprintf(outp, "The distance in miles is %.2f.\n", miles);
/* convert distance to kms */
kms = KMS_PER_MILE * miles;
/* display distance in kms */
fprintf(outp, "That equals %.2f kilometers.\n", kms);
/* close files */
fclose(inp);
fclose(outp);
return(0);
}
____________________________________________
I've created a file called, "input_file.txt" (with a value of "112.0") which has been stored to the same file folder as "prog.c"
I have not created "output_file.txt", as that should be created automatically at the time of program execution.
I can only guess that i'm not calling "input_file.txt" correctly at this line:
Code:
/* open input/output files */
inp = fopen("b:input_file.txt", "r");
outp = fopen("b:output_file.txt", "w");
____________________________________________
answer written to "output_file.txt" should be 180.21
__________________
Currently Reading: Please login or register to view this content. Registration is FREE
Last edited by Lashtal; 01-23-2011 at 05:06 AM..
|
|
|
|
01-23-2011, 12:31 PM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 2,883
Name: Matt
Location: Irvine, CA
|
Just a heads up, my C is a little rusty.
What does the b: prefix before the input and output file mean? If you're trying to open the file in binary mode the b goes on the mode parameter.
|
|
|
|
01-23-2011, 07:36 PM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 1,092
Name: Paul W
|
Correct -- so input line should be: inp = fopen("input_file.txt", "rb"); You should always test file pointers (and indeed any pointers) for NULLs before attempting to use them - that would have shown the problem immediately. A few other points -- leave off the b for binary unless you have a reason for using it and have a look at mode "a" for append - you're always truncating the file if it exists on the second fopen.
|
|
|
|
01-23-2011, 10:42 PM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 680
Name: Lashtal
|
First of all, thank you for the advice.
Here was what I was trying to do...
- this program is to be ran only once, via batch mode. (so there will be no recursion/re-running of the program, no user input)
- it reads "input_file.txt", with a predefined value (already instantiated)
- then writes the answer to "output_file.txt"
basically, I was trying to figure out how to use Input and Output files in C
the premise is basically the same in PHP, except without pointers.
from: http://en.wikipedia.org/wiki/C_file_...le_using_fopen
"The C standard provides for two kinds of files— text files and binary files— although operating systems are not required to distinguish between the two."
so, does it matter whether "b:" is included, or not at the beginning of inp = fopen?
__________________
Currently Reading: Please login or register to view this content. Registration is FREE
|
|
|
|
01-23-2011, 11:15 PM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 680
Name: Lashtal
|
Quote:
Originally Posted by NullPointer
What does the b: prefix before the input and output file mean?
|
That's a good question. I don't know.
it was used in the book i'm studying right now, to open, read and write to files. So I assumed it was standard C syntax.
after googling uses for "b:" however, I have found nothing.
__________________
Currently Reading: Please login or register to view this content. Registration is FREE
|
|
|
|
01-23-2011, 11:27 PM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 680
Name: Lashtal
|
Have also tried the following (revised code snippet) without success:
Code:
/* open input/output files */
inp = fopen("input_file.txt", "rb");
outp = fopen("output_file.txt", "w");
am puzzled, so think i'm going to ask around on multiple C forums as well, see what they have to say.
__________________
Currently Reading: Please login or register to view this content. Registration is FREE
|
|
|
|
01-24-2011, 07:16 AM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 1,092
Name: Paul W
|
If you write in binary you have to read binary. If you write in text you have to read text. One of the main reasons for using binary files is that it allows easier positioning of the file pointer (through writing via structures). "b:" is not C, "rb" and "wb" are.
|
|
|
|
01-29-2011, 12:36 AM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 680
Name: Lashtal
|
then, i'm going to write the authors of the book i'm using and ask them why they used "b:" in the way that they did for the example they put forth demonstrating input/output file operations.
I've been googling it, can't find it anywhere.
__________________
Currently Reading: Please login or register to view this content. Registration is FREE
|
|
|
|
01-29-2011, 05:31 AM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 1,092
Name: Paul W
|
Quote:
Originally Posted by Lashtal
then, i'm going to write the authors of the book i'm using and ask them why they used "b:" in the way that they did for the example they put forth demonstrating input/output file operations.
I've been googling it, can't find it anywhere.
|
I've never heard of it, it's not part of ANSI C - are they using some sort of proprietary C precompiler? If so, naughty - it would be non-portable code. The other explanation is that they're just wrong - it does happen - see the crappy C texts by Herbert Schildt, for example.
|
|
|
|
02-04-2011, 09:41 AM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 4,040
Name: Abel Mohler
Location: Asheville, North Carolina USA
|
Quote:
Originally Posted by Lashtal
|
lol.... must be an OLD textbook. I guess C hasn't changed though.
__________________
Software engineer at Please login or register to view this content. Registration is FREE
.
|
|
|
|
02-07-2011, 06:43 PM
|
Re: Help with Input, Output Files in ANSI C
|
Posts: 43,971
Name: Chris Hirst
Location: Blackpool. UK
|
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
|
« Reply to Help with Input, Output Files in ANSI C
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|