473,466 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question about fscanf() behavior

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
2 1516
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: John Phung | last post by:
Is there a fscanf equivalent for c++? Here's what I'm talking about: unsigned int NextAddress(ifstream& AddressFile) { unsigned int next_address; -->How do I rewrite the fscanf listed below...
3
by: Mantorok Redgormor | last post by:
I have failed to find an authoritive answer on the following: fscanf(NULL, ..); The rest of the function arguments have been omitted. I'm only curious as to what behavior is given when the...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
5
by: gb | last post by:
Hi, I have a small program which opens a file (Whose content is a mac address in the form xx:xx:xx:xx:xx:xx) and uses fscanf() to get individual bytes into an array which is defined as "unsigned...
17
by: vashwath | last post by:
#include <stdio.h> int main() { FILE *fp; char s; fopen("file.txt","w+"); fprintf(fp,"HI\n");
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
9
by: somenath | last post by:
Hi All, I was going through one piece of code which is written by an experience programmer and it is working fine. But I think the use of "strstr" is not proper because it may show undefined...
8
by: Fred | last post by:
I've got following program encapsuled fscanf, however, it doesn't work. I'm sure that the content format in "a.txt" is OK, the content would be correctly read if using fscanf directly, what's...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.