The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How to erase the last byte in a text file?
Discuss How to erase the last byte in a text file? in the C Programming forum on Dev Shed. How to erase the last byte in a text file? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 10th, 2004, 06:02 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
How to erase the last byte in a text file?
Hello,
I have a c++ program that needs to output data to a text file on command. This I can do, however I end up with an extra line in my file consisting of just a newline character, which I need to delete.
I know how to use seekp() to put the file pointer at this character, how can I then erase it? Effectively write backwards to the file?
|

February 10th, 2004, 06:17 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Colorful Colorado
Posts: 743
Time spent in forums: 21 h 16 m 10 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
|
|
|
the solution would be to fix your original program such that it does not print the extra new line.
|

February 10th, 2004, 07:04 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
You are programming on windows?
Set the file mode to "binary" to avoid the trailing newline.
I am not sure if windows has this function too, but I have found this in my man pages:
Code:
#include <unistd.h>
int
truncate(const char *path, off_t length);
int
ftruncate(int fd, off_t length);
hth,
M.
|

February 10th, 2004, 07:17 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I threw your library header in but it looks like Windows doesn't have that one.
I also don't think that binary mode would be an option either.
Thanks for the replies though!
|

February 10th, 2004, 07:33 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
|
Win32 api function SetEndOfFile() will truncate a file to the current file pointer position. See MSDN for description and parameters.
But as someone else said, you will be better off fixing your program.
|

February 10th, 2004, 07:33 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
Hmmm. Did your compiler come with a help file, specifically the "Platform SDK" Documentation? If not, you should definitively get it!
It's not ftruncate() but SetEndOfFile() - I found it here:
http://msdn.microsoft.com/library/d...etendoffile.asp
hth,
M.
[edit]
Hey Ancient Dragon, you beat me by a few seconds 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|