Debugging C++ Code to Read CSV File

  • Context: C/C++ 
  • Thread starter Thread starter khotsofalang
  • Start date Start date
  • Tags Tags
    C++ Csv File Reading
Click For Summary
SUMMARY

The discussion focuses on debugging C++ code for reading a CSV file. The user initially attempted to use the getline function with a comma as a delimiter, which resulted in incorrect output due to the absence of a comma after the last number in each line. The solution involved switching to a default newline delimiter and utilizing stringstream for parsing the lines effectively. This approach resolved the issue, allowing for the expected output format.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with file I/O operations in C++
  • Knowledge of string manipulation techniques in C++
  • Experience with the stringstream class in C++
NEXT STEPS
  • Learn about C++ file handling with fstream
  • Explore the stringstream class for efficient string parsing
  • Research CSV file parsing libraries in C++
  • Study error handling techniques in C++ file I/O
USEFUL FOR

C++ developers, software engineers working with data processing, and anyone involved in reading and parsing CSV files in their applications.

khotsofalang
Messages
21
Reaction score
0
I am trying to read from a csv file, the file looks like below:
1,2,3
4,5,6
7,8,9
0


I used getline(Input_file,size,',') but the problem with this is that it seems to see the numbers not separated by commas as one string., for the code below i was expecting the following output:

1
debug
2
debug
3
debug
4
debug
5
debug
6
debug
7
debug
8
debug
9
debug
0
debug

I tried to modify the program to look for and eof but it couldn't work...

my code looks like below:

if(input_file.good()){

while(!input_file.eof()){

input_file.getline(number,256,',');//number is an array of characters... i.e char number[10]


count<<number<<endl;
count<<"debug:\n";}}/*note that after the debug word only one number is supposed to be printed but instead I get:
1
debug
2
debug
3 -----> expecting a debug word between 3 and 4
4
debug
5
debug
6 -----> something wrong here, expecting a debug word between 6,7
7
debug
8
debug
9 --->clearly something wrong expecting a word between 9,0
0
debug
*/

I can't figure out what i am doing wrong, can anyone help me debug my code and a better way of doing what i am trying to do.
 
Last edited:
Technology news on Phys.org
Your getline() parameter for the terminator is a ',' but the last number on each input line is not followed by a ','. You could use input_file.getline(buffer, sizeof(buffer)) ... which defaults to newline '\n' as the terminator, but then you'd have to parse the lines, probably using sscanf().
 
thanx i solved it using a simple stringstream conversion...I guess i was sleepy last night when i was doing this part :D
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
22K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K