read each line of file

Thread Solved

Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: read each line of file

 
0
  #11
Feb 14th, 2007
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. }
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: read each line of file

 
0
  #12
Feb 14th, 2007
Originally Posted by Ancient Dragon View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

Re: read each line of file

 
0
  #13
Feb 17th, 2007
Originally Posted by Ancient Dragon View Post
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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: read each line of file

 
0
  #14
Feb 17th, 2007
Originally Posted by donaldunca View Post
... 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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: read each line of file

 
0
  #15
Feb 17th, 2007
<< mistake >>
Last edited by ~s.o.s~; Feb 17th, 2007 at 12:07 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: read each line of file

 
0
  #16
Feb 17th, 2007
Originally Posted by donaldunca View Post
... 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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: read each line of file

 
0
  #17
Feb 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

Re: read each line of file

 
0
  #18
Feb 21st, 2007
I have just finished my program! Thank you all of you
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC