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)
}
3 6410 oler1s 671
Recognized Expert Contributor
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?
-
while (!feof(fp))
-
{
-
fscanf(fp, "%d %s %c %c" , &age, &name, &sex, &sn);
-
printf("the data in name is %s\n",name)
-
}
I believe the problem lies in function FEOF.
You can try this: -
if (fscanf(fp, "%d %s %c %c" , &age, &name, &sex, &sn)==4)
-
printf("the data in name is %s\n",name);
-
or -
while (fscanf(fp, "%d %s %c %c" , &age, &name, &sex, &sn)==4)
-
{
-
printf("the data in name is %s\n",name)
-
}
-
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. .........
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 a file :
//********* BUM CODE *************************************
infile.open ("test.dat" , ios::in);
while (!infile.eof())
{
|
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);
|
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 need to go
back and check for new records and process them.
However:
|
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 me what number
this last record is. so I can tell what number the next record inserted will
be.
How do I do this?
Thanks,
|
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!
| |
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 spot in the array, it returns a segmentation fault. Anybody know what I'm missing?
#include <stdio.h>
#include <string.h>
int main (void)
{
FILE *file;
file = fopen("input2.txt", "r");
|
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, List &List)
{
User lUser;
while(!File.eof())
{
File >lUser;
|
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
....
|
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 help me what was wrong with my code..
================================================================================
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |