2

I am trying to read a database file (as txt) where I want to skip empty lines and skip the column header line within the file and store each record as an array. I would like to take stop_id and find the stop_name appropriately. i.e.

If i say give me stop 17, the program will get "Jackson & Kolmar".

The file format is as follows:

17,17,"Jackson & Kolmar","Jackson & Kolmar, Eastbound, Southeast  Corner",41.87685748,-87.73934698,0,,1
18,18,"Jackson & Kilbourn","Jackson & Kilbourn, Eastbound, Southeast Corner",41.87688572,-87.73761421,0,,1
19,19,"Jackson & Kostner","Jackson & Kostner, Eastbound, Southeast Corner",41.87691497,-87.73515882,0,,1

So far I am able to get the stop_id values but now I want to get the stop name values and am fairly new to c++ string manipulation

mycode.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string filename;
    filename = "test.txt";
    string data;

    ifstream infile(filename.c_str());

    while(!infile.eof())
    {
        getline(infile,line);
        int comma = line.find(",");
        data = line.substr(0,comma);
        cout << "Line " << count << " "<< "is "<< data << endl;
        count++;
    }
    infile.close();
    string sent = "i,am,the,champion";
 return 0;

}
koala421
  • 705
  • 2
  • 9
  • 24
  • https://stackoverflow.com/a/5605159/8491726 and you can give getline the delimiter - so it will read till comma and not the whole line – Artemy Vysotsky Sep 17 '17 at 19:47

2 Answers2

1

You can use string::find 3 times to search for the third occurrence of the comma, and you must store the positions of the last 2 occurrences found in line, then use them as input data with string::substr and get the searched text:

std::string line ("17,17,\"Jackson & Kolmar\",\"Jackson & Kolmar, Eastbound, Southeast  Corner\",41.87685748,-87.73934698,0,,1");
std::size_t found=0, foundBack; 
int i;
for(i=0;i<3 && found!=std::string::npos;i++){   
    foundBack = found;
    found=line.find(",",found+1);
}

std::cout << line.substr(foundBack+1,found-foundBack-1) << std::endl;
yacsha
  • 71
  • 4
0

You can read the whole line of the file intoa string and then use stringstream to give you each piece one at a time up until and exluding the commas. Then you can fill up your arrays. I am assuming that you wanted each line in it's own array and that you wanted unlimited arrays. The best way to do that is to have an array of arrays.

std::string Line;
std::array<std::array<string>> Data;
while (std::getline(infile, Line))
{
    std::stringstream ss;
    ss << Line;
    Data.push_back(std::vector<std::string>);
    std::string Temp;
    while (std::getline(ss, Temp, ','))
    {
        Data[Data.size() - 1].push_back(Temp);
    }
}

This way you will have a vector, full of vectors, each of which conatining strings of all your data in that line. To access the strings as numbers, you can use std::stoi(std::string) which converts a string to an integer.

  • Is there a way of not using vector...I simply want to get the "Jackson & Kolmar" (stop_id) part of the string and store that into an array – koala421 Sep 17 '17 at 22:10
  • Simply use the getline method to skip the first two parts and collect what is left into your vector. Sorry I misunderstood your question :) – The Great Sauron Sep 17 '17 at 22:34