File IO Question

J

JackC

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
 
R

Rolf Magnus

JackC said:
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?
 
J

JackC

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?

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?
 
J

Jim Langston

JackC said:
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<char> Data( 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 );

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top