473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Feb 11 '08 #1
3 1518
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?

Feb 11 '08 #2
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?
Feb 11 '08 #3
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(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?
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<charData( 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
ta*******@rocketmail.com
Feb 12 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
21
by: siroregano | last post by:
Hi Everyone- I'm new to this group, and almost-as-new to asking programming questions publicly, so please forgive me if I miss a convention or two! I have a text file, around 40,000 lines...
9
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that...
22
by: petermichaux | last post by:
Hi, I'm curious about server load and download time if I use one big javascript file or break it into several smaller ones. Which is better? (Please think of this as the first time the scripts...
2
by: sani8888 | last post by:
Hi everybody I am a beginner with C++ programming. And I need some help. How can I start with this program *********** The program is using a text file of information as the source of the...
12
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
4
by: saytri | last post by:
Hi guys! I am making a quiz. The questions are stored in a binary file. I made an option in the quiz where a user can add a question to the binary file. Altough this works, the problem is that...
14
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.