help with reading from stream

This is a discussion on help with reading from stream within the C Programming forums, part of the General Programming Boards category; Hello fellow C lovers. I have a questions concerning reading from a stream. Im reading filenames out of a file ...

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    help with reading from stream

    Hello fellow C lovers. I have a questions concerning reading from a stream.

    Im reading filenames out of a file like this text file:

    file.txt
    -------------------
    filename1\n
    filename2\n
    etc.
    -------------------

    Im using this loop for now:

    Code:
    char filename[100];
    int c, i = 0;
    FILE *fp;
    
    while ( (c = fgetc(fp)) != EOF) {
              if (c != '\n')
                  filename[i++] = c;
              else
                   break;
    
              filename[i] = '\0';
    
              /* here I open another file and use another while() loop
                  reading chars with fgetc and fputc() those chars into
                  a different fp                                                          */
    }

    My question is: It seems (going through gdb) that while in this loop
    if I open another fp and do something and then come back to
    the top of the loop to continue getting characters from the other
    fp, the fp points back to the beginning of the stream and not to where
    I left off in that stream. Is this normal, and is there a way to prevent
    this?

    Thanks
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First of all, I hope you're initializing fp before you get to the loop. I'm assuming you do and you just took it out to shorten the code.

    If you're using the same variable fp to point to a different file before you're done with the first, that's wrong. You need to have a second FILE * to point to reference the second file.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If I understand your question, look at fseek() and ftell()...
    Also your going to get overflow if you read more than 100 characters (sizeof(filename)).

    Also
    Code:
    filename[i] = '\0';
    Will never get reached.

    Why not just use fgets(), instead of brewing your own?

    Consider:
    Code:
    /* ... */
    
    FILE * fp;
    char buff[100];
    
    if((fp = fopen("file.txt", "rt")) == NULL)
    {
        perror("Failed to open file.txt");
        exit(0);
    }
    
    while(fgets(buff, sizeof(buff), fp) != NULL)
    {
        printf("Filename: %s\n", buff);
    }
    
    fclose(fp);
    /* ... */
    Last edited by zacs7; 05-31-2007 at 04:12 AM.

  4. #4
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    It's kinda difficult to explain.

    1. I'm in a while() loop using fgetc() to read from one stream the name
    of the filename my program will open. Say from this file:

    ----
    filename1\n
    filename2\n
    EOF
    ----

    I read 'filename1' into a char array and use fopen() to open that file
    in my current directory.

    I've also got a third stream open and am reading from filename1 stream
    and writing to this 3rd stream (all within this while() loop)

    (I'm writing from each stream I open with the filenames in the text file
    to the 3rd stream (file) making one big joined file.)

    Now I finish reading from filename1 and fclose() it and go back to the TOP
    of my while loop() to read the next filename out of it to find the next file to
    add to the 3rd stream.

    When I go back to the TOP I am still getting the first filename (filename1)

    Shouldn't this stream be at beginning of line 2?

    Ahh!! maybe I'm doin this all wrong


    P.S zacs7 thanks I forgot that fgets() reads up to and including newline. So i can use fgets() to read
    a line at a time out of this file.
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, look at using a linked list of streams or an array. Just make sure you don't use the same file pointer for different files without closing it first

  6. #6
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Oh no, I know I can't use the same fp.

    Thanks

    I think I'll use fgets()
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    There is nothing in the code you posted to indicate why this might be happening. The next read of fp should continue from where the last one stopped, not the beginning.

    You say you're using different FILE * variables for each fp (sharing the same variable for all three would be the most obvious cause of this problem), so I'm not sure why it's happening. You need to post the entirety of the while loop.

  8. #8
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Thanks brewbuck. I'll review the loop a little more and see what's going on, and then
    I'll post the code if I'm still stuck. But yes, I'm using 3 different file pointers. Maybe I'm
    just overlooking something. I'll see what I can do.
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New stream flushing FAQ candidate?
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 07-01-2009, 05:57 PM
  2. Stream Server
    By ch4 in forum Networking/Device Communication
    Replies: 18
    Last Post: 06-29-2009, 03:09 PM
  3. Cannot close file stream
    By g4j31a5 in forum C++ Programming
    Replies: 8
    Last Post: 01-02-2007, 12:52 AM
  4. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  5. reterning a reference to the input stream
    By indigo0086 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2006, 10:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21