读取csv文件源码
.cpp
#include “pch.h”
#include
#include <stdio.h>
using namespace std;
string Trim(string& str)
{
str.erase(0, str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}
stTrgInfo g_stTrgInfo;
int main()
{
bool b_TrgFlag = 0;
bool b_LogicFlag = 0;
int i = 0;
int j = 0;
int iLogicNum = 0;
int iStrLen = 0;
int iTrgValue = 0;
long int liWidth = 0;
long int liLowBit = 0;
string str_Type = “”;
string str_Name = “”;
string str_Width = “”;
string str_LowBit = “”;
string str_HighBit = “”;
string str_TrgValue = “”;
FILE *fp = NULL;
char filename[40] = { 0 };
printf("Input the file name\r\n");
printf("#~ ");
gets_s(filename);
//fp = fopen(filename, "r");
ifstream fin(filename); //打开文件流操作
string line;
while (getline(fin, line)) //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
{
//cout << "原始字符串:" << line << endl; //整行输出
istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
vector<string> fields; //声明一个字符串向量
string field;
iLogicNum = 0;
while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
{
fields.push_back(field); //将刚刚读取的字符串添加到向量fields中
iLogicNum++;
}
str_Type = Trim(fields[0]); //清除掉向量fields中第