reading csv file and saving elements as int16_t

Hello, i have to read a csv file and saving some elements as int16_t. The file is of this type:

1,0.982,1,1,0.9491
1,0.982,1,1,0.9491
1,0.982,1,1,0.9491
1,0.982,1,1,0.9491

I have to read the file and save the numbers of the first line in differents int16_t elements.
closed account (o9hRoG1T)
So what's your question again?
Hello Kelkut,

Welcome to the forum.

I will also ask what is your question? Do you have any code to show where you are having a problem? Or is it you just do not know where to start.

Without seeing what variables you have defined it makes it hard to tell you how to proceed.

This may help:
http://www.catb.org/esr/faqs/smart-questions.html

Suggestions for you:

Think about the variables you will be using and any header files that would cover the type for the variable.

Next I would work on opening the file and making sure it is open. If you are not use to dealing with file streams this code may be useful to you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string iFileName{ "" };  // <- Put file name here. Or you can use a variable.

std::ifstream inFile;

inFile.open(iFileName);

if (inFile.is_open())
{
	std::cout << "\n File " << iFileName << " is open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
}
else
{
	std::cout << "\n File " << iFileName << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
	exit(1);
}


As a beginner I found this useful. As you gain more this code can be shortened. When you figure out how to do that you will have learned something.

Hope that helps,

Andy
Sorry for the incomplete question. I honestly don't know where to start, i tried to use ifstream but when i read the csv file i'm not able to save the variuous numbers as int16_t but only as strings. I'm a complete beginner but i have to do this for an exam but nobody gave me some help so i'm confused aboute the code.
closed account (o9hRoG1T)
Post your current code. We start with that.
Hello Kelkut,

This may be a little beyond what you know, but I read an entire line ad put it in a string stream, extracted from the string stream into the variables using "std::stoi" and "std::stod".

Once I dealt with one line I realized you would either need a 2D array or a 2D vector since there is more than one line.

As outw said post what you have done and we can go from there.

Andy
I have not a real code, i'm making some attemps using this type of code. In every row of my csv i have 9 numbers, every number is a value i have to store as int16_t. If i try with the code below i can store the values as string and see them on the screen but it's not what i'm trying to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>

using namespace std;

int main(){

  ifstream ip("data.csv");

  if(!ip.is_open()) std::cout << "ERROR: File Open" << '\n';

  string bus1;
  string bus2;
  string bus3;
  string bus4;
 
  getline(ip,bus1,',');
  getline(ip,bus2,',');
  getline(ip,bus3,',');
  getline(ip,bus4,'\n');

ip.close();
}
closed account (o9hRoG1T)
oh man, i can't seem to find the quote button in this forum :D

@Handy Andy: Never ever read a file into a string, put that into a stringstream, read from the stringstream into another variable. Just think about how dumb this actually is: stream --> string --> stream --> some_variable
stream is redundant. Why would you produce a second stream if you already got a stream. This suggestion comes up quite often, since people don't know that getline is dumb (Kelkut realized it) and does only unformatted input.

But you have a formatted file so use formatted input (operator>>) and it does what you want, namely:
stream --> some_variable

So: if you want an int, operator>> gives you an int. If there is no int in the stream, you get an error, hence there is something wrong with the format. AWESOME. Look:
1
2
3
4
5
6
7
8
9
int col_1;
double col_2;
int col_3;
int col_4;
double col_5;
char sep = ',';
	
ip >> col_1 >> sep >> col_2 >> sep >> col_3 >> sep >> col_4 >> sep >> col_5;
cout << col_1 << sep << col_2 << sep << col_3 << sep << col_4 << sep << col_5;

Of course that can be improved, but thats the basis.
closed account (o9hRoG1T)
@lastchance
whether there needs to be a comma is up to the file owner. And the file owner sets the format and he set it to csv with comma. so yes, i do know the format.
For everything i dont know i'd use getline too but keep that in the string and dont put it into a stringstream, because i dont know what to do with it.
Sorry, @outw, I've done it again and deleted a post because I wasn't happy with it. My apologies!

I agree, if you know the precise format and can guarantee the number of columns then your method bypasses all the redundant stream operations.

However, if you want a routine that can work for arbitrary numbers of columns, allows for blanks, allows for strings with spaces then first isolate a whole line as a string, then stringstream it to, e.g., arbitrary-size vectors etc. Sadly, my experience at work is that whenever there's a spreadsheet of numeric data some well-meaning, but not terribly computer-savvy, data enterer will always insist on putting a comments column at the end. Plays havoc with the data file.

There's a place for both approaches. The problem here is that the OP hasn't really given enough information to go on (yet). At the moment, all the ints in his/her file seem to be 1.
Last edited on
Hello Kelkut,

That is a start.

At the top you are missing header file "string" very important as everything from line 12 on needs it to work.

Line 4 is best not to use as it WILL get you in trouble some day. See this for some reading:
https://www.geeksforgeeks.org/using-namespace-std-considered-bad-practice/

I did have a more involved way of reading and dealing with the file, but outw's last two lines is the simplest method. I just do not use it as often as I should, so I do not think about it first.

Something to consider now is that the file has 4 lines of five numbers. Do you just need to read this when needed or would storing what has been read be useful. I played with a 2D vector, but a 2D array will work.

Lines 8 an 10 are good. Although after I put a comment on line 4 I had to put "std::" in front of "ifstream" and also "string" and "getline" as these are all in the standard name space.

It is best to learn how to qualify what is in the standard name space now a little at a time instead of all at once.

Your code is workable, but it does not read all the numbers in a line and it only reads and deals with one line of the file. If you need to read the whole file a while loop is a good choice for reading a file of unknown length. Also in the while condition do not use "eof" to test for end of file as it will not work the way you think it will. You are most likely to process the last read twice.

Should you need to read the file at different times I would put the read in a function and pass the input stream and variables, all by reference, so you do not have to repeat the code several times.

Hope that helps,

Andy
@outw,

I think you may have misunderstood me I was not trying to read the entire file only one line at a time. My first attempt did not work the way I wanted. When I tried to read the last number I also read the entire next line. That is when the string stream came to mind.

The last two lines of your code reminded me that there is another way, I just do not use it that often to think of it first.

Andy
Thank you all.
Topic archived. No new replies allowed.