LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2005, 01:49 AM   #1
Hady
Member
 
Registered: Nov 2003
Posts: 55

Rep: Reputation: 15
Manipulation of text files in C++


Hi!

Could anyone tell me how I can add one line to the beginning of a text file using C++?
I know how to open/close/read from a text file, I just want adding one line without affecting any other content in the file..

Thank you for your time.
 
Old 05-30-2005, 05:24 AM   #2
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
I don't know if it can be done in other way, but one possibility is:

1.- open file.txt for reading and file.txt.bak for writing
2.- write a "\n" to file.txt.bak
3.- copy the content
4.- close the files
5.- remove file.txgt
6.- rename file.txt.bak to file.txt

step 5 shouldn't be very difficult. you only have to read from one file (which you know how to do) and write to the other, while the first file still has something inside.
 
Old 05-30-2005, 04:03 PM   #3
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
I think he meant `one line', not `one newline', but that's a minor issue.

you could also do something along the lines of (pseudo-code):
Code:
stringbuffer b;
b.append("myline\n")
for char in filestream('file.txt'): b.append(char);
f = filestream('file.txt', 'w');
for char in b: f.write(char);
f.close();
 
Old 05-31-2005, 03:00 AM   #4
Hady
Member
 
Registered: Nov 2003
Posts: 55

Original Poster
Rep: Reputation: 15
OK!
Thank you guys for your help!
 
Old 05-31-2005, 08:51 AM   #5
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Your pseudo-code looks a lot like python ;-)
 
Old 05-31-2005, 09:24 AM   #6
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
Your pseudo-code looks a lot like python ;-)
That's because python is runnable pseudo-code ;-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert DOS text files to UNIX text files ta0kira Linux - Software 7 03-15-2011 12:42 PM
Convert text files to html files lothario Linux - Software 3 09-27-2005 09:48 PM
Add text to many files Kanon Linux - General 4 08-28-2005 04:27 AM
More text manipulation ice_hockey Linux - General 2 05-28-2005 02:43 AM
CLI based Text manipulation selfsck Linux - General 1 07-15-2004 08:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration