Removing a line from a file
Aug 23, 2013 at 9:53am UTC
I have 60 different files. they are arranged like summary_1_1, summay_1_2 like this upto summary_1_10. again, summary_2_1, summary_2_2...so on. I have 6 groups (summary_1 to summary_6) and 10 files in each group. all are txt files.
I need to remove the first line from each file. the first line is the name of the file like summay_1_1 or summary_1_2....
I am using the following code, but it is not running and giving error at line 37. any suggestion??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include <fstream>
#include <string>
#include<sstream>
#include<direct.h>
using namespace std;
int main()
{
for (int i=1;i<6;i++)
{
stringstream aa;
aa<<"out_" <<i;
string b=aa.str();
mkdir(b.c_str());
for (int j=1;j<6;j++)
{
stringstream aa;
aa<<"summary_" <<i<<"_" <<j<<".out" ;
string a = aa.str();
stringstream oo;
oo<<b<<"/out_" <<i<<"_" <<j<<".txt" ;
string o= oo.str();
stringstream MORECS;
MORECS<<"MORECS" <<"_" <<i<<"_" <<j;
string MORECS= MORECS.str();
ifstream summary (a);
ofstream out(o); //temporary data storage
string line;
while (getline(summary,line)) //reading the summary.out file line by line
{
if (line!= MORECS)
out<<line<<endl;
}
summary.close();
out.close();
//remove(a.c_str());
//rename(o.c_str(),a.c_str());
}
}
system("pause" );
return 0;
}
Aug 23, 2013 at 10:04am UTC
sorry I did a mistake in the post
the code is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include <fstream>
#include <string>
#include<sstream>
#include<direct.h>
using namespace std;
int main()
{
for (int i=1;i<6;i++)
{
stringstream aa;
aa<<"out_" <<i;
string b=aa.str();
mkdir(b.c_str());
for (int j=1;j<10;j++)
{
stringstream aa;
aa<<"summary_" <<i<<"_" <<j<<".out" ;
string a = aa.str();
stringstream oo;
oo<<b<<"/out_" <<i<<"_" <<j<<".txt" ;
string o= oo.str();
stringstream MORECS;
MORECS<<"summary" <<"_" <<i<<"_" <<j;
string MORECS= MORECS.str();
ifstream summary (a);
ofstream out(o); //temporary data storage
string line;
while (getline(summary,line)) //reading the summary.out file line by line
{
if (line!= MORECS)
out<<line<<endl;
}
summary.close();
out.close();
//remove(a.c_str());
//rename(o.c_str(),a.c_str());
}
}
system("pause" );
return 0;
}
Aug 23, 2013 at 10:10am UTC
what about line 29? that should give you an error as well and is the source for line 37.
You define two variables with the same name within the same scope
Aug 23, 2013 at 10:15am UTC
Thank you. I hope now it will run
Aug 24, 2013 at 6:57am UTC
You will need to change your loops to get all six groups and ten files.
1 2 3 4
...
for (int i=1;i<=6;i++)
...
for (int j=1;j<=10;j++)
Topic archived. No new replies allowed.