473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fscanf from txt file is reading the last record twice

after opening the text file the the prog is soposed to read each record into certain variables and then display the variable name to the screen

the problem i am geting is that the last record in the text file is being printed
twice

the txt file is formated as so
4 Jennifer f n
3 Alexandra f n


while (!feof(fp))
{
fscanf(fp, "%d %s %c %c" , &age, &name, &sex, &sn);
printf("the data in name is %s\n",name)
}
Jun 24 '08 #1
3 6379
oler1s
671 Expert 512MB
Two hints. I want to see if you can figure this out for yourself.

1) feof does not do what you think it does. Or put another way. How would you know you have reached the end of something?

2) Are you familiar with return values? Functions can return something. What could returning a value be useful for? Do the C library functions return values?
Jun 24 '08 #2
Visame
7
Expand|Select|Wrap|Line Numbers
  1. while (!feof(fp))
  2. {
  3. fscanf(fp, "%d %s %c %c" , &age, &name, &sex, &sn);
  4. printf("the data in name is %s\n",name)
  5. }
I believe the problem lies in function FEOF.
You can try this:
Expand|Select|Wrap|Line Numbers
  1. if (fscanf(fp, "%d %s %c %c" , &age, &name, &sex, &sn)==4)
  2. printf("the data in name is %s\n",name);
  3.  
or
Expand|Select|Wrap|Line Numbers
  1. while (fscanf(fp, "%d %s %c %c" , &age, &name, &sex, &sn)==4)
  2. {
  3. printf("the data in name is %s\n",name)
  4. }
  5.  
Jul 6 '08 #3
hello here is our solution
change coding of fprintf use %wd format that is use width for each variable
fprintf(fp, "%3d %15s %1c %1c" , &age, &name, &sex, &sn);
and also make same change in fscanf also
while (fscanf(fp, "%3d %15s %1c %1c" , &age, &name, &sex, &sn))
{
printf("the data in name is %s\n",name)
}
and you will get your desire output.
so now Cheersssssssss..........
Nov 24 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Brian Ward | last post by:
First: sorry as a relative newbie for previously not including code. My question: Reading C++ books I almost always find programs such as the one below give the following type of code for reading...
7
by: DJP | last post by:
Hi, I need to read a file programmatically until end of file. My logic is as follows: while(!feof(Fp)) { fgets(readLine,10000,Fp);
12
by: francisds | last post by:
Hi, Can you guys see if there's a solution to this problem? I have a database from which I have to read each record and process that record. New records are being added all the time, so I...
3
by: Mark | last post by:
I'm using ASP.Net to accress a database, what I need to do is get the fields out of the very last record in the db. How do I do this? Actually I'm after the primary key, titled 'AdID' it'll tell...
4
by: Rico | last post by:
Hi All, Just wondering, in vb code, how to if the last record on a cascading form is the current record? Thanks!
1
by: Acolyte | last post by:
Alright, as a continuation of my previous question, I've got two problems with this program-the file read only reads the last line of the input file, and, when I try to assign a tokened word to a...
2
by: Kenneth | last post by:
Ok, so logically this code seems to make sense but for some reason it reads the last record twice before hitting eof. Is ifstream.eof() implementation dependent? void LoadFile(ifstream &File,...
6
by: Lee Sander | last post by:
Dear all, I would like to read a really huge file that looks like this: line_11 line_12 line_13 .... line_21 line_22 ....
1
by: psyvanz | last post by:
here is the code im working with... hope you can understand? cause this thing searches the first one, not the last one record... i wanted ONLY to find the last record in a table... Please...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.