Thread: Reading Input from a file, line-by-line

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    21

    Reading Input from a file, line-by-line

    For an assignment in my C course, I need to write a program that reads lines of numbers from a file, sorts them, writes the output to a file then reads the next line and continues, until a -1 is reached in the file.

    My question is: how would I go about doing this? Or, rather, which functions should I use to read the file as such. The pseudocode for what I want to do is:
    Open File
    Read (Line until end of line is reached)
    Convert each number read into int, then put it into a 1-dimensional array
    Do the sorting ('Pancake sorting', if you want to know)
    Write the output to a file.
    Read in the next line and repeat the sequence.

    I'm not exactly sure how to read the file and pick out the numbers from a string, and, moreso, how to make it start reading from a specific line in the file.

    (Note: Not asking for you to write the program for me, I'm just asking for help filling in the metaphorical blanks.)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    fgets() to read each line.
    sscanf() to parse the line.
    Numerous examples of each can be found on the forums.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are many different ways that you can achieve this. Here's some suggestions:

    Read the line using fgets() - you'll need to figure out what the maximum theoretical line length is so that you can fix up a string to read into. [You can detect and print an error message by checking if the last thing in the string is a newline - if it isn't, then the input array is too short.]

    Next scan for "delimiters" (spaces, commas, or whatever it is that separates one number from another). Make sure you spot end of line! Translate the bit between two delimeters as a number.

    Now sort the numbers

    And output them.

    If you write some code, you could get more specific advice when you post the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    21
    How should I go about having it so it picks up where it left off after sorting each line? I was thinking something along the lines of this:

    While (!= EOF)
    fgets (numbers, converts to int, puts it into temparray[n])
    if (end of line)
    sort(temparray [n])
    Arraycount = 0

    So, it goes through the file, puts the first line into the array, sorts the array, then, if it hasn't reached the end of the file, it resets the array count and starts over again on the next line.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yeah, something like that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    21
    Ok, here's what I've got. As per my assignment instructions, it actually just uses I/O redirection. However, I'm having some errors:
    Code:
    #include <stdio.h>
    char input [30];
    int stack [30];
    char c;
    int cnt = 0;
    
    int main ()
    {
            while ((c = getchar()) != '-1')
            {
                    if (c != '\n')
                    {
                            input[cnt++] = c;
                    }
                    if (c == '\n')
                    {
                            puts(input);
                            printf("Flip would be here.\n");
                            cnt = 0;
                    }
            }
    }
    Input.txt is as follows:
    Code:
    1 4 7 5 2 5 
    8 5 3 2 6 4 1 2 9 10 2
    6 9 16 8 4
    -1
    And the output is:
    Code:
    1 4 7 5 2 5 
    Flip would be here.
    8 5 3 2 6 4 1 2 9 10 2
    Flip would be here.
    6 9 16 8 44 1 2 9 10 2
    Flip would be here.
    -19 16 8 44 1 2 9 10 2
    Flip would be here.
    Segmentation fault
    So, the problem here is that it seems to keep reading after it hits -1, and it doesn't clear the string. What am I missing here?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    As characters, -1 is '-' and '1'; 10 is '1' and '0'. If you are trying to read in integers, read in integers. I don't believe there is such a character as '-1'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    21
    So, how would I convert '-' and '1' to an integer? Or, rather should I change it to just
    Code:
    while ((c = getchar()) != '-')
    , since the only reason a '-' would occur would be to denote the '-1'?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I dunno. Maybe something along this line for starters.
    Code:
       int number;
       while ( scanf("%d", &number) == 1 && number != -1 )
       {
          /* ... */
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 13
    Last Post: 04-05-2009, 10:16 AM
  3. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 02:45 AM
  4. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM