file I/O question

J

John Smith

I want to open text files for input and output. The input file is
read and some processing is done to its contents. The processed
text is then written to the output file. The output file position
is reset, it is read from the top and some statistics are compiled.

The output file must initially be empty for each run of the
program. But, if I open the output file in "r+" mode to allow
both read and update, the contents of the file from the previous
run are preserved -- not what I want. I ended up with this code:

/* create the output file or
delete its contents if it exists */
if((outfile = fopen("fatext.txt", "w"))==NULL)
{
fprintf(stderr,"can't open fatext.txt in ""w"" mode\n");
exit(EXIT_FAILURE);
}
fclose(outfile);
if((infile = fopen(argv[1], "r"))==NULL)
{
fprintf(stderr,"can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
/* reopen output file for read and update */
if((outfile = fopen("fatext.txt", "r+"))==NULL)
{
fprintf(stderr,"can't open fatext.txt in ""r+"" mode\n");
exit(EXIT_FAILURE);
}

This works, but it looks pretty ugly to me. Is there a better way?

JS
 
F

Flash Gordon

John said:
I want to open text files for input and output. The input file is read
and some processing is done to its contents. The processed text is then
written to the output file. The output file position is reset, it is
read from the top and some statistics are compiled.

How about the "w+" mode? That seems to be exactly what you want.
The output file must initially be empty for each run of the program.
But, if I open the output file in "r+" mode to allow both read and
update, the contents of the file from the previous run are preserved --
not what I want. I ended up with this code:

/* create the output file or
delete its contents if it exists */
if((outfile = fopen("fatext.txt", "w"))==NULL)
{
fprintf(stderr,"can't open fatext.txt in ""w"" mode\n");
exit(EXIT_FAILURE);
}
fclose(outfile);

So remove all the above
if((infile = fopen(argv[1], "r"))==NULL)
{
fprintf(stderr,"can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
/* reopen output file for read and update */
if((outfile = fopen("fatext.txt", "r+"))==NULL)

Change the above line to
if((outfile = fopen("fatext.txt", "w+"))==NULL)
{
fprintf(stderr,"can't open fatext.txt in ""r+"" mode\n");

Obviously change the above error as well ;-)
exit(EXIT_FAILURE);
}

This works, but it looks pretty ugly to me. Is there a better way?

Is that an improvement?
 
J

John Smith

Flash said:
John said:
I want to open text files for input and output. The input file is read
and some processing is done to its contents. The processed text is
then written to the output file. The output file position is reset, it
is read from the top and some statistics are compiled.


How about the "w+" mode? That seems to be exactly what you want.
The output file must initially be empty for each run of the program.
But, if I open the output file in "r+" mode to allow both read and
update, the contents of the file from the previous run are preserved
-- not what I want. I ended up with this code:

/* create the output file or
delete its contents if it exists */
if((outfile = fopen("fatext.txt", "w"))==NULL)
{
fprintf(stderr,"can't open fatext.txt in ""w"" mode\n");
exit(EXIT_FAILURE);
}
fclose(outfile);


So remove all the above
if((infile = fopen(argv[1], "r"))==NULL)
{
fprintf(stderr,"can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
/* reopen output file for read and update */
if((outfile = fopen("fatext.txt", "r+"))==NULL)


Change the above line to
if((outfile = fopen("fatext.txt", "w+"))==NULL)
{
fprintf(stderr,"can't open fatext.txt in ""r+"" mode\n");


Obviously change the above error as well ;-)
exit(EXIT_FAILURE);
}

This works, but it looks pretty ugly to me. Is there a better way?


Is that an improvement?

Yes, thanks. Can't imagine how I missed it.

JS
 
M

Michael Wojcik

fprintf(stderr,"can't open fatext.txt in ""w"" mode\n");

An aside: the doubled quotation characters in the second parameter
above don't have any effect. They create three string literals
during translation phase 3:

"can't open fatext.txt in "
"w"
" mode\n"

which are then spliced into a single string literal in phase 6:

"can't open fatext.txt in w mode\n"

(Actually, in phase 5, the \n has been replaced with the appropriate
member(s) of the execution character set, but we can ignore that here.)

If you want a literal double-quote character in a string literal, you
need the \" escape sequence:

"can't open fatext in \"w\" mode\n"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top