943,910 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 70863
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Feb 14th, 2007
0

Re: read each line of file

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.
  1. char line[80];
  2. char *ptr = 0;
  3. int i = 0;
  4.  
  5. while( fgets(line, sizeof(line), f) != NULL)
  6. {
  7. // clear the array
  8. memset(v,0,sizeof(v));
  9. i = 0;
  10. // extract numbers from line string
  11. ptr = strtok(line," ");
  12. while( ptr != NULL)
  13. {
  14. // insert into array
  15. v[i] = atoi(ptr);
  16. ++i;
  17. // get next number from the string
  18. ptr = strtok(NULL, " ");
  19. }
  20. // now do something with these numbers
  21.  
  22. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 14th, 2007
0

Re: read each line of file

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.
  1. char line[80];
  2. char *ptr = 0;
  3. int i = 0;
  4.  
  5. while( fgets(line, sizeof(line), f) != NULL)
  6. {
  7. // clear the array
  8. memset(v,0,sizeof(v));
  9. i = 0;
  10. // extract numbers from line string
  11. ptr = strtok(line," ");
  12. while( ptr != NULL)
  13. {
  14. // insert into array
  15. v[i] = atoi(ptr);
  16. ++i;
  17. // get next number from the string
  18. ptr = strtok(NULL, " ");
  19. }
  20. // now do something with these numbers
  21.  
  22. }
From a purely style issue, I would not use 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:
  1. do
  2. {
  3. fgets(line, sizeof(line), f) != NULL)
  4. n = 0;
  5. // skip leading whitespace
  6. while (isspace(line[n])) n++;
  7.  
  8. i = 0;
  9. do
  10. {
  11. // extract numbers from line string
  12. v[i] = atoi(&line[n]);
  13.  
  14. // skip to next value
  15. while (!isspace(line[n])) n++; // skip over this value
  16. while ( isspace(line[n]))
  17. {
  18. if (line[n] == '\n') break; // isspace() sees \n as whitespace
  19. n++; // skip over the spaces
  20. }
  21. } while( line[n] != '\n')
I feel I have more control over the input by doing essentially what strtok() does anyway. As I said, purely a personal style issue.

Now, about your code:
  1. #include<conio.h> //// should not use this header, not portable
  2. #include<stdio.h>
  3. main()
  4. {
  5. clrscr(); //// Not portable. Also in Standard C you cannot use
  6. //// an executable command before your declarations
  7. int v[10],v1[10],i=0,j,t,k,number,count=0;
  8. FILE *f ;
  9. f = fopen("numbers.inp", "r"); //// please use whitespace
  10. do
  11. {
  12. i++;
  13. fscanf (f, "%d", &v[i]); //// please use whitespace for readability
  14. count++;
  15. }
  16. while((number=getc(f))!=EOF);
As ADragon said, 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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is online now Online
7,732 posts
since May 2006
Feb 17th, 2007
0

Re: read each line of file

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.
  1. char line[80];
  2. char *ptr = 0;
  3. int i = 0;
  4.  
  5. while( fgets(line, sizeof(line), f) != NULL)
  6. {
  7. // clear the array
  8. memset(v,0,sizeof(v));
  9. i = 0;
  10. // extract numbers from line string
  11. ptr = strtok(line," ");
  12. while( ptr != NULL)
  13. {
  14. // insert into array
  15. v[i] = atoi(ptr);
  16. ++i;
  17. // get next number from the string
  18. ptr = strtok(NULL, " ");
  19. }
  20. // now do something with these numbers
  21.  
  22. }
... but it stop reading at the first line and not jump to second line. Could you help me at this point?
thank you!
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006
Feb 17th, 2007
0

Re: read each line of file

Click to Expand / Collapse  Quote originally posted by donaldunca ...
... but it stop reading at the first line and not jump to second line. Could you help me at this point?
thank you!
Since the only code you posted above is ADragon's code, and his works fine, we have no idea what's wrong with yours.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is online now Online
7,732 posts
since May 2006
Feb 17th, 2007
0

Re: read each line of file

<< mistake >>
Last edited by ~s.o.s~; Feb 17th, 2007 at 12:07 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Feb 17th, 2007
0

Re: read each line of file

Click to Expand / Collapse  Quote originally posted by donaldunca ...
... but it stop reading at the first line and not jump to second line. Could you help me at this point?
thank you!
I've experienced problems with fgets() not stopping at each line before. It turns out that the file I was using was from MacOS 9, which used a totally different encoding for newlines, and that was what screwed up fgets().

So if you didn't create the textfile yourself, consider recreating it just to be sure.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 17th, 2007
0

Re: read each line of file

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 21st, 2007
0

Re: read each line of file

I have just finished my program! Thank you all of you
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: how do u find prime numbers in an array
Next Thread in C Forum Timeline: turboC





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC