Debugging C++ Code to Read CSV File

In summary, the conversation discusses the issue of reading from a csv file and using getline() to separate numbers by commas. However, the last number on each line is not followed by a comma and the program is not outputting the expected results. Various solutions are suggested, including using a stringstream conversion and parsing the lines with sscanf(). The issue is ultimately resolved and it is realized that the issue was due to a simple mistake.
  • #1
khotsofalang
21
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]


cout<<number<<endl;
cout<<"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
  • #2
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().
 
  • #3
thanx i solved it using a simple stringstream conversion...I guess i was sleepy last night when i was doing this part :D
 

Related to Debugging C++ Code to Read CSV File

1. How do I debug my C++ code to read a CSV file?

To debug your C++ code to read a CSV file, you can use a debugger tool such as GDB or Visual Studio Debugger. These tools allow you to step through your code line by line and examine the values of variables to identify any errors.

2. Why is my C++ code not reading the CSV file correctly?

There could be several reasons for this. Some common issues include incorrect file path, incorrect delimiter used, or incorrect data types used for variables. Using a debugger tool can help you identify and fix these errors.

3. How can I check if my CSV file is being read correctly?

You can use the debugging tool to examine the values of the variables after reading the CSV file. You can also print out the values of the variables to the console or write them to a separate file to check if they are being read correctly.

4. What is the best approach for debugging C++ code to read a CSV file?

The best approach is to use a debugger tool and go through your code step by step, checking the values of variables and identifying any errors. It is also helpful to break down your code into smaller functions and test each one separately to narrow down the source of the error.

5. How can I prevent errors when debugging my C++ code to read a CSV file?

To prevent errors, it is important to properly handle exceptions and validate user input. You can also use libraries or built-in functions that are specifically designed for reading and parsing CSV files, which can help simplify the process and minimize the chances of errors.

Similar threads

  • Programming and Computer Science
Replies
30
Views
3K
  • Programming and Computer Science
Replies
2
Views
799
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
14
Views
5K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
427
  • Programming and Computer Science
Replies
6
Views
9K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top