JackC wrote:
On 11 Feb, 19:25, Rolf Magnus <ramag...@t-online.dewrote:
>
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?
>JackC wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>No. It adds 5 to the beginning of the file. What you need to use is
>SEEK_END.
>>
>>
>Use an offset of -5.
>>
>>
>Why would you need to close the file for that?
>>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:
>>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);
>>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.
>>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.
>>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?
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: