这是我从 .csv 文件中获取的代码,它让女巫在分隔符之间并给出这些分隔符的位置。但这确实只针对第一行如何继续第二行??
#include <tchar.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
//#include <limits>
using namespace std;
#define TAILLE_MAX_LIGNE 1203
int _tmain(int argc, _TCHAR* argv[])
{
char stringFile [TAILLE_MAX_LIGNE];
string stringLineToAnalyse;
size_t positionCharOld, positionCharNew;
string separateurChar = ";";
string contenuLocalChamp = "";
vector <string> localStringVector;
localStringVector.clear(); // Initialisation du vecteur // VECTOR INTIALISE
ifstream file;
file.open("C:/Users/Alex/Desktop/STAGE/test.csv");
if(file.is_open())
{
file.getline(stringFile, TAILLE_MAX_LIGNE);
// file.ignore(numeric_limits<streamsize>max(),'\n'));
stringLineToAnalyse = stringFile;
cout << "tout va bien" << endl;
cout << stringLineToAnalyse << endl;
// initialisation de la recherche dans la ligne
// INITIALISE SEARCH INTO THE LIGNE
positionCharOld = 0;
bool finDelaBoucle = false;
while(finDelaBoucle == false)
{
// boucle itérative
positionCharNew = stringLineToAnalyse.find(separateurChar, positionCharOld);
if(positionCharNew != string::npos)
{
cout << "separateur trouve a la position " << positionCharNew << endl; // SEPARATOR POSITION
if((positionCharNew-positionCharOld) > 0)
{
contenuLocalChamp = stringLineToAnalyse.substr(positionCharOld, positionCharNew-positionCharOld);
cout << "le contenu de la string entre separateur est " << contenuLocalChamp << endl; // CONTENT BEATWEEN 2 SEPARATOR
localStringVector.push_back(contenuLocalChamp);
}
else
{
cout << "ce champ vide" << endl; // EMPTY FIELD
}
positionCharOld = positionCharNew+1;
}
else
{
finDelaBoucle = true;
system("PAUSE");
cout << "fin de la boucle" << endl; // END OF THE LOOP
system("PAUSE");
}
}
}
else
{
cout << "pas de fichier" << endl;
system("PAUSE");
}
return 0;
}
最佳答案
整个例子与问题不是很相关:如何逐行读取文件
std::string s;
while (std::getline(file, s))
{
std::cout << s << "\n"; // each next line printed
}
注意 std::getline
的使用,这比干预 char[]
安全得多
while(std::getline(file, stringLineToAnalyse))
{
会查看示例代码中的要点
关于c++ - 如何在 C++ 中获取不同行的 .csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162033/