Thread: Flushing a line in an array...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    13

    Flushing a line in an array...

    I have a quick question...I'm sorting an array with only numbers in it, two per line. I wanna flush the first string of non-numbers and the other value in that line, if there is one of course, whether it was the first or last, and continue on with the sort. I started a while loop...
    Code:
    char flush (int count, FILE* fp)
    {
        char c;
        printf("Line %d is invalid, skipping line.\n", count);
        while (((c = fgetc(fp)) != EOF && (c != '\n')));
        return c;
    }
    I'm wondering if, at the end I should I make c != '\n' and my count can't equal 2? Would that do it? Because if my count is 0, that should flush the line, or if it's 1, that should flush the line, and if its not already two, it gets sent to this flush function.

    Am I trying to add too much to one loop?

    Thanks, gator

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, so do you know the format of your data in the file? I am afraid I am not following exactly what you are trying to accomplish here.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    c needs to be an int if you are trying to read EOF. Why don't you just use fgets+sscanf, and if your sscanf doesn't return 2, to just throw the line away (ignore it, and call fgets again).


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    Yes sir. I'm reading a text file and placing it into an array. I need to read two consecutive numbers and count those as a line. The first value read that is not a number invalidates that line and I have to toss it, but carry on with the scan. Since it's two per line, if the second value is not a number, I have to toss that line as well, including the number before it. Basically I have to toss the line if either of the two aren't both numbers, print an error, and contine reading the file. Believe it or not, it actually helps a little writing this down. I so wanna add more logic to the end of my flush code, but I don't know if it'll help if the second value in the line of two is not a number.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    I'm throwing it away if it's a character, but not if it's the only value left at EOF. Does that sound right? I mean, I don't wanna lose the last value in a file if the file has an odd number index or the EOF is less than the max size.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ever heard of scanf() and sscanf()?

    (Google is your friend)

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    Quote Originally Posted by CommonTater View Post
    Ever heard of scanf() and sscanf()?

    (Google is your friend)
    Of course...its a flush function from a scanf!!! It's during the scanf!!! What is it looking for during the scanf???

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quzah hit the nail on the head with this one. Take a closer look at what he is telling you to do:

    Quote Originally Posted by quzah View Post
    c needs to be an int if you are trying to read EOF. Why don't you just use fgets+sscanf, and if your sscanf doesn't return 2, to just throw the line away (ignore it, and call fgets again).

    Quzah.
    After considering this, take a look here. Play around with this and then post if you have any further questions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Cgmgator View Post
    Of course...its a flush function from a scanf!!! It's during the scanf!!! What is it looking for during the scanf???
    Say for example you're looking for two integers...
    Code:
    if (fscanf(File,"%d %d",&a,&b) == 2)
      ++count;
    If you check the return value of fscanf() you will get the number of successful conversions... anything but 2 would tell you not to increment your array pointers and carry on to the next line.

    A variant of scanf(), sscanf() can be used with fgets() to break out a line at a time and then convert from the line...
    Code:
    do
      { fgets(buff, buffsize,file);
         if (sscanf(buff,"%d %d",&a[count],&b[count]) == 2)
           count++
       } while (! feof(file));
    Of course you will need to flesh this out with error checking and bounds checking, but the general concept is there.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    Awesome, I'll try it out! Thanks a lot!

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You could also alternatively check the return of fgets so you wouldn't have to use feof:
    Code:
    while(fgets(buff,buffsize,file) != NULL){
         ........
    }


    Some reading for the OP:
    feof FAQ
    What is EOF?
    Last edited by AndrewHunter; 06-30-2011 at 10:47 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    Awesome, I'll try it out! Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Flushing buffer
    By $l4xklynx in forum C Programming
    Replies: 6
    Last Post: 03-04-2009, 10:07 PM
  2. flushing stdin
    By crash88 in forum Linux Programming
    Replies: 1
    Last Post: 05-18-2006, 01:21 AM
  3. flushing ....
    By twomers in forum C++ Programming
    Replies: 10
    Last Post: 02-09-2006, 12:29 PM
  4. Flushing a stringstream
    By Eibro_Guest in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2003, 03:21 PM
  5. Flushing Buffer?
    By Dangerous Dave in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 09:01 AM