-1

I have 79 .txt files like this:

iRun iDate showerEnergy thetaRad phiRad totalShowers totalParticles  e+ e- μ+ μ-
4001 121125 5e+07 1.61401 0.00118607 1 9929166 6909475 1271116 1399686 151330 148624
-1 4001 121125 5e+07 1.61401 0.00118607 2 9929167 6909475 1271116 1399686 151330 148624

it has just 3 lines, 1st the iRun iDate on this line and 2nd is starting with 4001 and 3rd starting with -1. And I just want to read these text files' 2nd lines. Also I want to write all 79 text files' 2nd lines in a text file if I read them clearly.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
Umut Keskin
  • 37
  • 1
  • 10
  • Which language do you want to use? What have you tried so far? – René Vogt Mar 04 '16 at 15:51
  • I am a newbie to this kind of reading/writing works. I want to use C++ language, want a simple thing just want to read all whole 2nd lines of all x79 files, if i be able to read/write one, i can make the others. – Umut Keskin Mar 04 '16 at 16:32
  • I'm sorry my answer was for C# not C++. I just deleted it. – Quantic Mar 04 '16 at 16:35
  • Try to looks at [all these answers](http://stackoverflow.com/search?q=line+reading+%5Bc%2B%2B%5D) for how to read files by lines in C++ – Soren Mar 04 '16 at 16:49
  • `getline(fin, line); getline(fin, line); fout << line << std::endl;` – crashmstr Mar 04 '16 at 16:59

2 Answers2

1

This is very trivial with <fstream>. A very basic program would look something like:

#include <fstream>
#include <string>

int main() {
    std::ifstream infile("indata.txt"); // Open input file
    std::string lineData = "";

    getline(infile, lineData); // Get first line.
    getline(infile, lineData); // Get second line.

    std::ofstream outfile;
    outfile.open("outdata.txt", std::ios_base::app);
    outfile << lineData << std::endl; // Append lineData to outfile. Creates the file if necessary.
    return 0;
}
Josiah
  • 4,663
  • 2
  • 31
  • 49
  • This just worked thank you a lot. But i want to ask a simple question too. How can i add just numbers for them in to their first starting lines, i mean when i writing the first text i want to add 1, and 2 for 2nd etc. – Umut Keskin Mar 04 '16 at 17:19
  • And of course, i can not write more than 1 txt file's 2nd line in another text file. I really don't understand. How can i duplicate your method for more than 40 txt files ? All 2nd lines must go to 1 common text file. I can just add first, second not works. – Umut Keskin Mar 04 '16 at 17:54
  • You will need to use a `for` loop. I'll let you investigate how to do that. There are plenty of resources for understanding them. – Josiah Mar 04 '16 at 18:35
0

try writing something.

psuedo code looks like this

open outfile
for each text file
{
   open file
   skip line
   read line
   write line to out file
   close file
}
close output file

so the first thing you are going to have to do is work out how to enumerte the files. Two choices:

the rest is simple c++ file io

If this is a practical problem (as opposed to a class assignment) I would used awk, perl, sed etc rather than write code

Community
  • 1
  • 1
pm100
  • 48,078
  • 23
  • 82
  • 145