By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,835 Members | 1,894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,835 IT Pros & Developers. It's quick & easy.

Question about fscanf() behavior

P: n/a
The following program illustrates a question I have about fscanf()
behavior:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
FILE *testfile;
char s1[] = "01234567890123456789";
int c;

/* create a file and write some test data */
testfile = fopen("test.dat", "w+");
if (testfile == NULL) {
printf("Could not open file\n");
return EXIT_FAILURE;
}
fprintf(testfile, "%s", s1);

/* rewind() testfile and fputc() a character ... */
rewind(testfile);
fputc('Z', testfile);

/* ... and read-in s1 with fscanf() */
strcpy(s1, "overwriteMe");
c = fscanf(testfile, "%s", s1);

/* print the results */
printf("%d %s\n", c, s1);

fclose(testfile);

return EXIT_SUCCESS;
}

After rewinding the file and printing a character to it, I would expect
fscanf() to begin reading at position 1, but it returns EOF and leaves the
string s1 unchanged (at least with Borland's compiler on my Win XP
machine.) I've googled on this, but haven't found an answer. Neither
have I found an answer in _C A Reference Manual_, fifth edition. From
what I understand a file position indicater is maintained in the FILE
structure, but I'm not expert enough (yet) to know how to check that.
Thanks for any help.

--Gary

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Gary Baydo wrote:
The following program illustrates a question I have about fscanf()
behavior:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
FILE *testfile;
char s1[] = "01234567890123456789";
int c;

/* create a file and write some test data */
testfile = fopen("test.dat", "w+");
if (testfile == NULL) {
printf("Could not open file\n");
return EXIT_FAILURE;
}
fprintf(testfile, "%s", s1);
Probably not the source of trouble, but something you
should be aware of: An output line is "well-formed" only if
it ends with a newline character. On some systems if you
omit the newline on the last line written, that line may
never actually be recorded in the file.
/* rewind() testfile and fputc() a character ... */
rewind(testfile);
fputc('Z', testfile);

/* ... and read-in s1 with fscanf() */
strcpy(s1, "overwriteMe");
c = fscanf(testfile, "%s", s1);


Here, though, is a real problem. When you "switch
directions" between output and input, you need to give the
I/O machinery a chance to shift gears, as it were. You
can't just do an output operation immediately followed by
input, or input immediately followed by output; you must
call one of the file-positioning functions rewind(), fseek(),
or fsetpos() in between. (When switching from output to
input, an fflush() in between will also work.) C Standard,
section 7.19.5.3, paragraph 6.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #2

P: n/a
On Sun, 06 Nov 2005 17:09:23 -0500, Eric Sosman wrote:
/* rewind() testfile and fputc() a character ... */
rewind(testfile);
fputc('Z', testfile);

/* ... and read-in s1 with fscanf() */
strcpy(s1, "overwriteMe");
c = fscanf(testfile, "%s", s1);


Here, though, is a real problem. When you "switch
directions" between output and input, you need to give the
I/O machinery a chance to shift gears, as it were. You
can't just do an output operation immediately followed by
input, or input immediately followed by output; you must
call one of the file-positioning functions rewind(), fseek(),
or fsetpos() in between. (When switching from output to
input, an fflush() in between will also work.) C Standard,
section 7.19.5.3, paragraph 6.


Thank you. I downloaded the C99 Rationale v5.10 and found your
reference. This requirement is not mentioned in Deitel's _C How to
Program_, fourth edition, in the chapter on "C File Processing." Nor are
a few other things I've learned in the last few minutes. Thanks again.

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.