delete first line from a text file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 12
Reputation: mannantes is an unknown quantity at this point 
Solved Threads: 0
mannantes mannantes is offline Offline
Newbie Poster

delete first line from a text file

 
0
  #1
Oct 26th, 2007
HI,
plz can you help me i have a text file like this :
  1. <ol style="list-style-type: decimal"><li>telemetre_18_04_2007_11h_08_mn_50sec.dbt</li>
  2. <li>1176887334023317 1 D 3 1.486789 124 -17.150263</li>
  3. <li>1176887334050289 1 D 3 1.506669 125 -16.790839</li>
  4. <li>1176887334077692 1 D 3 1.521077 126 -16.517200</li>
  5. <li>1176887334103341 2 D 3 1.579571 127 -15.251071</li>
  6. <li>1176887334130332 2 D 3 1.557395 128 -15.766200</li>
  7. <li>1176887334157684 0 D 3 1.574303 129 -15.378240
  8.  
  9. </li>
  10. <li>telemetre_18_04_2007_11h_08_mn_50sec.dbt</li>
  11. <li>1176887334023324 1 D 3 1.486789 124 -17.150263</li>
  12. <li>1176887334050214 2 D 3 1.506669 125 -16.790839</li>
  13. <li>1176887334077674 0 D 3 1.521077 126 -16.517200</li>
  14. <li>1176887334103345 0 D 3 1.579571 127 -15.251071</li>
  15. <li>1176887334130345 1 D 3 1.557395 128 -15.766200</li>
  16. <li>1176887334157675 0 D 3 1.574303 129 -15.378240</li>
  17. </ol>

i want delete line number 1 and 8.
and after I want to compare 3 files similar at this file,compared to the 16 first character.
PLZ do you have a solution for me, this is my first code in c++.
THANKS
Last edited by mannantes; Oct 26th, 2007 at 9:00 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,852
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 119
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: delete first line from a text file

 
0
  #2
Oct 26th, 2007
Use getline functions, if it line 1 and 8 do not print them.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: delete first line from a text file

 
0
  #3
Oct 26th, 2007
If you want to delete those lines from the file itself then you have to rewrite it. First open that file for reading, then open another temporary file for writing (name it anything you want except don't give it the same name as the original). After that, code a loop using getline() to read a line from the original file and if it is not one of the lines you want to delete write it back to the temp file. After the file has been completely read close both file, then delete the original file and rename the temp to the original.

After you get the above working then you can do the rest of the assignment.

A word of caution -- until you have the above all coded and working right you should make a duplicate copy of the original file so that you can easily restore it if your program screws up, which it most likely will do.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: mannantes is an unknown quantity at this point 
Solved Threads: 0
mannantes mannantes is offline Offline
Newbie Poster

Re: delete first line from a text file

 
0
  #4
Oct 26th, 2007
OK thx ,
but plz say me how can i open another temporary file can u give me an exemple
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: mannantes is an unknown quantity at this point 
Solved Threads: 0
mannantes mannantes is offline Offline
Newbie Poster

Re: delete first line from a text file

 
0
  #5
Oct 26th, 2007
Originally Posted by ithelp View Post
Use getline functions, if it line 1 and 8 do not print them.
this is only an exemple in my file i have >10000 line i can't do that..
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: mannantes is an unknown quantity at this point 
Solved Threads: 0
mannantes mannantes is offline Offline
Newbie Poster

Re: delete first line from a text file

 
0
  #6
Oct 29th, 2007
no other anwser!!!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: delete first line from a text file

 
0
  #7
Oct 29th, 2007
Do you know how to use file streams in C++? Just create a file, it doesn't matter what you call it, for writing (ofstream), and then create another stream for reading (ifstream). Use the getline function on the input file stream (while( std::getline(in, line) )), where line is a std::string), and keep a running total of which number you're at (int line_num = 0; /* ... */ (inside the loop) line_num++), and if the line number matches the numbers you want to delete don't save the line to the output file. Else save it. If you want to delete the original file do so and perhaps rename the second file to what the first was called. Otherwise... you're done.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: delete first line from a text file

 
0
  #8
Oct 29th, 2007
Originally Posted by mannantes View Post
OK thx ,
but plz say me how can i open another temporary file can u give me an exemple
Ok here is a very very simple example. I didn't compile or test this so it might contain errors, but it should give you an idea of what you have to do.
  1. #include <fstream>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string line;
  9. // open input file
  10. ifstream in("infile.txt");
  11. if( !in.is_open())
  12. {
  13. cout << "Input file failed to open\n";
  14. return 1;
  15. }
  16. // now open temp output file
  17. ofstream out("outfile.txt");
  18. // loop to read/write the file. Note that you need to add code here to check
  19. // if you want to write the line
  20. while( getline(in,line) )
  21. {
  22. out << line << "\n";
  23. }
  24. in.close();
  25. out.close();
  26. // delete the original file
  27. remove("infile.txt");
  28. // rename old to new
  29. rename("outfile.txt","infile.txt");
  30.  
  31. // all done!
  32. return 0;
  33. }
Last edited by Ancient Dragon; Oct 29th, 2007 at 11:06 am. Reason: thanks twomers for the corrections
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: delete first line from a text file

 
1
  #9
Oct 29th, 2007
>> I didn't compile or test this so it might contain errors
Neither using namespace std nor std::s are there! Neither is iostream. Tut tut tut.
Last edited by twomers; Oct 29th, 2007 at 10:22 am.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: mannantes is an unknown quantity at this point 
Solved Threads: 0
mannantes mannantes is offline Offline
Newbie Poster

Re: delete first line from a text file

 
0
  #10
Oct 29th, 2007
Very good.
BIG BIG THanks.
i have an other question.
the last !!
how can i compare the last LINE of my file with others line of my second file to merge them.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC