如何阅读更多的CSV文件 [英] how to read more csv file
问题描述
我有一个C ++脚本,可以读取一个csv文件,对其进行解析并写入另一个csv文件中.
当我尝试为一个文件运行该文件时,它的效果很好.但是,当尝试一起读写某些文件时,只会对第一个文件起作用.
Hi,
I have one C++ script that read one csv file, parse that and write in another csv file.
It works great when I try run that for one file. But when try read and write some files together it only do for first file.
#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <stdio.h>
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
typedef vector<string> stringArray;
typedef vector<char[40]> charArray;
stringArray listString; // store each line in the input file
vector<stringArray> data; // store data of the input file
/*
* readInput is to read the input file name and get data in that file.
*/
void readInput(char *fileName){
// printf("****************ReadInput***************\n");
// get input file name
// printf("Input file name: ");
int sum=0;
// char fileName[256];
// cin.getline (fileName,256);
// read file
freopen(fileName, "r", stdin);
string tmp = ""; // a line in the input file
while(getline(cin, tmp)){
// add string into vector
listString.push_back(tmp);
// cout << listString[listString.size() - 1] << endl;
sum=sum+1;
cout<<sum<<endl;
if(sum>100000)break;
}
printf("\n\n");
}
/*
* parseValues is to extract data in the input file
*/
void parseValues(){
printf("****************ParseValue***************\n");
for (int i = 0; i < listString.size(); ++i){
char tmp[2048];
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 to the output.csv
*/
void writeOutput(){
freopen("output1.csv", "w", stdout);
//freopen("reza.txt", "w", stdout);
// for each row
for (int i = 0; i < listString.size(); ++i){
// for each column
for (int j = 0; j < data[i].size(); ++j)
//if(atof(data[i][1].c_str())>1300)
{
cout << data[i][j].c_str() << ",";
}
cout << "\n";
}
}
int main(){
readInput("book2.csv");
parseValues();
writeOutput();
readInput("1.csv");
parseValues();
writeOutput();
return 0;
}
请说一下我该如何解决,我的代码也读取第二个文件?
问候,
Please say how I can solve this and my code read second file too?
Regards,
推荐答案
您只需要循环输入文件列表即可.
Hi,
You need just to loop on a input files list.
int main(){
int x;
string fileNameArray[3] = {"book1.csv", "book2.csv", "book3.csv"};
for (x = 0; x < 3; x ++) {
readInput(fileNameArray[i].c_str());
parseValues();
writeOutput();
}
return 0;
}
不要忘记更改输出文件名的每个名称,否则将被删除.
我看到了这个问题,您需要关闭文件,然后在写出put函数的末尾进行操作:
Don''t forget to change output file name each name, other wise it would be erased.
I see the problem, you need to close your file ate the end of the write out put functions :
/*
* parseValues is to write data to the output.csv
*/
void writeOutput(){
freopen("output1.csv", "w", stdout);
//freopen("reza.txt", "w", stdout);
// for each row
for (int i = 0; i < listString.size(); ++i){
// for each column
for (int j = 0; j < data[i].size(); ++j)
//if(atof(data[i][1].c_str())>1300)
{
cout << data[i][j].c_str() << ",";
}
cout << "\n";
}
fclose (stdout);
}
最好的问候.
EL GAABEB.
Best Regards.
EL GAABEB.
一个简单的循环并清除缓冲区即可解决问题.
谢谢,
Hi,
A Simple loop and clearing the buffers will do the trick.
Thanks,
自从您谈论C ++以来,我认为您应该更有效地使用C ++(请参阅我对问题的评论):
例如
主要功能:
Since you talk about C++, I think you should use C++ more effectively (see my comment to your question):
E.g.
Main function:
int Main()
{
CReadWriteCsv in1("In1.csv");
in1.read(1000);
CReadWriteCsv in2("In2.csv");
in2.read(1000);
in1.append("output.csv");
in2.append("output.csv");
return 0;
}
和用于处理CSV文件的类:
And the class to process CSV files:
#ifndef CREARWRITECSV_H
#define CREARWRITECSV_H
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
class CReadWriteCsv
{
public:
CReadWriteCsv(char* infile): m_infile(infile) {}
void read(int maxRecords)
{
ifstream is(m_infile);
read(is, maxRecords);
}
void append(char* outfile)
{
ofstream os(outfile, ios_base::app);
write(os);
}
private:
typedef vector<string> CRecord;
typedef vector<CRecord> CContent;
string m_infile;
CContent m_content;
void read(istream& is, int maxRecords)
{
m_content.clear();
string line;
while(maxRecords-- > 0 && getline(is, line))
{
CRecord record;
m_content.push_back(readLine(line, record));
}
}
void write(ostream& os)
{
for (CContent::iterator it=m_content.begin(); it!=m_content.end(); ++it)
{
writeRecord(*it, os);
}
}
CRecord& readLine(const string& line, CRecord& record)
{
string field;
istringstream iss(line, istringstream::in);
while(getline(iss, field, ',')) record.push_back(field);
return record;
}
void writeRecord(CRecord& record, ostream& os)
{
for(CRecord::iterator it=record.begin(); it!=record.end(); ++it)
{
if (it!=record.begin()) os << ",";
os << *it;
}
os << "\n";
}
};
#endif
这篇关于如何阅读更多的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!