Solved

Progressing to the next line

Posted on 2004-09-18
10
194 Views
Last Modified: 2012-05-05
this is a pretty basic file I/O thing that i need help on.  im used to javas buffered reader which seems less complicated than the c method.
basically, i need to know how to get to the next line of a file for reading.  i can read whatever i need from the line (in this case 1 integer), but if there is no integer on the line or it doesnt meet my criteria, how do i skip that line, using the fgetc method of reading in a \n character.  i only have to have the value of the integer from the line, but im not sure how to get to the next line or how to start scanning from that line.  i hope this makes sense, thank you!
0
Comment
Question by:bzzoy
  • 5
  • 4
10 Comments
 

Author Comment

by:bzzoy
Comment Utility
also, if the file contains a line such as
7 8,  how would i get it to only read the 7?
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
You can use fgets() to read a complete line from the file.

Here's an example:

#include <string.h>
#include <stdio.h>
#define MAX 100
//MAX should be larger than the largest line length
int main(void)
{
   FILE *stream;
   char msg[MAX];

   stream = fopen("DUMMY.FIL", "r");

   fgets(msg, sizeof(msg), stream);
   fputs(msg,stdout);
   //printf("%[^\n]", msg);
   fclose(stream);
   return 0;
}

fgets() reads the line into a char[] buffer.
Then,you can use sscanf() to read individual integers from the buffer.
int val1,val2;
sscanf(msg,"%d %d",&val1,&val2);
0
 
LVL 2

Expert Comment

by:sneeuw_chan
Comment Utility
You need to check if fgets gets the complete line, if it's longer than 100 characters, that will fail.
Besides, there is no need to first read lines and then process them, when you can fscanf the stream directly.
What you can do to skip to the next line is to:
    fscanf(stream, "%*[^\n]\n");  /* Read a string of non-newline chars, followed by a newline.  String is discarded because of '*' modifier */
And to read a value, simply:
    fscanf(stream, "%d", &i);

I think I mentioned this in some other question already, but I can't remember which.
0
 

Author Comment

by:bzzoy
Comment Utility
yeah theres like a billion questions on this stuff.  my main issue is will that progress to the next line, and where in a while loop would i have all this stuff
0
 
LVL 2

Accepted Solution

by:
sneeuw_chan earned 150 total points
Comment Utility
The first 'fscanf' line I quoted will read characters from the stream, up to and including the next newline.  In other words, it will progress to the beginning of the next line.  You put it in the sput where you want to progress to the next line, obviously.  You may want to check its return value against EOF, and exit the loop if you get it.
Something like:

 do {
     ...
 } while (fscanf(stream, "%*[^|n]\n") != EOF);

should work, although there may be edge cases I haven't thought of.  You want to check other cases for robustness, or maybe even loop like:
 while(!feof(stream)) {
    ...
    fscanf(stream, "%*[^\n]\n");
 }
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:bzzoy
Comment Utility
if ((theFile = fopen("testFile.txt", "r")) == NULL){
   return EXIT_FAILURE;
}

while (fscanf(theFile, "%*[^n]\n") != EOF) {
   fscanf(theFile, %d, &value);
  printf("%d\n", value);
}
printf("finish loop\n");

  if (fclose(theFile) == EOF) {
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


is this sorta what you had in mind?   testFile.txt contains
64 abs 55
-235bsdf
sadff
9

so basically when i scan this thing i only want to read in 64 and 9.   with what i have above, i only print out one 0 and exit the loop :/
0
 
LVL 2

Expert Comment

by:sneeuw_chan
Comment Utility
Yeah, that's kinda what I had in mind..  But the while needs to be at the end, it needs to be a do-while loop.
Plus, you want to check the return value of the fscanf %d
By the way, I see there's a few typo's, first there's no \ before the n in the while-fscanf, second there's no quotes around the %d in the second fscanf.

Like this:
do {
    if  (fscanf(theFile, "%d", &value) == 1)
        printf("%d\n", value);
} while (fscanf(theFile, "%*[^\n]\n") != EOF);

I tested it, works on my box.  One small thing is that it also reads '-235' as a negative value.  Is that a problem ?
0
 

Author Comment

by:bzzoy
Comment Utility
i actually used your feof method

while(!feof(theFile)){
  fscanf(theFile, "%d", &value);
  printf("%d\n", value);
  if (value >= 0 && value <= 32767){  //if value is in range of 0 - 32767
   printf("correct value\n")
  }
  else {
   fscanf(theFile,"%*[^\n]\n");   //continues to next line if there is no valid value  ie.  a letter or -235 etc.
   }
}

however this gives me an infiinite loop of printing 64, the first value and correct value

before i added in the if statements, it was successfully reading in the first integer values (including the negative)
0
 

Author Comment

by:bzzoy
Comment Utility
well nevermind i got it now, i just had to progress it to the next line under ("correct value").  it looks like its working now, thanks a ton for your help and promptness sneeuw, you deserve an extra 100 points for dealing with my noobishness :)
0
 
LVL 2

Expert Comment

by:sneeuw_chan
Comment Utility
No prob, glad to help.  :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now