how to read only integers from a file with c
-
Im trying to read a file, containing Strings n integers.. please help me to only to read the integers and find the total marks file contain 10 students names and marks ,each in a line and students name and marks separated by a comma . eg: John,89 Katharin,78
F.R.Majeed
-
Im trying to read a file, containing Strings n integers.. please help me to only to read the integers and find the total marks file contain 10 students names and marks ,each in a line and students name and marks separated by a comma . eg: John,89 Katharin,78
F.R.Majeed
This sounds like a course assignment... :suss: Think about it logically: x - Find a way to read the file line by line x - What could you do when you obtain a line? x - Is there a way to split the line apart, maybe separate name and mark? x - When you find a way to separate them, it is simply the case of casting the string to an int and visa versa. If you get really stuck here is a nice link of tools you could use to help with the string: http://www.cplusplus.com/reference/string/string/[^]
-
This sounds like a course assignment... :suss: Think about it logically: x - Find a way to read the file line by line x - What could you do when you obtain a line? x - Is there a way to split the line apart, maybe separate name and mark? x - When you find a way to separate them, it is simply the case of casting the string to an int and visa versa. If you get really stuck here is a nice link of tools you could use to help with the string: http://www.cplusplus.com/reference/string/string/[^]
venomation wrote:
it is simply the case of casting the string to an int and visa versa.
You cannot cast a string to an
int
, it must be converted.Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
This sounds like a course assignment... :suss: Think about it logically: x - Find a way to read the file line by line x - What could you do when you obtain a line? x - Is there a way to split the line apart, maybe separate name and mark? x - When you find a way to separate them, it is simply the case of casting the string to an int and visa versa. If you get really stuck here is a nice link of tools you could use to help with the string: http://www.cplusplus.com/reference/string/string/[^]
-
@ venomation :confused: what u mention above could be done through oop language but i want to know how to do it through C (sop)
Doing this with C language programming is not difficult; just a few statements.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
Im trying to read a file, containing Strings n integers.. please help me to only to read the integers and find the total marks file contain 10 students names and marks ,each in a line and students name and marks separated by a comma . eg: John,89 Katharin,78
F.R.Majeed
Have you considered
fscanf()
?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Have you considered
fscanf()
?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
fp=fopen("SDetails.txt","r+");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
fscanf(fp, "%d", &myInt);
printf(" %d ",myInt);
sum=sum+myInt;
}printf("%d",sum);
fclose(fp);what i did was like above ..but it print the same int 3 times ..i cant find the error...please help now i can read only ints from the file..thx
-
fp=fopen("SDetails.txt","r+");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
fscanf(fp, "%d", &myInt);
printf(" %d ",myInt);
sum=sum+myInt;
}printf("%d",sum);
fclose(fp);what i did was like above ..but it print the same int 3 times ..i cant find the error...please help now i can read only ints from the file..thx
What you have will never work as you expect. The call to
fgetc()
is going to advance the file pointer, thenfscanf()
is going to be reading from the middle of some byte. Have you checked the return value fromfscanf()
? If it does not find a number to read from the file, it may be retaining the last read value. Is your file laid out something like: Name1,288,7099,441 Name2,1500 Name3,587,208"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous