How to read a comma delimited text file into arrays [duplicate] - c++

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
How to use stringstream to separate comma separated strings [duplicate]
(2 answers)
Closed 8 months ago.
I have been working on this project and been reading in text files with ONLY spaces in between and not commas so I need to update how I read in the file into arrays. I have tried using stringstream() and getline() but no luck. Does anyone know how I can read in this file into arrays?
This is how I been doing it before
void readData(ifstream& inputFile, double lat[], double lon[], double yaw[], int& numLines)
{
// Read in headers.
string header;
getline(inputFile, header);
// Read in data and store in arrays.
for (int i = 0; i < numLines; i++)
{
if (inputFile >> lat[i])
{
inputFile >> lon[i];
inputFile >> yaw[i];
}
}
but Im not sure how to modify it to read in this type of file into arrays and also the only ones I'm interested in are
latitude
longitude
altitude(feet)
speed(mph)
gps
power
pitch
roll
yaw
motor on
834,,,,,,,,,,,,,,,,
latitude,longitude,altitude(feet),ascent(feet),speed(mph),distance(feet),max_altitude(feet),max_ascent(feet),max_speed(mph),max_distance(feet),time(millisecond),gps,power,pitch,roll,yaw,motor on
43.5803481,-116.7406331,0,0,0,0,0,0,0,0,539,10,97,178,180,141,0
43.5803481,-116.7406329,0,0,0,0,0,0,0,0,841,10,97,178,180,141,0
43.5803482,-116.7406328,0,0,0,0,0,0,0,0,1125,10,97,178,180,141,0
43.5803481,-116.7406329,-1,0,0,0,0,0,0,0,1420,10,97,178,180,141,0
43.580348,-116.7406328,0,0,0,0,0,0,0,0,1720,10,97,178,180,140,0
43.5803479,-116.7406326,-1,0,0,0,0,0,0,0,2023,10,97,178,180,140,0
43.5803478,-116.7406326,0,0,0,0,0,0,0,0,2344,10,97,178,180,140,0
43.5803476,-116.7406329,-1,0,0,0,0,0,0,0,2620,10,97,178,180,140,0
43.5803475,-116.7406329,-1,0,0,0,0,0,0,0,2922,10,97,178,180,140,0
43.5803473,-116.7406329,0,0,0,0,0,0,0,0,3221,10,97,178,180,140,0
If someone can point in the right direction that would be great. Thanks

You need to extract those commas, to the variable of type char:
std::ifstream f("d:\\temp\\z.txt");
string s;
double lat, lon, alt, asc, speed, dist, max;
vector<double> lat_vec, lon_vec, asc_vec, speed_vec, dist_vec, max_vec;
char c;
while (!f.eof()) {
f>>s; // get one line
stringstream st(s);
// below, eat first number, then comma, then second number, etc.
if (st>>lat>>c>>lon>>c>>alt>>c>>asc>>c>>speed>>c>>dist>>c>>max) {
lat_vec.push_back(lat); lon_vec.push_back(lon);
asc_vec.push_back(asc); speed_vec.push_back(speed);
dist_vec.push_back(dist); max_vec.push_back(max);
}
}
// if you really need arrays, not vectors
double *dist_ar = new double[dist_vec.size];
for (int i=0;i<dist_vec.size(); i++)
dist_ar[i]=dist_vec[i];
return 0;

Related

c++ getline putting different parts of a line into variables [duplicate]

This question already has answers here:
How can I read and parse CSV files in C++?
(39 answers)
Closed 5 years ago.
For the life of me, I can't seem to figure out how to do this properly.
First, I read a line from a csv file. Lets say, that line has
2992854,BOB,3452,394832
I don't want to read each result into a console like practically every example I've found, I want them to go in order, into these 4 variables:
int time;
string name;
int location;
int point;
Right now, this is my code:
string line;
ifstream inputFile("input.csv");
std::list<Cramista> Cramistas;
while (!inputFile.eof())
{
int time;
string name;
int location;
int point;
std::vector<std::string> stringArray;
std::size_t position = 0, found;
getline(inputFile, line);
while ((found = line.find_first_of(',', position)) != std::string::npos)
{
stringArray.push_back(line.substr(position, found - position));
position = found + 1;
}
time = stoi(stringArray[0]);
name = stringArray[1];
location = stoi(stringArray[2]);
point = stoi(stringArray[3]);
}
UPDATED:
So, with what I have here, I'm able to get the first 3 out of 4 pieces of the line, and put them into an array, which I can then move into variables. Trying to figure out how to get that 4th part.
I've got 2992854, BOB, and 3452 but I don't have 394832.
I mean basically in order to avoid "reading each result into the console" using
cin >> time >> name >> location >> point;
you would have to split the line by commas (assuming it's a string, then convert the non string data to integers.

How does one correctly store data into an array struct with stringstream? [duplicate]

This question already has answers here:
Why does reading a record struct fields from std::istream fail, and how can I fix it?
(9 answers)
Closed 6 years ago.
I was wondering how to store data from a CSV file into a structured array. I realize I need to use getline and such and so far I have come up with this code:
This is my struct:
struct csvData //creating a structure
{
string username; //creating a vector of strings called username
float gpa; //creating a vector of floats called gpa
int age; //creating a vector of ints called age
};
This is my data reader and the part that stores the data:
csvData arrayData[10];
string data;
ifstream infile; //creating object with ifstream
infile.open("datafile.csv"); //opening file
if (infile.is_open()) //error check
int i=0;
while(getline(infile, data));
{
stringstream ss(data);
ss >> arrayData[i].username;
ss >> arrayData[i].gpa;
ss >> arrayData[i].age;
i++;
}
Further, this is how I was attempting to print out the information:
for (int z = 0; z<10; z++)
{
cout<<arrayData[z].username<<arrayData[z].gpa<<arrayData[z].age<<endl;
}
However, when running this command, I get a cout of what seem to be random numbers:
1.83751e-0383 03 4.2039e-0453 1.8368e-0383 07011688
I assume this has to be the array running not storing the variables correctly and thus I am reading out random memory slots, however, I am unsure.
Lastly, here is the CSV file I am attempting to read.
username,gpa,age
Steven,3.2,20
Will,3.4,19
Ryan,3.6,19
Tom,3,19
There's nothing in your parsing code that actually attempts to parse the single line into the individual fields:
while(getline(infile, data));
{
This correctly reads a single line from the input file into the data string.
stringstream ss(data);
ss >> arrayData[i].username;
ss >> arrayData[i].gpa;
ss >> arrayData[i].age;
You need to try to explain to your rubber duck how this is supposed to take a single line of comma-separated values, like the one you showed in your question:
Steven,3.2,20
and separate that string into the individual values, by commas. There's nothing about the >> operator that will do this. operator>> separates input using whitespaces, not commas. Your suspicions were correct, you were not parsing the input correctly.
This is a task that you have to do yourself. I am presuming that you would like, as a learning experience, or as a homework assignment, to do this yourself, manually. Well, then, do it yourself. You have the a single line in data. Use any number of tools that C++ gives you: the std::string's find() method, or std::find() from <algorithm>, to find each comma in the data string, then extract each individual portion of the string that's between each comma. Then, you still need to convert the two numeric fields into the appropriate datatypes. And that's when you put each one of them into a std::istringstream, and use operator>> to convert them to numeric types.
But, having said all that, there's an alternative dirty trick, to solve this problem quickly. Recall that the original line in data contains
Steven,3.2,20
All you have to do is replace the commas with spaces, turning it into:
Steven 3.2 20
Replacing commas with spaces is trivial with std::replace(), or with a small loop. Then, you can stuff the result into a std::istringstream, and use operator>> to extract the individual whitespace-delimited values into the discrete variables, using the code that you've already written.
Just a small word of warning: if this was indeed your homework assignment, to write code to manually parse and extract comma-delimited values, it's not guaranteed that your instructor will give you the full grade for taking the dirty-trick approach...
UNDER CONSTRUCTION
Ton, nice try and nice complete question. Here is the answer:
1) You have a semicolon after the loop:
while(getline(infile, data));
delete it.
How did I figure that out easily? I compiled with all the warnings enabled, like this:
C02QT2UBFVH6-lm:~ gsamaras$ g++ -Wall main.cpp
main.cpp:24:33: warning: while loop has empty body [-Wempty-body]
while(getline(infile, data));
^
main.cpp:24:33: note: put the semicolon on a separate line to silence this warning
1 warning generated.
In fact, you should get that warning without -Wall as well, but get into using it, it will also make good to you! :)
2) Then, you read some elements, but not 10, so why do you print 10? Print as many as the ones you actually read, i.e. i.
When you try to print all 10 elements of your array, you print elements that are not initialized, since you didn't initialize your array of structs.
Moreover, the number of lines in datafile.csv was less than 10. So you started populating your array, but you stopped, when the file didn't have more lines. As a result, some of the elements of your array (the last 6 elements) remained uninitialized.
Printing uninitialized data, causes Undefined Behavior, that's why you see garbage values.
3) Also this:
if (infile.is_open()) //error check
could be written like this:
if (!infile.is_open())
cerr << "Error Message by Mr. Tom\n";
Putting them all together:
WILL STILL NOT WORK, BECAUSE ss >> arrayData[i].username; eats the entire input line and the next two extractions fail, as Pete Becker said, but I leave it here, so that others won't make the same attempt!!!!!!!
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct csvData //creating a structure
{
string username; //creating a vector of strings called username
float gpa; //creating a vector of floats called gpa
int age; //creating a vector of ints called age
};
int main()
{
csvData arrayData[10];
string data;
ifstream infile; //creating object with ifstream
infile.open("datafile.csv"); //opening file
if (!infile.is_open()) { cerr << "File is not opened..\n"; }
int i=0;
while(getline(infile, data))
{
stringstream ss(data);
ss >> arrayData[i].username;
ss >> arrayData[i].gpa;
ss >> arrayData[i].age;
i++;
}
for (int z = 0; z< i; z++)
{
cout<<arrayData[z].username<<arrayData[z].gpa<<arrayData[z].age<<endl;
}
return 0;
}
Output:
C02QT2UBFVH6-lm:~ gsamaras$ g++ -Wall main.cpp
C02QT2UBFVH6-lm:~ gsamaras$ ./a.out
username,gpa,age00
Steven,3.2,2000
Will,3.4,1900
Ryan,3.6,1900
Tom,3,1900
But wait a minute, so now it works, but why this:
while(getline(infile, data));
{
...
}
didn't?
Because, putting a semicolon after a loop is equivalent to this:
while()
{
;
}
because as you probably already know loops with only one line as a body do not require curly brackets.
And what happened to what I thought it was the body of the loop (i.e. the part were you use std::stringstream)?
It got executed! But only once!.
You see, a pair of curly brackets alone means something, it's an anonymous scope/block.
So this:
{
stringstream ss(data);
ss >> arrayData[i].username;
ss >> arrayData[i].gpa;
ss >> arrayData[i].age;
i++;
}
functioned on its one, without being part of the while loop, as you intended too!
Any why did it work?! Because you had declared i before the loop! ;)

Parsing with strings into different types with different delimiters

This is actually the first time I have used this website: usually I am able to find the a solution from a previous post, but this time I am to no avail. Anyways, my problem is that I am trying to parse a file that contains commas and parentheses. It also contains strings and doubles that I need to change from the string into a double so I can use the data. I made a struct to store the data.
struct Artery{
string segment; string name;
double length; double radius; double wall_thickness;
double young_modulus; double compliance;
Artery(string seg, string n, double l, double r, double w, double y, double c)
: segment(seg), name(n), length(l), radius(r), wall_thickness(w), young_modulus(y), compliance(c){}
};
The data will be in the format as follows:
(Segment, Name, Length, Radius, Wall Thickness, Young's Modulus, Compliance)
(2A, Aorta_Ascendens, 2, 1.47, .164, 4, 53.4)
(2B, Aorta_Ascendens, 2, 1.44, .161, 4, 51.0)
(3A, Arcus_Aorta, 2, 1.12, .132, 4, 29.6)
I'm really just a beginner into the C++ language (taking a college course on the subject right now actually) and we haven't covered low-level programming with pointers and such that I know is what is usually used to parse this kind of thing. Could I get some help with this? I really don't have any idea on how to get this working.
Below is what I currently have in the process of attempting to parse the file. I wanted to eventually at the end have a vector of Artery that will serve as the basis for me accessing the information.
void read_data(vector<Artery>& v, ifstream& ifs){
//reads and seperates file for arterial data (currently from the westerhoff model)
vector<string> data_string;
while (!ifs.eof){
string data_set = " ";
getline(ifs, data_set);
data_string.push_back(data_set);
}
for (int i = 0; i < data_string.size(); ++i){
string seg; string n;
double l; double r; double w; double y; double c;
istringstream iss(data_string[i]);
}
}
To recap: I need help with parsing the file with the above format and then convert that format into an Artery. I want to then compile them into a vector of Artery to later have access to them.
Thank you,
Zav
There are many ways to do this.
Assuming your filestream, ifs is already initialized and is_open (that is you've already done the checking of the stream, then you do all operations in one go (reducing your runtime by at least a factor of two compared to your version):
void read_data(std::vector<Artery>& v, std::ifstream ifs) {
std::vector<std::string> data_strings;
std::string temp;
//read the file by lines
while(std::getline(ifs, temp)) {
//store the line (included for completeness)
data_strings.push_back(temp);
//as you didn't state if your lines began and ended with parentheses
//I opted to cover removal of them:
size_t begin = temp.find("(");//as there is only 1
size_t end = temp.find(")");//as there is only 1
temp = temp.substr(begin+1, end-1);
std::istringstream iss(temp);
Artery artery;
int col = 1;//the current column
//here we're going to use the stringstream to tokenize the string by commas
//using std::getline
while (std::getline(iss, temp, ',')) {
std::istringstream is(temp);//this is another string stream initialized to the token (which may or may not be a string)
//determine which artery property to modify based on the column number
switch(col) {
case 1:
is >> artery.segment;
break;
case 2:
is >> artery.name;
break;
case 3:
is >> artery.length;
break;
case 4:
is >> artery.radius;
break;
case 5:
is >> artery.wall_thickness;
break;
case 6:
is >> artery.young_modulus;
break;
case 7:
is >> artery.compliance;
break;
default:
std::cerr << "Invalid column!\n";
}
++col;
}
//now the artery has been correctly initialized, we can store it:
v.push_back(artery);
}
}
You'll notice that this version doesn't utilize the Artery Constructor that you supplied. It could have been used, but from how you've defined Artery, it highly resembles a POD (Plain-Old-Data type); hence, I've treated it in this solution.
Also, as you I wasn't sure if parentheses were part of each constituent line, I've treated them accordingly by removing them. (Of course, if your data doesn't contain these, you can remove the code that handles that deletion, easily).
NOTE:
My use of the stringstream extraction operator (>>) doesn't check for proper assignment. But, as this is a naive example, that is something you can do for yourself.

Using multiple instances of getline in C++

I've been working on a class assignment for C++ and we're required to acquire input from a text file and assign those values to an array....one is a string, the second an int, and the third a double.
We've only been introduced to arrays and I don't know anything yet about pointers or linked lists, or any of the higher end stuff, so I feel like I'm somewhat limited in my options. I've worked all day trying to figure out a way to acquire input from the text file and assign it to the appropriate array. I've tried to use getline to read the input file and set a delimiter to separate each piece of data but I get an error when I try to use it more than once. From what I've read, this has to do with how I'm overloading the function but I'm at a loss at resolving it. Every explanation I've read about it goes beyond my current level of familiarity. Right now, I'm focused on this fragment of code:
for (int i = 0; i < EMP_NUM; i++) // Get input from text file for names.
getline(inFile, nameAr[i], '*');
for (int i = 0; i < EMP_NUM; i++) // Input for hours.
getline(inFile, hoursAr[i], '*');
for (int i=0; i < EMP_NUM; i++) // Input for hourly rate.
getline(inFile, hrateAr[i], '*');
I'm trying to use getline three times and write the data to three separate arrays, then make a series of calculations with them later and output them to another text file. The first instance of getline doesn't produce any compiler errors but the latter two do. I'm not quite sure of another solution to get the data into my arrays, so I'm at a loss. Any help would be great!
If I understand correctly you merely have three values in a file: a string, an int and a double. I assume they are delimited by whitespace.
If that is so then you don't need std::getline(). Rather, use the extraction operator:
std::ifstream file("input.txt");
std::string s;
if( ! (file >> s) ) { // a single word extracted from the file
// failure
}
int n;
// ...
1) Instead of three different iteration, use only one
2) Pass string object in getline instead of pointers
string buf;
for (int i = 0; i < EMP_NUM; i++) // Get input from text file for names.
{
getline(inFile, buf, '*');
nameAr[i] = buf;
getline(inFile, buf, '*'); //assuming delimiter is again *
hoursAr[i] = atoi(buf.c_str() ); //C way to doing it...however in c++ u have to use stringstreams....
getline(inFile, buf);
hrateAr[i] = atof(buf.c_str() );;
}
What do the compiler errors say? Are you sure that the error is caused by getline? Maybe it's not because the getline calls but because of multiple declarations of i.

Reading delimited files in C++ [duplicate]

This question already has answers here:
How can I read and parse CSV files in C++?
(39 answers)
Closed 8 years ago.
What is the best way to read in a tab delimited file in C++ and store each line as a record? I have been looking for an open source library to help with this, but have been unsuccessful so it looks like I will have to write my own.
typedef vector<vector<string> > Rows;
Rows rows;
ifstream input("filename.csv");
char const row_delim = '\n';
char const field_delim = '\t';
for (string row; getline(input, row, row_delim); ) {
rows.push_back(Rows::value_type());
istringstream ss(row);
for (string field; getline(ss, field, field_delim); ) {
rows.back().push_back(field);
}
}
This will get you started. It doesn't do any checking that each row has the same number of fields, allow for escaping field_delim, etc.
There is no problem in using iostreams - you could read each line with getline into string, and then use stringstream on that string to iterate over fields.
There are a few libraries listed in wikipedia's article CSV_application_support.