• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Progressing to the next line

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
bzzoy
Asked:
bzzoy
  • 5
  • 4
1 Solution
 
bzzoyAuthor Commented:
also, if the file contains a line such as
7 8,  how would i get it to only read the 7?
0
 
ankuratvbCommented:
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
 
sneeuw_chanCommented:
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
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
bzzoyAuthor Commented:
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
 
sneeuw_chanCommented:
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
 
bzzoyAuthor Commented:
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
 
sneeuw_chanCommented:
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
 
bzzoyAuthor Commented:
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
 
bzzoyAuthor Commented:
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
 
sneeuw_chanCommented:
No prob, glad to help.  :-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now