?
Solved

Progressing to the next line

Posted on 2004-09-18
10
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 

Author Comment

by:bzzoy
ID: 12094366
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
ID: 12094400
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
ID: 12094811
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:bzzoy
ID: 12095529
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 600 total points
ID: 12095560
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
 

Author Comment

by:bzzoy
ID: 12095666
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
ID: 12095796
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
ID: 12095894
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
ID: 12095910
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
ID: 12096016
No prob, glad to help.  :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

762 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