Home > Back-end >  Why does getline() cut off CSV Input?
Why does getline() cut off CSV Input?

Time:01-04

im trying to read and parse my CSV files in C and ran into an error.

The CSV has 1-1000 rows and always 8 coloumns.

Generally what i would like to do is read the csv and output only lines that match a filter criterium. For example Coloumn 2 is timestamp and only in a specific time range.

My problem is that my programm cuts off some lines. At the point where the data is in the string record variable its not cutoff. As soon as i push it into the map of int/vector its cutoff. Am I doing something wrong here?

Could someone help me indentify what the problem truly is or maybe even give me a better way to do this?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <map>
#include "csv.h"

using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream; using std::ostringstream;
using std::istringstream;

string readFileIntoString(const string& path) {
    auto ss = ostringstream{};
    ifstream input_file(path);
    if (!input_file.is_open()) {
        cerr << "Could not open the file - '"
            << path << "'" << endl;
        exit(EXIT_FAILURE);
    }
    ss << input_file.rdbuf();
    return ss.str();
}

int main()
{

    int filterID = 3;
    int filterIDIndex = filterID;
    string filter = "System";

    /*Filter ID's:
        0 Record ID
        1 TimeStamp
        2 UTC
        3 UserID
        4 ObjectID
        5 Description
        6 Comment
        7 Checksum
    */
 
    string filename("C:/Storage Card SD/Audit.csv");
    string file_contents;
    std::map<int, std::vector<string>> csv_contents;
    char delimiter = ',';

    file_contents = readFileIntoString(filename);

    istringstream sstream(file_contents);
    std::vector<string> items;
    string record;

    int counter = 0;
    while (std::getline(sstream, record)) {
        istringstream line(record);
        while (std::getline(line, record, delimiter)) {
            items.push_back(record);
            cout << record << endl;
        }
        
        csv_contents[counter] = items;
        //cout << csv_contents[counter][0] << endl;
        items.clear();
        counter  = 1;
    }

CodePudding user response:

I can't see a reason why you data is being cropped, but I have refactored you code slightly and using this it might be easier for you to debug the problem, if it doesn't just disappear on its own.

int main()
{
    string path("D:/Audit.csv");

    ifstream input_file(path);
    if (!input_file.is_open()) 
    {
        cerr << "Could not open the file - '" << path << "'" << endl;
        exit(EXIT_FAILURE);
    }
    std::map<int, std::vector<string>> csv_contents;
    std::vector<string> items;
    string record;
    char delimiter = ';';

    int counter = 0;
    while (std::getline(input_file, record))
    {
        istringstream line(record);
        while (std::getline(line, record, delimiter))
        {
            items.push_back(record);
            cout << record << endl;
        }

        csv_contents[counter] = items;
        items.clear();
          counter;
    }
    return counter;
}

I have tried your code and (after fixing the delimiter) had no problems, but I only had three lines of data, so if it is a memory issue it would have been unlikely to show.

  •  Tags:  
  • Related