| | |
delete first line from a text file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
HI,
plz can you help me i have a text file like this :
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
plz can you help me i have a text file like this :
C++ Syntax (Toggle Plain Text)
<ol style="list-style-type: decimal"><li>telemetre_18_04_2007_11h_08_mn_50sec.dbt</li> <li>1176887334023317 1 D 3 1.486789 124 -17.150263</li> <li>1176887334050289 1 D 3 1.506669 125 -16.790839</li> <li>1176887334077692 1 D 3 1.521077 126 -16.517200</li> <li>1176887334103341 2 D 3 1.579571 127 -15.251071</li> <li>1176887334130332 2 D 3 1.557395 128 -15.766200</li> <li>1176887334157684 0 D 3 1.574303 129 -15.378240 </li> <li>telemetre_18_04_2007_11h_08_mn_50sec.dbt</li> <li>1176887334023324 1 D 3 1.486789 124 -17.150263</li> <li>1176887334050214 2 D 3 1.506669 125 -16.790839</li> <li>1176887334077674 0 D 3 1.521077 126 -16.517200</li> <li>1176887334103345 0 D 3 1.579571 127 -15.251071</li> <li>1176887334130345 1 D 3 1.557395 128 -15.766200</li> <li>1176887334157675 0 D 3 1.574303 129 -15.378240</li> </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.
Use getline functions, if it line 1 and 8 do not print them.
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.
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.
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. •
•
•
•
OK thx ,
but plz say me how can i open another temporary file can u give me an exemple
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <string> #include <iostream> using namespace std; int main() { string line; // open input file ifstream in("infile.txt"); if( !in.is_open()) { cout << "Input file failed to open\n"; return 1; } // now open temp output file ofstream out("outfile.txt"); // loop to read/write the file. Note that you need to add code here to check // if you want to write the line while( getline(in,line) ) { out << line << "\n"; } in.close(); out.close(); // delete the original file remove("infile.txt"); // rename old to new rename("outfile.txt","infile.txt"); // all done! return 0; }
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.
>> I didn't compile or test this so it might contain errors
Neither
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.
![]() |
Similar Threads
- delete a list in text file (Python)
- How to delete a row in text file? (Visual Basic 4 / 5 / 6)
- how do i read the last line of a text file? (Python)
- to delete contents of a text file (Java)
- 10 line text file (Java)
Other Threads in the C++ Forum
- Previous Thread: Error in borland c++
- Next Thread: listview groups
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






