ifstream and csv

I have a text file thats separated by colons for example

c:toyota:2006:corolla:56000 (type(car or truck):make:year:model:mileage)

I am running into errors on how to separate each and store them into respective variables

char type;
char *make;
char model; //Have to put model into an int*, so I have been typecasting into an int
char *model;
int mileage;

I am running into a problem where its reading the colons into the MAKE variable since its defaulted to char *make = new char[100]; since the make of the vehicles vary.

i have tried

ifstream fin;

fin >> type >> COLON >> make etc...

but just crashes the program as im assuming its just storing the whole into the make pointer.

Any help would be great, thanks!


Also, I can read it all into a temp char pointer but then how would you be able to parse into the variables?
You could use c-strings and strtok() to split the string at each colon.
Or use a stringstream. The following code assumes you have used getline() to read a single line from the file into a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

    using namespace std;

int main()
{
    string line = "c:toyota:2006:corolla:56000";
    istringstream ss(line);

    const char colon = ':';
    string word;
    while (getline(ss,word, colon))
    {
        cout << word << endl;
    }


    // Alternative version
    ss.clear(); // reset flags.
    ss.str(line);


    char type;
    char make[20];
    char model[20];
    int year;
    int mileage;

    getline(ss,word, colon);
    type = word[0];
    getline(ss,word, colon);
    strcpy(make, word.c_str());
    getline(ss,word, colon);
    year = atoi(word.c_str());
    getline(ss,word, colon);
    strcpy(model, word.c_str());
    getline(ss,word, colon);
    mileage = atoi(word.c_str());

    cout << "Type: "      << type
         << "  Make: "    << make
         << "  Model: "   << model
         << "  Year: "    << year
         << "  Mileage: " << mileage
         << endl;


    return 0;

}
Last edited on
Thanks I havent utilized getline or strtok but Ill give it a try!

Thanks!
So I am trying to store year and mileage into an integer pointer by doing a loop but realized that i am using a string variable so its not working correctly. Any tips ?
does atoi store parse the integers into a character array?

i am trying to do the following

char model[4];
int *modelPtr;

model = atoi(word.c_str());
for(int i =0; i < 4; i++)
modelPtr[i] = (int)model[i];

atoi() takes a character string and returns an integer - assuming the string contains a valid representation of an int, such as "-1" or "+23" or "7654".
http://www.cplusplus.com/reference/cstdlib/

I'm having difficulty understanding what you need to do with model. I thought that was a string, such as "corolla". As such, it cannot be used with atoi() because it is ordinary text, not a numeric value.

In that context, the code you posted above doesn't seem to make sense, I wouldn't have any idea of how to fix it.

Perhaps you could describe what it is you need to do, or give the relevant part of the specifications for this project.


Last edited on
Topic archived. No new replies allowed.