C/C++ Debugging C++ Code to Read CSV File

Click For Summary
The discussion centers on reading data from a CSV file using C++. The user initially encounters issues with the getline function, which is set to use a comma as a delimiter. This causes the program to treat numbers not followed by a comma as a single string, resulting in unexpected output. The user expected a "debug" message to appear after each number but instead saw multiple numbers printed together without the expected separation. Suggestions include using getline with the default newline delimiter and then parsing the line with functions like sscanf. Ultimately, the user resolves the issue by utilizing a stringstream conversion, indicating that fatigue may have contributed to the initial coding errors.
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]


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
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
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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