| | |
read each line of file
Thread Solved
![]() |
fscanf() does not differentiate between rows. So you will be better off not using that function at all. Instead, I would use fgets() to read one row at a time then parse the string that was read.
c Syntax (Toggle Plain Text)
char line[80]; char *ptr = 0; int i = 0; while( fgets(line, sizeof(line), f) != NULL) { // clear the array memset(v,0,sizeof(v)); i = 0; // extract numbers from line string ptr = strtok(line," "); while( ptr != NULL) { // insert into array v[i] = atoi(ptr); ++i; // get next number from the string ptr = strtok(NULL, " "); } // now do something with these numbers }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
fscanf() does not differentiate between rows. So you will be better off not using that function at all. Instead, I would use fgets() to read one row at a time then parse the string that was read.
c Syntax (Toggle Plain Text)
char line[80]; char *ptr = 0; int i = 0; while( fgets(line, sizeof(line), f) != NULL) { // clear the array memset(v,0,sizeof(v)); i = 0; // extract numbers from line string ptr = strtok(line," "); while( ptr != NULL) { // insert into array v[i] = atoi(ptr); ++i; // get next number from the string ptr = strtok(NULL, " "); } // now do something with these numbers }
memset() nor strtok(). memset() is not necessary because you should be using only the array values you've loaded so you shouldn't have problems with uninitialized variables. As for strtok() I prefer to parse the values by hand with loops. For example: c Syntax (Toggle Plain Text)
do { fgets(line, sizeof(line), f) != NULL) n = 0; // skip leading whitespace while (isspace(line[n])) n++; i = 0; do { // extract numbers from line string v[i] = atoi(&line[n]); // skip to next value while (!isspace(line[n])) n++; // skip over this value while ( isspace(line[n])) { if (line[n] == '\n') break; // isspace() sees \n as whitespace n++; // skip over the spaces } } while( line[n] != '\n')
strtok() does anyway. As I said, purely a personal style issue.Now, about your code:
c Syntax (Toggle Plain Text)
#include<conio.h> //// should not use this header, not portable #include<stdio.h> main() { clrscr(); //// Not portable. Also in Standard C you cannot use //// an executable command before your declarations int v[10],v1[10],i=0,j,t,k,number,count=0; FILE *f ; f = fopen("numbers.inp", "r"); //// please use whitespace do { i++; fscanf (f, "%d", &v[i]); //// please use whitespace for readability count++; } while((number=getc(f))!=EOF);
fscanf() cannot distinguish end of lines. So you just read all the numbers into your 10-value array and corrupted memory. One thing you might want to consider for debugging is using
printf()'s to display pertinent values as your program executes. Then you would have seen immediately that i was getting too large for your array. You still may not know how to fix it, but at least you know what's happening. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
fscanf() does not differentiate between rows. So you will be better off not using that function at all. Instead, I would use fgets() to read one row at a time then parse the string that was read.
c Syntax (Toggle Plain Text)
char line[80]; char *ptr = 0; int i = 0; while( fgets(line, sizeof(line), f) != NULL) { // clear the array memset(v,0,sizeof(v)); i = 0; // extract numbers from line string ptr = strtok(line," "); while( ptr != NULL) { // insert into array v[i] = atoi(ptr); ++i; // get next number from the string ptr = strtok(NULL, " "); } // now do something with these numbers }
thank you!
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
... but it stop reading at the first line and not jump to second line. Could you help me at this point?
thank you!
So if you didn't create the textfile yourself, consider recreating it just to be sure.
"Technological progress is like an axe in the hands of a pathological criminal."
Files transferred to MS-Windows from *nix will also have that problem unless the file is run through a translation program. Most FTP programs can correctly translate the file during file transfer operation.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- read to end of line problem (C)
- Reading specific line in a file. / Searching (C++)
- Read in a string from a text file (C)
- input from file into class (C++)
- How to read end of line? (C++)
Other Threads in the C Forum
- Previous Thread: how do u find prime numbers in an array
- Next Thread: turboC
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency givemetehcodez grade gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






