![]() |
| | #1 |
| Registered User Join Date: Mar 2010
Posts: 8
| About text file I/O 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]);
}
Last edited by noobiest09; 03-20-2010 at 02:02 AM. Reason: Forgot the other bracket |
| noobiest09 is offline | |
| | #2 |
| and the hat of Destiny Join Date: Aug 2001 Location: The edge of the known universe
Posts: 22,495
| fscanf returns a result - you should check it for success. |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Mar 2010
Posts: 8
| |
| noobiest09 is offline | |
| | #4 |
| and the hat of Destiny 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. |
| Salem is offline | |
| | #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);
}
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 | |
| | #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 | |
| | #7 |
| Registered User Join Date: Mar 2010
Posts: 8
| Thanks! I'll try to do the things you guys suggested. |
| noobiest09 is offline | |
| | #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 | |
| | #9 | |
| Registered User Join Date: Mar 2010
Posts: 8
| Quote:
| |
| noobiest09 is offline | |
| | #10 | |
| Registered User 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' 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |