I want to make the second column of File_2 append to File_1.
As the following,
1.0E-8, 1.2E-3, 2.4E-3
1.3E-8, 1.1E-3, 2.2E-3
1.5E-8, 1.5E-3, 2.0E-3
My idea is to take the File_2's second column into a string array,
then ">>" into a file, but failed. Using cout can find nothing in the array.
Besides, I tried to make the second column(string) convert to number then convert back into string when ">>", but obtained "segment failure", as the 21,35 and 93 lines in the attached script.
Does anybody have ideas or suggestions?
Many thanks for your help!
1 ///////////////////////////////////////////////////////////////////////////// 2 /// This is final for one channel ".csv" file and another for ".tsv" file ///
3 /////////////////////////////////////////////////////////////////////////////
4 #include <iostream>
5 #include <sstream>
6 #include <fstream>
7 #include <vector>
8 #include <string>
9 #include <cstring>
10 11 using namespace std;
12
13 int main ()
14 {
15 16 //part 1, is to get the amplitude of channel_1 and put them into an array.
17 ifstream inFile_ch1 ("../test_7_10lines.tsv"); 18 string line_ch1;
19 int linenum_ch1 = 0;
20 string amp_ch1[5000];
21 //double amp_ch1[5000];
22 while (getline (inFile_ch1, line_ch1))
23 {
24 linenum_ch1++;
25 if(linenum_ch1 >= 22)
26 {
27 istringstream linestream(line_ch1);
28 string item_ch1;
29 int itemnum_ch1 = 0, counter_ch1 = 0;
30 while (getline (linestream, item_ch1, ' ' ) )
31 {
32 itemnum_ch1++;
33 counter_ch1 = (itemnum_ch1-1)/2;
34 if ((itemnum_ch1%2)==0) amp_ch1[counter_ch1] = item_ch1;
35 // istringstream(item_ch1) >> amp_ch1[counter_ch1] ;
36 //get item_ch1, convert into number,
37 }
38 }//end of if(linenum_ch1>>22)
39 }//enf of while(getline(inFile_ch1,line_ch1))
40 //end of "part 1"
41
42 // cout<<"amp_ch1_0 = "<<amp_ch1[0]<<endl<<"amp_ch1_1 = "<<amp_ch1[1]<<endl<<"amp_ch1_2 = "<<amp_ch 1[2]<<"amp_ch1_3 = "<<amp_ch1[3]<<endl;
43
44
45 // part 2, get the time info from another file which contains two channels' time info
46 ifstream inFile ("../test_8_10lines.csv");
47 string line;
48 int linenum = 0;
49 while (getline (inFile, line))
50 {
51 linenum++;
52 if(linenum >= 22)
53 {
54 istringstream linestream(line);
55 double first_amp_base[5000];
56 //to the linenum = 22, take all of the items as the base item to be subtracted to other ite ms.
57 //bulid an array to store all of this line's items.
58 if (linenum == 22)
59 {
60 string item_22;
61 int counter_22 = 0, itemnum_22 = 0;
62 while (getline (linestream, item_22, ','))
63 {
64 itemnum_22 ++;
65 counter_22 = (itemnum_22-1)/2;
66 if ( (itemnum_22 % 2) != 0)
67 istringstream(item_22) >> first_amp_base[counter_22] ;
68 }
69 }
70
71 //End of part 2.
72
73 // part 3, get channel 2's amplitude info and two channel's time info then write to some files.
74 string item, first_item_line, firstamp, amp_ch1;
75 double first_amp_line_number, first_amp_line_result;
76 int itemnum = 0, counter = 0;
77 while (getline (linestream, item, ','))
78 {
79 itemnum++;
80 counter = (itemnum-1)/2;
81
82 stringstream sstrm;
83 sstrm << "two_channels_"<<counter << ".tsv";
84 ofstream outFile(sstrm.str().c_str(),ios_base::app);
85
86 if ((itemnum%2)==0)
87 {
88 if ( !(istringstream(first_item_line) >> first_amp_line_number) ) first_amp_line_number = 0.0;
89 //get first_item_line, convert into number,
90 first_amp_line_result = first_amp_line_number - first_amp_base[counter];
91 //then "- first_amp_base[i]".
92 string first_item_line = static_cast<ostringstream*>( &(ostringstream() << first_amp _line_result))->str();//convert the subtracted result into string.
93 //string amp_ch1 = static_cast<ostringstream*>( &(ostringstream() << amp_ch1[counter ]))->str();//convert the subtracted result into string.
94
95 outFile << first_item_line <<" "<< amp_ch1[counter] <<" "<<item<<endl;
96 }
97 first_item_line = item ;
98 outFile.close();
99 }
100 //End of part 3.
101 }//end of if(linenum>>22)
102 }
103 return 0;
104 }
What's with the line numbers in the unformatted post? Please copy and paste properly formatted code, highlight the code, and press the <> button to the right of the editor box.
I want to make the second column of File_2 append to File_1.
As the following,
1.0E-8, 1.2E-3, 2.4E-3
1.3E-8, 1.1E-3, 2.2E-3
1.5E-8, 1.5E-3, 2.0E-3
My idea is to take the File_2's second column into a string array,
then ">>" into a file, but failed. Using cout can find nothing in the array.
Besides, I tried to make the second column(string) convert to number then convert back into string when ">>", but obtained "segment failure", as the 18,32 and 90 lines in the attached script.
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
usingnamespace std;
int main ()
{
//part 1, is to get the amplitude of channel_1 and put them into an array.
ifstream inFile_ch1 ("../test_7_10lines.tsv");
string line_ch1;
int linenum_ch1 = 0;
string amp_ch1[5000];
//double amp_ch1[5000];
while (getline (inFile_ch1, line_ch1))
{
linenum_ch1++;
if(linenum_ch1 >= 22)
{
istringstream linestream(line_ch1);
string item_ch1;
int itemnum_ch1 = 0, counter_ch1 = 0;
while (getline (linestream, item_ch1, ' ' ) )
{
itemnum_ch1++;
counter_ch1 = (itemnum_ch1-1)/2;
if ((itemnum_ch1%2)==0) amp_ch1[counter_ch1] = item_ch1;
// istringstream(item_ch1) >> amp_ch1[counter_ch1] ;
//get item_ch1, convert into number,
}
}//end of if(linenum_ch1>>22)
}//enf of while(getline(inFile_ch1,line_ch1))
//end of "part 1"
// cout<<"amp_ch1_0 = "<<amp_ch1[0]<<endl<<"amp_ch1_1 = "<<amp_ch1[1]<<endl<<"amp_ch1_2 = "<<amp_ch1[2]<<"amp_ch1_3 = "<<amp_ch1[3]<<endl;
// part 2, get the time info from another file which contains two channels' time info
ifstream inFile ("../test_8_10lines.csv");
string line;
int linenum = 0;
while (getline (inFile, line))
{
linenum++;
if(linenum >= 22)
{
istringstream linestream(line);
double first_amp_base[5000];
//to the linenum = 22, take all of the items as the base item to be subtracted to other items.
//bulid an array to store all of this line's items.
if (linenum == 22)
{
string item_22;
int counter_22 = 0, itemnum_22 = 0;
while (getline (linestream, item_22, ','))
{
itemnum_22 ++;
counter_22 = (itemnum_22-1)/2;
if ( (itemnum_22 % 2) != 0)
istringstream(item_22) >> first_amp_base[counter_22] ;
}
}
//End of part 2.
// part 3, get channel 2's amplitude info and two channel's time info then write to some files.
string item, first_item_line, firstamp, amp_ch1;
double first_amp_line_number, first_amp_line_result;
int itemnum = 0, counter = 0;
while (getline (linestream, item, ','))
{
itemnum++;
counter = (itemnum-1)/2;
stringstream sstrm;
sstrm << "two_channels_"<<counter << ".tsv";
ofstream outFile(sstrm.str().c_str(),ios_base::app);
if ((itemnum%2)==0)
{
if ( !(istringstream(first_item_line) >> first_amp_line_number) ) first_amp_line_number = 0.0;
//get first_item_line, convert into number,
first_amp_line_result = first_amp_line_number - first_amp_base[counter];
//then "- first_amp_base[i]".
string first_item_line = static_cast<ostringstream*>( &(ostringstream() << first_amp_line_re
sult))->str();//convert the subtracted result into string.
//string amp_ch1 = static_cast<ostringstream*>( &(ostringstream() << amp_ch1[counter]))->str
();//convert the subtracted result into string.
outFile << first_item_line <<" "<< amp_ch1[counter] <<" "<<item<<endl;
}
first_item_line = item ;
outFile.close();
}
//End of part 3.
}//end of if(linenum>>22)
}
return 0;
}
OMG! Why are you using C++ for text processing?
Use the right tool for the job: do it in Perl, Ruby or Scala.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Import the classes from io package. These classes are used to read or write from files.
import scala.io._
// Read file1 line by line. Split each line into array using "," as a separator and take the second value (arrays are indexed from 0, just as in C, thus the 1 in the code). f1 gets an iterator iterating over Strings.
val f1 = (Source fromFile "file1" getLines) map (_.split(", *")(1))
// Read file2 line by one. f2 gets an iterator over the lines of the file (as String)
val f2 = Source fromFile "file2" getLines
// Create a new object for writing to the output file.
val out = new java.io.FileWriter("file3")
// Append lines f2 to corresponding lines of f1 and write the result using the out object.
f1 zip f2 foreach { x => out.write(x._1 + ", " + x._2 + "\n") }
// Close the out object (flushes and closes the output file)
out.close
6 lines instead of 102 and uses much less memory for large files. :D
Right, I know, but when someone uses extremely wrong tool for the job, it might be good to tell him some other, better tools exist. Otherwise one can get very fast dissapointed with C++ - because he is doing things that C++ was not designed to do.
Thanks for your comment.
To be honest, I do have no any knowledge on Perl, Ruby or Scala. So,
1, please tell me the code you attached is which kind of software above three?
2, I don't understand your code completely, do you mind to add some comments ,
and/or provide some links or articles which will be good for my understanding?
1. This is Scala. You can start here: www.scala-lang.org. At first the language might seem complicated (although not as complicated as C++), but programming is very easy. I'm sure you will enjoy it.
2. I do not think any comments are needed, the code is really self explanatory, when you know the Scala's standard library. But, ok, I'll add them.