Changing multiple files using C++

I have 60 files in text format. they are summary_1, summary_2, summary_3 and so on. first line of each file is "time dayof". I need to remove the "time dayof" line from each file. I am doing the following code but unable to open new files as I could not understand how to use the for loop here.

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
  #include <iostream>
#include <fstream>
#include <string>
#include<cstdio>
using namespace std;
int main()
{
    
	for(int i=1;i<60;i++)
	{


   ifstream summary ( "summary.out" );
   ofstream out("outfile.txt"); //temporary data storage

   string line;
   while(getline(summary,line)) //reading the summary.out file line by line
   {
	   if(line!="time  dayof")
		   out<<line<<endl;
   }
   summary.close();
   out.close();

   remove("summary.out");
   rename("outfile.txt","summary.out");
   }


  system("pause");
  return 0;
}
You need to construct a string that contains the name of the summary file for the current iteration of the loop. This string will need to be constructed as "summary_" followed by the current loop index, followed by the ".out" extension.

You can then use this filename to open the file, rather than just "summary.out" as you're doing now.
I have tried to build up the string but it is giving error
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
#include <iostream>
#include <fstream>
#include <string>
#include<cstdio>
using namespace std;
int main()
{
  
	for(int i=1;i<6;i++)
	{
  stringstream a;

	a<<"summary_"<<i<<".out";	 

   ifstream summary (a);
   ofstream out("outfile.txt"); //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();

   /*remove("summary.out");
   rename("outfile.txt","summary.out");
 

}

  system("pause");
  return 0;
} */
solved the problem. thank you
We are established psychics, but could you nevertheless humour us by telling the error message?


You say that you have data to process. This implies that you are not just learning to program, but actually need to do something "useful". The obvious question is then: Why C++? A one-liner shell script with common GNU programs accomplishes the "remove first line from file(s)" hands down.
Topic archived. No new replies allowed.