C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-20-2010, 01:59 AM   #1
Registered User
 
Join Date: Mar 2010
Posts: 8
About text file I/O

Hi! I'm relatively new at programming so I'm having a bit of a problem.

My first question is, how do I print out long strings with spaces from a text file? I tried printing it out the normal way but only the first word gets printed out. My professor required us to print out bible passages from a text file.

For my second question, I have this code:
Code:
fp=fopen("Score.txt", "r");
for(i=0;i<30;i++){
fscanf(fp,"%s\n%s\n\n",scorename[i],scorescore[i]);
printf("%s\n%s\n\n",scorename[i],scorescore[i]);
}
It's supposed to print out the scores stored in Score.txt. But say, I have only one score recorded. It prints out 29 junk data for the rest. I'm planning on replacing 30 with n so that it will print only the correct number of scores. But my problem is, how can I keep track of the number of scores inside the text file? I need to retain this even when restarting the program.

Last edited by noobiest09; 03-20-2010 at 02:02 AM. Reason: Forgot the other bracket
noobiest09 is offline   Reply With Quote
Old 03-20-2010, 02:20 AM   #2
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
fscanf returns a result - you should check it for success.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 03-20-2010, 02:22 AM   #3
Registered User
 
Join Date: Mar 2010
Posts: 8
Quote:
Originally Posted by Salem View Post
fscanf returns a result - you should check it for success.
I'm sorry for being such a noob but how do I do that?
noobiest09 is offline   Reply With Quote
Old 03-20-2010, 02:56 AM   #4
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
You read the manual page for fscanf

You then do
SomeKindOfTypeName result = fscanf.....

You then do
if ( result == SomethingYouReadInTheManual )

Presumably, you read something to figure out what %d etc means. Just keep reading the same thing.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 03-20-2010, 03:04 AM   #5
Registered User
 
Join Date: Feb 2010
Posts: 26
If you want to get the number of scores in a file try the followings.
Code:
#include<stdio.h>
int main()
{
        FILE *fp=popen("wc -l score.txt","r");
        int n;
        fscanf(fp,"%d",&n);
        printf("%d\n",n);
}
wc-l will return the number of lines in a file.
popen will return the file pointer which is having the output of wc- l score.txt.
Then get the number of lines using that file pointer.
So finally 'n' will have the number of lines in a file.
Then use that 'n' value in your for loop.

Last edited by kiruthika; 03-20-2010 at 03:08 AM.
kiruthika is offline   Reply With Quote
Old 03-20-2010, 03:08 AM   #6
Registered User
 
Join Date: Nov 2008
Location: INDIA
Posts: 60
Answer for your first question :
If you want to read a line from the file you have to use the function fgets.It will read the entire line . I thing you used fscanf there .It will read only the first word.

Answer for the second question :
As like salem said you have to check the return value of fscanf.If it returns EOF , you just stop reading it and come out from the loop.

Code:
#include <stdio.h>
main()
{
FILE *fp=fopen("Score.txt", "r");
char scorename[20];
char scorescore[3];
int i;
for(i=0;i<30;i++){
if(fscanf(fp,"%s\n%s\n\n",scorename,scorescore)==EOF)
        break;
printf("%s\n%s\n\n",scorename,scorescore);
}
}
karthigayan is offline   Reply With Quote
Old 03-20-2010, 03:13 AM   #7
Registered User
 
Join Date: Mar 2010
Posts: 8
Thanks! I'll try to do the things you guys suggested.
noobiest09 is offline   Reply With Quote
Old 03-20-2010, 03:16 AM   #8
Registered User
 
Join Date: Feb 2010
Posts: 26
Answer for your first question.

If you used fscanf() to read content from the file, then use like the following.
Code:
        fscanf(fp,"%[^\n]",s);
kiruthika is offline   Reply With Quote
Old 03-20-2010, 03:21 AM   #9
Registered User
 
Join Date: Mar 2010
Posts: 8
Quote:
Originally Posted by karthigayan View Post
Answer for your first question :
If you want to read a line from the file you have to use the function fgets.It will read the entire line . I thing you used fscanf there .It will read only the first word.

Answer for the second question :
As like salem said you have to check the return value of fscanf.If it returns EOF , you just stop reading it and come out from the loop.

Code:
#include <stdio.h>
main()
{
FILE *fp=fopen("Score.txt", "r");
char scorename[20];
char scorescore[3];
int i;
for(i=0;i<30;i++){
if(fscanf(fp,"%s\n%s\n\n",scorename,scorescore)==EOF)
        break;
printf("%s\n%s\n\n",scorename,scorescore);
}
}
I tried this and it worked. Thanks a lot!
noobiest09 is offline   Reply With Quote
Old 03-20-2010, 04:54 AM   #10
Registered User
 
UltraKing227's Avatar
 
Join Date: Jan 2010
Location: USA, New york
Posts: 123
just a final note on fscanf:

fscanf, reads a word and then another one.
so if you have a file like:

Quote:
Message From File
EOF
and you use fscanf two times, you get first
'Message' then 'From'. but if you have four
'fscanf's then you get all the words. so fscanf
reads a word and proceeds to the next one,
when encountered the end of the line, it goes
to the next one and gets the word of that line.
hope it helped.
__________________
Macselent Corp
UltraKing227 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting the contents of a text file dagorsul C++ Programming 2 04-29-2008 12:36 PM
C++ std routines siavoshkc C++ Programming 33 07-28-2006 12:13 AM
struct question caduardo21 Windows Programming 5 01-31-2005 04:49 PM
Unknown Memory Leak in Init() Function CodeHacker Windows Programming 3 07-09-2004 09:54 AM
simulate Grep command in Unix using C laxmi C Programming 6 05-10-2002 04:10 PM


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22