HI,
plz can you help me i have a text file like this :

[LIST=1]
[*]telemetre_18_04_2007_11h_08_mn_50sec.dbt
[*]1176887334023317	1	D	3	1.486789	124	-17.150263	
[*]1176887334050289	1	D	3	1.506669	125	-16.790839	
[*]1176887334077692	1	D	3	1.521077	126	-16.517200	
[*]1176887334103341	2	D	3	1.579571	127	-15.251071	
[*]1176887334130332	2	D	3	1.557395	128	-15.766200	
[*]1176887334157684	0	D	3	1.574303	129	-15.378240	



[*]telemetre_18_04_2007_11h_08_mn_50sec.dbt
[*]1176887334023324	1	D	3	1.486789	124	-17.150263	
[*]1176887334050214	2	D	3	1.506669	125	-16.790839	
[*]1176887334077674	0	D	3	1.521077	126	-16.517200	
[*]1176887334103345	0	D	3	1.579571	127	-15.251071	
[*]1176887334130345	1	D	3	1.557395	128	-15.766200	
[*]1176887334157675	0	D	3	1.574303	129	-15.378240
[/LIST]

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

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.

OK thx ,
but plz say me how can i open another temporary file can u give me an exemple

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..

no other anwser!!!

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

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.

#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;
}

>> 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.

commented: thanks for pointing out those errors +20

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.

how can i compare the last LINE of my file with others line of my second file to merge them.

I'm not going to give you free code for this one -- it'll cost you lots and lots of $$$$. But you can get free help here if you try to do it yourself and post your attempt.

Hi,
How can i do to read lines 8 to End of my file
this code is only for line 1 to 7 :

[LIST=1]
[*]ifstream input("c:\\test2.txt");
[*]	ofstream out("c:\\fichier3.txt");
[*]   
[*]      string str;
[*]      vector<string> arrayL;
[*]      while ( getline(input, str))
[*]      {
[*]          arrayL.push_back(str);
[*]	  }		
[*]	  for(int i = 0; i <arrayL.size() ; i++)
[*]	{
[*]		while(arrayL[i].length()==0);
[*]		cout << arrayL[i]<< endl;
[*]		out  <<arrayL[i]<< "\n";
[*]	}  
[*]		cin.get();
[*]		return 0;
[/LIST]

but for the last line I didn't find a solution to read them
can u help me plz.

What are you tring to do at line 11? With that semicolon at the end that line does nothing.

>>How can i do to read lines 8 to End of my file this code is only for line 1 to 7 :

Looks like it reads the whole file to me. If you want to skip the first 7 lines then just don't add them to the array -- its not possible to just skip them.

What are you tring to do at line 11? With that semicolon at the end that line does nothing.

at this line if line='\n' do nothing only read lines 1 to 7.
and now i want read line 8 to 14 because i want compare line 14 with other lines like this of my second file txt
to merge them

>>at this line if line='\n' do nothing only read lines 1 to 7
Look at your code again because that is not what it says. The loop from lines 9 thru 14 do not read anything from the file because the previous loop has read it all. All the loop in lines 9-14 is doing is iterating through an array that's already in memory. So you need to just delete line 11 because it can cause an infinit loop that will crash your program.

If you just want to write lines 8-end-of-file to the output file then just start the loop that begins on line 8 with 8 instead of 0

for(int i = 8; i <arrayL.size() ; i++)

If you just want to write lines 8-end-of-file to the output file then just start the loop that begins on line 8 with 8 instead of 0

for(int i = 8; i <arrayL.size() ; i++)

for 8 Number of line it's only an example.
look at my exampleFile.txt .
I can't put i=8 because I have several lines in my files and I can't know each time the number of the line which follows '\n'.can u help me plz to find a solution.

once you read a line from the file, do not copy it if it starts with "telemetre_". eg.

const char remove_this[] = "telemetre-" ;
string line ;
while( getline(infile,line) )
{
  if( line.compare( 0, sizeof(remove_this)-1, remove_this ) != 0 ) 
     outfile << line << '\n' ;
}

to compare the first 16 characters`of two strings

if( string_one.compare( 0, 16, string_two ) == 0 ) cout << "first 16 chars match\n" ;

I think I'm still confused about what you want. According to your original post you have three files that contain portions of the one large file (exampleFile.txt). Each section of exampleFile.txt is separated by a line that begings with the text telemetre_.

What are the file names ? Are the names the same as the first section line ? such as telemetre_18_04_2007_11h_23_mn_15sec.dbt ? And you want to compare all the lines in that file with the lines that follow in exampleFile.txt ?

To do that then I think you need to do something a little more complex than what you have posted. It isn't necessary to read exampleFile.txt into memory all at one time. Instead, read it one line at a time. If the line just read is a filename, then open that file and get ready to compare all the lines in exampleFile.txt until another filename is reached.

open exampleFile.txt
while not at end-of-file
    do we have a filename ?
         yes, then open second file
   otherwise
        read a line from second file and compare it with
        the line just read from exampleFile.txt.  If they are
        the same then continue the file loop and read another
        line from exampleFile.txt.  If however they are not the
        same you have to do something (issue a warning or
        do something else)
end of while

I think I'm still confused about what you want. According to your original post you have three files that contain portions of the one large file (exampleFile.txt). Each section of exampleFile.txt is separated by a line that begings with the text telemetre_.

What are the file names ? Are the names the same as the first section line ? such as telemetre_18_04_2007_11h_23_mn_15sec.dbt ? And you want to compare all the lines in that file with the lines that follow in exampleFile.txt ?

To do that then I think you need to do something a little more complex than what you have posted. It isn't necessary to read exampleFile.txt into memory all at one time. Instead, read it one line at a time. If the line just read is a filename, then open that file and get ready to compare all the lines in exampleFile.txt until another filename is reached.

open exampleFile.txt
while not at end-of-file
    do we have a filename ?
         yes, then open second file
   otherwise
        read a line from second file and compare it with
        the line just read from exampleFile.txt.  If they are
        the same then continue the file loop and read another
        line from exampleFile.txt.  If however they are not the
        same you have to do something (issue a warning or
        do something else)
end of while

Big thx for your help
this is my 3 files to compare them :

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.