c++ 用 list 或者 map 对 csv文件进行删减

kk_qwaz 2017-07-27 05:06:23
如图,把文件中 空格 和出错信息“#N/A”整行信息删除
最终只保留 StuNum(学号)、class(班级)、Dorm(宿舍)三列(删除其他三列)

结果如图所示


对list 、map函数 不太熟,网上搜了一些例子可是还是不会写,请各位大神指点一下
...全文
909 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
kk216927 2017-08-11
  • 打赏
  • 举报
回复

// 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);
		}
	}
	
}

kk_qwaz 2017-08-04
  • 打赏
  • 举报
回复

//结构体
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;
};
kk_qwaz 2017-08-04
  • 打赏
  • 举报
回复

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);
    }
}
在网上查了查,改成这样了
kk_qwaz 2017-08-02
  • 打赏
  • 举报
回复
引用 18 楼 qq_33866143 的回复:

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++;
    }
}
//这样就知道你存储内容的具体位置,思路因该是这样。
//根据位置筛选你的内容删除。
谢谢帮忙 但是我是这样想的,id、Name、StuNum、 class、 Dorm、 Age是 list 的6个节点,通过节点快速找到所需要的数据,然后删除,就不用遍历文件内容再删除了
kk_qwaz 2017-08-01
  • 打赏
  • 举报
回复
大神们路过请帮忙看一下
qq_33866143 2017-08-01
  • 打赏
  • 举报
回复

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++;
    }
}
//这样就知道你存储内容的具体位置,思路因该是这样。
//根据位置筛选你的内容删除。
唯恐天下不乱 2017-07-31
  • 打赏
  • 举报
回复
引用 13 楼 jk_216 的回复:
[quote=引用 8 楼 shit_moment 的回复:] 如果这样楼主还不会,那就没办法了。
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] 大神,用这个方法写,运行不出来[/quote] 这个只是告诉你一个主题的框架。 读文件的操作还要你来啊。 慢慢来,把遇到的问题具体发出来。会有人帮你看的。 这个问的太笼统
kk_qwaz 2017-07-31
  • 打赏
  • 举报
回复
void readCSV(istream &input, list<string > &output) { string csvLine; // read every line from the stream while (getline(input, csvLine)) { istringstream csvStream(csvLine); list<string> csvColumn; string csvElement; // read every element from the line that is seperated by commas and put it into the vector or strings while (getline(csvStream, csvElement, ',')) { csvColumn.push_back(csvElement); } output.push_back(csvColumn); } } 这是之前读入文件的代码,请问应该怎么改?
kk_qwaz 2017-07-31
  • 打赏
  • 举报
回复
另一篇帖子里看到一个新思路 定义一个结构体,把数据写到节点里 struct TcsvMake { int id; string Name; int StuNum; int class; int Dorm; int Age; }; typedef list<TcsvMake> LISTcsvMake; 不过这个怎么把文件读入到list里啊
kk_qwaz 2017-07-30
  • 打赏
  • 举报
回复
引用 11 楼 pan0227 的回复:
祝你成功,写出来就分享下,谢谢
感觉写不出来了
pan0227 2017-07-30
  • 打赏
  • 举报
回复
祝你成功,写出来就分享下,谢谢
kk_qwaz 2017-07-30
  • 打赏
  • 举报
回复
初学者写这个是不是比较难,还是没写出来,老是出错 今天再不出来就放弃了,还是谢谢大家的帮忙
kk_qwaz 2017-07-30
  • 打赏
  • 举报
回复
引用 8 楼 shit_moment 的回复:
如果这样楼主还不会,那就没办法了。
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] 大神,用这个方法写,运行不出来
唯恐天下不乱 2017-07-28
  • 打赏
  • 举报
回复
文件内容读进来之后,遍历一下, 是要删除的话,就把这一行删除。 然后把内容重新写到一个文件里。
kk216927 2017-07-28
  • 打赏
  • 举报
回复
有个疑惑,请教一下大家 我现在已经把a.csv文件读取到 list 里 看了一下list和map的函数,都是先写数据进去,然后再进行处理的 那么读取文件之后应该怎么处理
kk216927 2017-07-28
  • 打赏
  • 举报
回复
引用 2 楼 shit_moment 的回复:
erase函数就可以了吧
erase是删除一个或者一定范围的元素,可是csv文件中有n行6列数据,请问这个范围要怎么写
唯恐天下不乱 2017-07-28
  • 打赏
  • 举报
回复
erase函数就可以了吧
kk_qwaz 2017-07-28
  • 打赏
  • 举报
回复
引用 8 楼 shit_moment 的回复:
如果这样楼主还不会,那就没办法了。
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;
    }
  }
}
十分感谢,之前没接触过STL,正在学
唯恐天下不乱 2017-07-28
  • 打赏
  • 举报
回复
如果这样楼主还不会,那就没办法了。
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;
    }
  }
}
引用 6 楼 jk_216 的回复:
[quote=引用 5 楼 shit_moment 的回复:] 文件内容读进来之后,遍历一下, 是要删除的话,就把这一行删除。 然后把内容重新写到一个文件里。
还是不懂要怎么删除行、列 [/quote]
赵4老师 2017-07-28
  • 打赏
  • 举报
回复
所谓修改删除文件a某位置的内容,其实是读打开文件a,再将‘a中修改删除位置之前的内容+修改删除的内容+a中修改删除位置之后的内容’保存到文件b,关闭文件a,删除文件a,将文件b改名为与之前文件a相同的名字,仅此而已。http://bbs.csdn.net/topics/391975224
加载更多回复(2)

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧