I'm doing the following block of code to simply read a .csv file of 11 columns and 10 rows. The aim is to perform an analysis on the data in each row, so I wanted to separate each data point and assign their values to variables in loops. This is the code:
By calling v[0] through v[10], you are making the assumption that the vector will ALWAYS successfully push_back 11 elements into it, without any room for error.
When debugging, check the size of your vector v before line 32. What does it say the size of the vector (alternatively, if dealing with STL containers while debugging is too annoying, just print the v.size() here and see).
If you showed an example of your csv file, we could more than likely tell you where the issue is.
For example, what if your file ends with a newline (for example, as is "proper" in a *nix philosophy)? Your call to getline will succeed on line 18, but your line might be empty. What happens then?
Thanks for this. I checked the v.size() bit and noticed that even though there are only 10 rows of data, the size was showing for 11 rows. For context, I have now removed one column from my raw data set, making it 10 columns. So the size should come 10 and should show 10 times. Instead, I am getting an 11th extra instance where the size is 1. I don't understand why this is happening.
Post your exact csv file contents. Preferably within [output] and [/output] tags. If it's CSV, I would expect there be commas. And tell us whether or not the last line is empty (i.e. whether or not the file ends with a newline).
For the looping, if I use your code, I will have to declare line and temp outside the loops first. Currently they are being declared inside the loop. Should I move the variables out of their respective loops then?
Yes, but your bigger issue is that you only have 10 columns, yet you are trying to extract 11 distinct things from each line. This doesn't make sense.
And for the extra line at the end, you should check if v.size() < 11, and if so, then break out of your loop, because it wasn't able to extract all the data it needed.
I mentioned that I had removed a column. So I removed the variable empcode (which was the first column previously) and shifted all other v[] to 0 to 9. That makes 10 columns.
Also, I just made my loop conditions based on your input, and now the output for v.size() is:
5
5
5
5
5
So its only reading 5 rows and 5 columns. Not sure why this is occurring.
From your original code, and your original CSV file, change:
1 2 3 4 5 6
while (ss.good())
{
string temp;
getline(ss, temp, ',');
v.push_back(temp);
}
to
1 2 3 4 5 6 7 8 9 10 11 12
while (ss.good())
{
string temp;
getline(ss, temp, ',');
v.push_back(temp);
}
if (v.size() < 11)
{
// this line did not contain enough data to use; possibly end of file
continue;
}
I'm constantly getting the following errors when debugging:
invalid_argument at memory location 0x00FEF528
The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
Also, when running this, the first basic_c value is ALWAYS coming to 0 even though its actually 600000. I'm beyond stumped at this point since every fix seems to be creating fresh errors in different places.
EDIT: I'm dumb. The issue was popping up because of the file type of my .csv. I changed it from CSV UTF-8 to just CSV and it seems to be working fine. Thanks so much guys for putting up with my code!