【问题标题】:How to get different line of .csv File in c++?如何在 C++ 中获取不同行的 .csv 文件?
【发布时间】:2012-10-21 03:39:36
【问题描述】:

这是我从 .csv 文件中获取的代码,它让 itch 女巫在分隔符之间,并给出这些分隔符的位置。但这确实只针对第一行如何继续以第二行为例??

#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;
}

【问题讨论】:

  • 循环读取?例如。 while (file.getline(...))
  • 你应该清理你的格式,那些空格真的很糟糕。此外,这是一个说英语的网站,所以 cmets 对法语的任何人都没有用。
  • 对不起,所有法国人的事情,不会再发生了!
  • 还有可怕的空间......

标签: c++ csv getline


【解决方案1】:

假设您尝试读取的文件格式或多或少正确,您很可能希望使用在&lt;string&gt; 中定义的std::getline 函数。请记住,这与调用 std::basic_istream 类的成员函数 getline 不同,后者对 char 数组进行操作。

这几乎可以归结为这样的事情 - 下面的代码用文件中找到的每一行填充向量:

#include <string>
#include <fstream>
#include <vector>

int main()
{
    std::ifstream myfile("/etc/passwd",std::ios::in);
    if(!myfile.good()) return 1;

    std::vector<std::string> myfile_lines;
    std::string single_line;

    while(std::getline(myfile,single_line))
    {
        myfile_lines.push_back(single_line);
    }

    myfile.close();
}

【讨论】:

  • 我正在努力弄清楚您在此处添加了哪些信息(可能除了命名声明 std::getline 的头文件之外)。您甚至不需要对文件格式进行太多假设,因为 de OP 的代码已经足够显示,如果标题和标签 csv 还不足以提示...
  • 当你发布你的答案时,我已经准备好了我的答案,所以我决定 - 到底是什么。 :)
【解决方案2】:

整个例子与问题不是很相关:如何逐行读取文件

std::string s;
while (std::getline(file, s))
{
    std::cout << s << "\n"; // each next line printed
}

注意std::getline 的使用,这比干预char[] 安全得多

while(std::getline(file, stringLineToAnalyse))
{

会在您的示例代码中找到重点

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多