Reading Integers with defined characters C++

I'm trying to parse a file that has has integers and strings in a CSV file. There in columns and rows and would like to read a integer and string in "" based on a character i and s.

This is what the table looks like:

SessionAbortCount i=2282 UInt32
SessionTimeoutCount i=2281 UInt32
OUT.CV ns=2;s=0:SIGGEN/SG1/OUT.CV Float
OUT.CV ns=2;s=0:SIGGEN/SG10/OUT.CV Float
OUT.CV ns=2;s=0:SIGGEN/SG100/OUT.CV Float


I need advice and help on how to parse the value i to an integer and when it reads a s puts in "". Currently I can read the whole file, but it reads all as a string. when I would like it to see i it will be a integer and if its a s its a string but it puts it as "".

Here my code so far.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/
/ PARSINGTHEFILE. CPP MAIN 

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "CsvReader.h"
#include "Csvline.h"
#include <cstdio>
using namespace std;


int main(int argc, char* argv) {

    CsvReader reader;
    vector<Csvline> lines = reader.read("C:\\Users\\E1416162\\Documents\\PE-Lab UAServer NodeIDs.csv", true);
    int i = 0;

    for (auto& line : lines) {

        cout << "Display Name: " << lines[i].values[0].c_str() << endl;
            cout << "     Node ID: " << lines[i].values[1].c_str() << endl;
            cout << "   Data Type: " << lines[i].values[2].c_str() << endl;
        cout << endl;
        i++;
         if (i == 20)
            break;

    return 0;
}

// Csvline CPP MAIN 
#include "Csvline.h"
using namespace std;

void Csvline::parse(std::string line, char delimiter) {

    std::stringstream inLine(line);

    std::string temporaryColumn = "";
    while (getline(inLine, temporaryColumn, delimiter)) {
        values.push_back(temporaryColumn);
    }

}
// get a column value
string Csvline::getString(int column) {

    return values[column];

}
double Csvline::getDouble(int column) {
    return atof(values[column].c_str());
}
int Csvline::getInt(int column) {
    return atof(values[column].c_str());
}
float Csvline::getFloat(int column) {
    return atof(values[column].c_str());
}






// CsvReader CPP 
#include "CsvReader.h"

vector<Csvline> CsvReader::read(string fileName, bool hasHeader) {
    ifstream inputFile;
    vector<Csvline> lines;
    inputFile.open(fileName.c_str());

    string line = "";

    if (hasHeader)
        getline(inputFile, line);


    while (getline(inputFile, line)) {

        Csvline csvLine;
        csvLine.parse(line);
        lines.push_back(csvLine);



    }
    int i = 0;
    printf("Line %d, value_0 = %s, value_1 = %s, value_2 = %s ", i, lines[i].values[i].c_str(), lines[i].values[i + 1].c_str(), lines[i].values[i + 2].c_str());

    return lines;
}




// CsvReader HEADER

#pragma once
#ifndef CSVREADER_H
#define CSVREADER_H
#include <vector>
#include <fstream>
#include <iostream>

#include "Csvline.h"

using namespace std;



// This is to read a file 
class CsvReader
{
public:
        CsvReader() {}
        vector<Csvline>read(string fileName, bool hasHeader);
};

#endif




// Csvline HEADER 

#pragma once
//defining class
#ifndef CSVLINE_H
#define CSVLINE_H

#include <string>
#include <sstream>
#include <vector>

using namespace std;
// 
class Csvline
{

public:

    vector<string>values;


public:
        //constructer
        Csvline(){}
        Csvline(const Csvline& other) {
            values = other.values;
        }
        
        Csvline operator=(const Csvline& other) {

            values = other.values;
        }


        ~Csvline(){}
    //get a line and return nothing 
    void parse(std::string lines, char delimiter = ',');
    // get a column value
    string getString(int column);
    double getDouble(int column);
    int getInt(int column);
    float getFloat(int column);
};

#endif 

Last edited on
you don't need to do any manual string to int work nor add quotes.

read the data as text. find the i= as a token, and place that in the largest type c++ supports (eg uint64_t) (that way you can ignore its type from excel and just use a type that can handle anything, less complicated) when you find the i=, you can split out the substring from there up to the next token (space? where are your commas in that csv??!) and call stoi (string to int) or use a stringstream or whatnot to shovel it into the target integer.

for strings its less intense, you only need the token s= and then grab to the next delimiter as a substring and its done.

when you output, you can stream out the int and it generates text naturally (cout << x, right, x is an int type and its all good) but if you need to add quotes to an output, manually insert the \" symbol into the string (s+= '\"'; s+= yoursubstring; s+= '\"'; )
Last edited on
I'm not sure what you want when you get an s? This code will display the chars from the following : to the terminating space. If this isn't what you want, given the input data then would you provide what the output should be please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>

int main() {
	if (std::ifstream ifs { "nodes.txt" })
		for (std::string line; std::getline(ifs, line); ) {
			if (auto fndi { line.find("i=") }; fndi != std::string::npos)
				std::cout << std::strtoull(line.data() + fndi + 2, nullptr, 10) << '\n';

			if (auto fnds { line.find("s=") }; fnds != std::string::npos)
				if (auto fndc { line.find(':') }; fndc != std::string::npos)
					std::cout << line.substr(fndc + 1, line.find(' ', fndc + 1) - fndc - 2) << '\n';
		}
	else
		return (std::cout << "Issue opening file\n"), 1;
}



2282
2281
SIGGEN/SG1/OUT.C
SIGGEN/SG10/OUT.C
SIGGEN/SG100/OUT.C


Obviously if this is what is wanted, then instead of just displaying the data, it can be used/saved etc etc.
Last edited on
Topic archived. No new replies allowed.