File IO Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts

  • Jim Langston
    Guest replied
    Re: File IO Question

    JackC wrote:
    On 11 Feb, 19:25, Rolf Magnus <ramag...@t-online.dewrote:
    >JackC wrote:
    >>Hi,
    >>
    >>Im having some problems working out how I can read the last 5 bytes
    >>of a file which i already have open. Heres what i have tried:
    >>
    >>pFile = fopen ("/home/jc/data.txt" , "rw");
    >>
    >>// Some IO here (preserve position pointer required)
    >>
    >>// Read in last 5 bytes
    >>fseek(pFile , 5, SEEK_SET);
    >>fgets(buffe r, 5, pFile);
    >>
    >>// More IO here (resume old position pointer)
    >>
    >>fclose(pFile) ;
    >>
    >>This doesn't work because SEEK_SET adds 5 onto the end of the file,
    >>
    >No. It adds 5 to the beginning of the file. What you need to use is
    >SEEK_END.
    >>
    >>i need a way to set the position to the current end - 5 bytes,
    >>whilst preserving the current position once im done.
    >>
    >Use an offset of -5.
    >>
    >>Any ideas on how to approach this? I thought about getting the file
    >>size and using fseek with SEEK_SET from the beginning of the file,
    >>but i think this would involve closing/reopening the file which i
    >>would like to avoid.
    >>
    >Why would you need to close the file for that?
    >
    Thanks, sorry that was a typo in my code i did mean SEEK_END, but i
    didn't think i could use a negative offset.
    >
    If i try this:
    fseek(pFile, -5, SEEK_END);
    fgets(buffer, 5, pFile);
    >
    It doesn't read the last 5 characters, but instead I just get some
    random chars back.
    >
    Any ideas?
    I tried this program to see if I could find any problems. It gives me the
    first 4 characters of the file, and the last 4 characters of the file.
    Rudimentary error checking since that wasn't my main concern. It seems to
    work as expected for me. Remember that the fgets wants to save 1 spot for
    the null terminator, so telling it to read 5 will read 4 and add a null
    temrinator.

    #include <cstdio>
    #include <vector>
    #include <iostream>

    int main()
    {
    FILE* Input = std::fopen("C:/test.txt", "r");
    if ( Input == NULL )
    {
    std::cout << "Error occured opening file." << "\n";
    return 0;
    }

    std::vector<cha rData( 10 );

    if ( fgets( &Data[0], 5, Input ) == NULL )
    std::cout << "Error occured reading";
    else
    std::cout << "Data:" << &Data[0] << "\n";

    std::fseek( Input, -5, SEEK_END );

    if ( fgets( &Data[0], 5, Input ) == NULL )
    std::cout << "Error occured reading";
    else
    std::cout << "Data:" << &Data[0] << "\n";

    std::fclose( Input );

    }




    --
    Jim Langston
    tazmaster@rocke tmail.com


    Leave a comment:


  • JackC
    Guest replied
    Re: File IO Question

    On 11 Feb, 19:25, Rolf Magnus <ramag...@t-online.dewrote:
    JackC wrote:
    Hi,
    >
    Im having some problems working out how I can read the last 5 bytes of
    a file which i already have open. Heres what i have tried:
    >
    pFile = fopen ("/home/jc/data.txt" , "rw");
    >
    // Some IO here (preserve position pointer required)
    >
    // Read in last 5 bytes
    fseek(pFile, 5, SEEK_SET);
    fgets(buffer, 5, pFile);
    >
    // More IO here (resume old position pointer)
    >
    fclose(pFile);
    >
    This doesn't work because SEEK_SET adds 5 onto the end of the file,
    >
    No. It adds 5 to the beginning of the file. What you need to use is
    SEEK_END.
    >
    i need a way to set the position to the current end - 5 bytes, whilst
    preserving the current position once im done.
    >
    Use an offset of -5.
    >
    Any ideas on how to approach this? I thought about getting the file
    size and using fseek with SEEK_SET from the beginning of the file, but
    i think this would involve closing/reopening the file which i would
    like to avoid.
    >
    Why would you need to close the file for that?
    Thanks, sorry that was a typo in my code i did mean SEEK_END, but i
    didn't think i could use a negative offset.

    If i try this:
    fseek(pFile, -5, SEEK_END);
    fgets(buffer, 5, pFile);

    It doesn't read the last 5 characters, but instead I just get some
    random chars back.

    Any ideas?

    Leave a comment:


  • Rolf Magnus
    Guest replied
    Re: File IO Question

    JackC wrote:
    Hi,
    >
    Im having some problems working out how I can read the last 5 bytes of
    a file which i already have open. Heres what i have tried:
    >
    pFile = fopen ("/home/jc/data.txt" , "rw");
    >
    // Some IO here (preserve position pointer required)
    >
    // Read in last 5 bytes
    fseek(pFile, 5, SEEK_SET);
    fgets(buffer, 5, pFile);
    >
    // More IO here (resume old position pointer)
    >
    fclose(pFile);
    >
    This doesn't work because SEEK_SET adds 5 onto the end of the file,
    No. It adds 5 to the beginning of the file. What you need to use is
    SEEK_END.
    i need a way to set the position to the current end - 5 bytes, whilst
    preserving the current position once im done.
    Use an offset of -5.
    Any ideas on how to approach this? I thought about getting the file
    size and using fseek with SEEK_SET from the beginning of the file, but
    i think this would involve closing/reopening the file which i would
    like to avoid.
    Why would you need to close the file for that?

    Leave a comment:


  • JackC
    Guest started a topic File IO Question

    File IO Question

    Hi,

    Im having some problems working out how I can read the last 5 bytes of
    a file which i already have open. Heres what i have tried:

    pFile = fopen ("/home/jc/data.txt" , "rw");

    // Some IO here (preserve position pointer required)

    // Read in last 5 bytes
    fseek(pFile, 5, SEEK_SET);
    fgets(buffer, 5, pFile);

    // More IO here (resume old position pointer)

    fclose(pFile);

    This doesn't work because SEEK_SET adds 5 onto the end of the file, i
    need a way to set the position to the current end - 5 bytes, whilst
    preserving the current position once im done.

    Any ideas on how to approach this? I thought about getting the file
    size and using fseek with SEEK_SET from the beginning of the file, but
    i think this would involve closing/reopening the file which i would
    like to avoid.

    Thanks alot for any help,

    Jack
Working...