用C ++编写csv文件 [英] Write in csv file in C++
问题描述
我在csv / txt文件中有以下格式的一些数据:
7/11/2012 6:45,7 / 12/2012 6: 45,1.0139,1.0123,1.0067,1.0236,1.0253,1.0289,
7/11/2012 6:45,7 / 12/2012 6:45,96.95,97.08,96.86,0,0, 0,
7/11/2012 6:45,7 / 12/2012 6:45,1.5507,1.5487,1.5441,1.5623,1.5639,1.5691,
我正在尝试解析这些行并写入另一个csv文件。 CSV必须包含不同的行和&列。
我的代码是:
void readInput( char * fileName){
printf( **************** ReadInput *************** \\\
跨度>);
// 读取文件
ifstream fin(fileName);
string tmp = ; // 输入文件中的一行
while (getline(fin,tmp)){
// 将字符串添加到矢量中
listString.push_back(tmp);
cout<< listString [listString.size() - 1 ]<< ENDL;
}
printf( \ n\ n);
fin.close();
}
/ *
* parseValues用于提取数据输入文件
* /
void parseValues(){
printf( **************** ParseValue *************** \\ \
跨度>);
for ( int i = 0 ; i< listString.size(); ++ i){
char tmp [ 100000 < /跨度>];
strcpy(tmp,listString [i] .c_str()); // 将字符串复制到char数组
stringArray tmpArray;
// 利用字符串标记提取数据
char * pch;
pch = strtok(tmp, ,);
while (pch!= NULL){
tmpArray.push_back(pch);
printf( %s \ t,pch);
// 获取下一个标记
pch = strtok(NULL, ,);
}
data.push_back(tmpArray);
printf( \ n);
}
}
/ *
* parseValues是在文件开头写入数据
* /
void writeOutput01( char * fileName){
ofstream fout(fileName);
// 每行
for ( int i = 0 ; i< listString.size(); + + i){
// 每列
for ( int j = 0 ; j< data [i ] .size(); ++ j)
fout<< data [i] [j]<< ' ,';
fout<< \ n;
}
fout.close();
}
解析和写入时,写入3行1列。
它写在一列而不是每行8列。
如何解决这个问题?
问候,
< blockquote>您认为\ n是行尾字符,但并非如此。不幸的是,这取决于您的平台。请参阅:
http://en.wikipedia.org/wiki/End_of_line [< a href =http://en.wikipedia.org/wiki/End_of_linetarget =_ blanktitle =New Window> ^ ]。
您需要以特定于您的平台的方式编写行尾,或使其跨平台。请参阅:
http://stackoverflow.com / questions / 8689344 / portable-end-of-line-newline-in-c [ ^ ]。
int n = 1,k = 2,l = 9;
ofstream o,a;
o.open(test.csv,ios :: app);
o<< n<<,<< l< < << k; //像这样做将把它放在不同的列
Hi,
I have some data in csv/txt file in below format:
7/11/2012 6:45,7/12/2012 6:45,1.0139,1.0123,1.0067,1.0236,1.0253,1.0289,
7/11/2012 6:45,7/12/2012 6:45,96.95,97.08,96.86,0,0,0,
7/11/2012 6:45,7/12/2012 6:45,1.5507,1.5487,1.5441,1.5623,1.5639,1.5691,
I am trying parsing this lines and write in another csv file. CSV must be include different rows & columns.
My code is:
void readInput(char* fileName) {
printf("****************ReadInput***************\n");
// read file
ifstream fin(fileName);
string tmp = ""; // a line in the input file
while(getline(fin, tmp)) {
// add string into vector
listString.push_back(tmp);
cout << listString[listString.size() - 1] << endl;
}
printf("\n\n");
fin.close();
}
/*
* parseValues is to extract data in the input file
*/
void parseValues() {
printf("****************ParseValue***************\n");
for (int i = 0; i < listString.size(); ++i) {
char tmp[100000];
strcpy(tmp, listString[i].c_str()); // copy string to char array
stringArray tmpArray;
// utilize string token to extract data
char * pch;
pch = strtok (tmp,",");
while (pch != NULL) {
tmpArray.push_back(pch);
printf ("%s\t",pch);
// get the next token
pch = strtok (NULL, ",");
}
data.push_back(tmpArray);
printf("\n");
}
}
/*
* parseValues is to write data at the beginning of file
*/
void writeOutput01(char* fileName) {
ofstream fout(fileName);
// for each row
for (int i = 0; i < listString.size(); ++i) {
// for each column
for (int j = 0; j < data[i].size(); ++j)
fout << data[i][j] << ',';
fout << "\n";
}
fout.close();
}
When it parsing and write, writes in 3 rows and one column.
It writes in one column instead 8 columns for each line.
How to fix this?
Regards,
You think that "\n" is the end of line character, but it is not exactly so. Unfortunately, it depends on your platform. Please see:
http://en.wikipedia.org/wiki/End_of_line[^].
You need to write end of line in a way specific for your platform or make it cross-platform. Please see:
http://stackoverflow.com/questions/8689344/portable-end-of-line-newline-in-c[^].
—SA
int n=1, k=2,l=9;
ofstream o,a;
o.open("test.csv", ios::app) ;
o<<n<<","<<l<<","<<k; // doing it like this will put it in different columns
这篇关于用C ++编写csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!