Problem with getline in C++ programming

Click For Summary
SUMMARY

The forum discussion centers on a C++ programming issue where the user encounters a problem with the getline function, which appears to discard the last cell of data when reading from a CSV file. The user provided a code snippet that uses getline to read strings separated by commas, but the last value is not captured. The problem arises from the way the end-of-file (EOF) is handled in conjunction with getline. A solution involves ensuring that the last line is processed correctly, potentially by checking for EOF after the loop or using alternative methods to read the last value.

PREREQUISITES
  • Understanding of C++ file I/O operations
  • Familiarity with the getline function in C++
  • Basic knowledge of string manipulation and conversion in C++
  • Experience with CSV file formats and parsing
NEXT STEPS
  • Research proper handling of end-of-file conditions in C++ file streams
  • Learn about alternative methods for reading CSV files in C++, such as using std::ifstream with a loop
  • Explore the use of std::getline with different delimiters and its implications
  • Investigate the use of libraries like Boost or RapidCSV for efficient CSV parsing in C++
USEFUL FOR

C++ programmers, software developers working with file I/O, and anyone involved in data parsing from CSV files.

khotsofalang
Messages
21
Reaction score
0

Homework Statement



I am solving a problem in which I am reading numbers from cells in a csv file, I used getline to get strings from the csv file then converted the string to an integer... but the getline seemingly discards the last cell in the csv file


Homework Equations



this my C++ code:
if (input_file.good()){
array= new int;
while(!input_file.eof()){getline(input_file,number,',');//input filename is istream
//problem seems to be with the getline discarding the last cell
if(input_file.good()){

//otherwise everything is okay except i nid that number in last cell

count<<number<<endl;
stringstream convert(number);
convert>>num;
array=num;
// count<<num;
i++;}}}

The Attempt at a Solution



I attempted the solution as above, please let me know if there is an effecient way to get all data from the file
 
Physics news on Phys.org
I am not quite sure why this is happening (I mostly use C, almost never getline) but have you tried getc?
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 23 ·
Replies
23
Views
9K