64,637
社区成员
发帖
与我相关
我的任务
分享
// CFileRead.h
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <list>
using namespace std;
struct TcsvMake
{
int id;
string Name;
int StuNum;
int class;
int Dorm;
int Age;
};
typedef list<TcsvMake> LISTcsvMake;
class CFileRead
{
public:
//CFileRead();
//virtual ~CFileRead();
void readCSV(ifstream &input);
LISTcsvMake m_csvMakeList;
};
//CFileRead.cpp
#include<iostream>
#include <list>
#include"CFileRead.h"
#include "string.h"
#pragma warning(disable:4996)
using namespace std;
void CFileRead::readCSV(ifstream &input)
{
string csvLine;// 从输入流中读取每一行
getline(input, csvLine);
while (getline(input, csvLine))
{
TcsvMake tcsv;
char *csv = (char*)csvLine.data();
if (NULL != csv)
{
string id = strtok(csv, ",");
istringstream csvStream(id);
csvStream >> tcsv.id;
string name = strtok(NULL, ",");
istringstream CsvStream(name);
CsvStream >> tcsv.Name;
string stuNum = strtok(NULL, ",");
istringstream cSvStream(stuNum);
cSvStream >> tcsv.StuNum;
string class = strtok(NULL, ",");
istringstream csVStream(class);
csVStream >> tcsv.class;
string dorm = strtok(NULL, ",");
istringstream CSvStream(dorm);
CSvStream >> tcsv.Dorm;
string age = strtok(NULL, ",");
istringstream CSVStream(age);
CSVStream >> tcsv.Age;
m_csvMakeList.push_back(tcsv);
}
}
}
//结构体
struct TcsvMake
{
int id;
string Name;
int StuNum;
int class;
int Dorm;
int Age;
};
typedef list<TcsvMake> LISTcsvMake;
//CFileRead 类
class CFileRead
{
public:
void readCSV(istream &input);
LISTcsvMake m_csvMakeList;
};
void CFileRead::readCSV(istream &input)
{
m_csvMakeList;
string csvLine;
// 从输入流中读取每一行
while (getline(input, csvLine))
{
istringstream csvStream(csvLine);
list <string> csvColumn;
string csvElement;
TcsvMake tcsv;
// 从逗号分隔的行中读取每个元素,并把它转化为向量或字符串
while (getline(csvStream, csvElement, ','))
{
csvColumn.push_back(csvElement);
csvStream >> tcsv.id >> tcsv.Name >> tcsv.StuNum >> tcsv.class >> tcsv.Dorm >> tcsv.Age;
}
m_csvMakeList.push_back(tcsv);
}
}
在网上查了查,改成这样了
class TcsvMake
{
string text;
int CellRow;
int CellNum
};
typedef list<TcsvMake> LISTcsvMake;
void readCSV(istream &input, list<TcsvMake> &output)
{
int i = 0 ;
while (getline(input, csvLine))
{
istringstream csvStream(csvLine);
// list<string> csvColumn;
class TcsvMake* csvMake= new TcsvMake();
string csvElement;
// read every element from the line that is seperated by commas and put it into the vector or strings
int j = 0 ;
while (getline(csvStream, csvElement, ','))
{
csvMake->text= csvElement;
csvMake->CellNum =i;
csvMake->CellNum = j ;
output.push_back(csvMake);
j++;
}
i++;
}
}
//这样就知道你存储内容的具体位置,思路因该是这样。
//根据位置筛选你的内容删除。
typedef std::list<std::string> TRow;
typedef std::list<TRow> TTable;
TTable table;
TRow row;
row.push_back("170220");
table.push_back(row);
TTable::iterator itRow = table.begin();
for (; itRow != table.end(); itRow++;)
{
TRow::iterator itCell = it->begin();
for (;itCell != itRow->end();itCell++)
{
if(itCell->comapre("#N/A") ==0)
{
itRow = table.erase(itRow);
break;
}
}
}
还是不懂要怎么删除行、列
[/quote]