1

Here is a code I made to get from a .csv file, It get itch witch are in beetween to separator and give positon of those separator. But this does make it only for the first line how to continue on the second one for exemple ??

#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;
}
sehe
  • 374,641
  • 47
  • 450
  • 633

2 Answers2

4

The whole example is not very relevant to the question: how to read a file line-by-line

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

Note the use of std::getline which is MUCH safer than meddling with char[]

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

would look to the point in your sample code

sehe
  • 374,641
  • 47
  • 450
  • 633
0

Assuming that the file you're trying to read is more-or-less properly formatted, you most likely want to use the std::getline function, defined in <string>. Keep in mind that this is not the same as calling the member function getline of std::basic_istream classes, which operates on char arrays.

It all pretty much boils down to something like this - the code below fills the vector with every line found in the file :

#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();
}
Daniel Kamil Kozar
  • 18,476
  • 5
  • 50
  • 64
  • I'm trying really hard to figure out what information you added here (besides naming the header file declaring `std::getline`, perhaps). You didn't even need much assumption about the file format, as de OP's code shows enough, if the title and tag [tag:csv] weren't enough of a hint already... – sehe Oct 31 '12 at 16:32
  • I had my answer ready when you posted yours, so I just decided - what the hell. :) – Daniel Kamil Kozar Oct 31 '12 at 16:53