using multiple files using C++ and saving them with same name
Aug 22, 2013 at 12:59pm UTC
I have 60 files like summary_1, summary_2, summary_3 and so on. I need to remove the first line "time dayofyr" from each file and store them in the same file. I am using the following code, but it is giving error while converting string to chr
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 52 53
#include <iostream>
#include <fstream>
#include <string>
#include<sstream>
#include<cstring>
using namespace std;
int main()
{
for (int i=1;i<6;i++)
{
stringstream aa;
aa<<"summary_" <<i<<".out" ;
string a = aa.str();
stringstream oo;
oo<<"out_" <<i<<".txt" ;
string o= oo.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!="time dayofyr" )
out<<line<<endl;
}
summary.close();
out.close();
char * operator
{
return a.c_str();
}
remove(a);
char * operator
{
return a.c_str();
}
char * operator
{
return o.c_str();
}
rename(o,a);
}
system("pause" );
return 0;
}
Aug 22, 2013 at 1:04pm UTC
the stream (previous to C++11) don't take std::string.
use .c_str():
ifstream summary (a.c_str());
Aug 22, 2013 at 1:13pm UTC
¿what are you trying to do in lines 33--45?
Also, ¿is it so hard to indent your code?
Aug 22, 2013 at 1:19pm UTC
I have used the same as in line 35. but it is showing error
Aug 22, 2013 at 1:33pm UTC
that makes no sense at all.
change this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
char * operator
{
return a.c_str();
}
remove(a);
char * operator
{
return a.c_str();
}
char * operator
{
return o.c_str();
}
rename(o,a);
to this
1 2
remove(a.c_str());
rename(o.c_str(),a.c_str());
Aug 22, 2013 at 1:40pm UTC
thank you very much!!
Topic archived. No new replies allowed.