Thread: Reading the first line of a text file and storing in an array

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    10

    Reading the first line of a text file and storing in an array

    hello there,
    Im trying to get one of the functions for my code to work which reads in two text files line by line and compares each line and so far i havent been able to do so, i am trying to read the first line of a text file only and store that in a char array, i have only been able to find how to store an entire file or contents of a directory into an array not the first line, i have tried using fgets but with no avail, any sujestions or things to try would be great
    thanks in advance
    Last edited by Ciaran; 04-01-2011 at 01:13 PM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If you open the file in text mode...
    Code:
    #define BUF_SIZE 400
    
    char buffer[BUF_SIZE + 1], *pnt;
    FILE *fh;
    int line;
    
    if (!(fh = fopen(FILE_NAME, "r"))) {
        printf("Failure to open file:  %s\n", FILE_NAME);
        exit(0);
        }
    for (line = 1; ; line++) {
        if (!fgets(buffer, BUF_SIZE, fh)) /* reads one line at a time */
            break;
        if (!(pnt = strchr(buffer, '\n'))) {
            printf("No end-of-line detected in line %d. Too long for buffer.\n", line);
            exit(0);
            }
        *pnt = '\0';
        /* do stuff */
        }
    fclose(fh);
    Last edited by nonoob; 04-01-2011 at 01:24 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fgets(charArrayName, sizeof(charArrayName), filePointerName) is what you want, clearly.

    It pulls in one line of text at a time, including the newline at the end of the line (space permitting), and adds the required end of string char, for you. That allows the whole line of text to be used as one big string, with any of C's string handling functions (to use them, include <string.h>).

    So get started! Make sure your char array is comfortably bigger than the rows of text you are getting.

    @Nonoob:

    You posted a workable snippet of code before the OP posted any code -- you're going straight to hell!
    Last edited by Adak; 04-01-2011 at 01:22 PM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yeah.. I should have stopped at emphasizing open mode not being "binary". But strangely, the OP's attempt to use fgets failed him. So I did not know where else he may have gone wrong. I anticipated most issues such as buffer overrun, end-of-line terminator, failure to open file, error handling, etc. I just saved him and us another 20 posts.

    But you're right. It's more effective learning when one makes one’s own mistakes.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @Nonoob, that was just a joke - don't take it seriously. You did exactly the right thing, of course.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    sfp=fopen("c:\\serverlist.txt", "a+");//points server file pointer to server file list 
    cfp=fopen("c:\\clientlist.txt", "a+");//points client file pointer to client file list 
    cpl-fopen("c:\\copylist.txt", "a+");//points copy list file pointer to copy list
    Here you should open your input files using "r" not "a+" unless you want the file pointer at the end of the file...

    Code:
    //-delete line 1 of each text file 
    //-therefore old second line should become new first line
    Don't try to delete it from your input files. That is not as simple as you may think. Simply ignore the first line when reading it.

    A few other issues...
    You are not checking the return values of your fopen() calls. What are you going to do if one of the files doesn't open?

    You are also not checking the returns from your fgets(). What happens if one hits end of file before the other and returns 0?

    You never want to clear a string buffer to spaces. Either set it to 0s or simply use sll1[0] = 0 ... which is adequate to tell the string library functions it's empty.

    On a more general level... do try to pick more descriptive names for your variables ... sll1 is pretty cryptic, use descriptive names like ServerString or SrvDat ....

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    In response the three files are created by another function in the program and checked there, this just opens them, I tried to just ignore the first line but as the compare moves down it is comparing each line one and if sll1 (server list line 1) doesn't match cll1 (client list line 1) then it gets added to a copy list and has to move on to the next entry in the server list to compare with the copy list, hence why i need to be able to delete a line, i read somewhere that it is easier just to create a new file that excludes the first line or something.
    but im most welcoming of any ideas or suggestions to make it work better, or more simply

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Opening a file is NEVER guaranteed. What happens if there's a problem writing the file and it never gets created... When doing IO work, ALWAYS test for success.

    Ignoring a line from a file is easy... call fgets() to read the first line of each file. Then simply start your loop to compare the rest... It's no big issue, just one extra step.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  2. Storing data from text file into array?
    By flaris in forum C Programming
    Replies: 6
    Last Post: 12-02-2010, 02:19 PM
  3. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  4. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM

Tags for this Thread