Extracting information of data in column format using C++

Click For Summary

Discussion Overview

The discussion revolves around extracting specific data from a column-formatted file using C++. Participants explore various methods for reading data, particularly focusing on how to access specific columns and reset the reading position within the file.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant inquires about using the file.getline() command to extract data from a specific column while skipping spaces.
  • Another participant suggests using the >> operator for reading data, noting it functions similarly to cin.
  • There are requests for examples of using the >> operator to access specific lines and columns in the data.
  • A participant mentions that each >> operator reads data until a blank space is encountered, making it suitable for columnar data.
  • One participant asks how to reset the reading position to the start of the file, with suggestions to close and reopen the file or use functions like rewind(), seekpos(), and seekoff().
  • A later reply proposes reading the entire file into a buffer and then parsing it into a data structure for easier access to specific items.

Areas of Agreement / Disagreement

Participants express various methods for reading data and resetting the file pointer, but there is no consensus on the best approach, as multiple techniques are discussed without resolution.

Contextual Notes

Some participants mention specific functions and methods without providing detailed examples or clarifications on their usage, leaving some assumptions and conditions unresolved.

Leonardo Machado
Messages
56
Reaction score
2
Hello friends, how are you ?

Today's question is: How do i choose the data i want to extract from a file in c++ ?

I have this data file in columns format, something like:

Code:
x      y      z
1      2      3
4      5      6
7      8      9

I know there is the command file.getline() , and this command can be manipulated to get the string the size i want, but there is a way of skip the space between the numbers and catch only the data of the second column?

And plus, there are any way to get it not in str format ? but in a float or a double format ?

Thanks for the tips :wink:
 
Technology news on Phys.org
What about the >> operator?
 
DrClaude said:
What about the >> operator?

Could you give any example of using >> ?
It works like cin, right ? But how do i command it to get information of an specific line and column ?
 
Leonardo Machado said:
Could you give any example of using >> ?
It works like cin, right ? But how do i command it to get information of an specific line and column ?
It works with cin. or any istream. Each >> will read data until it reaches a blank space, so columns are easy to take care of.

I'm sure you can find good examples online.
 
  • Like
Likes   Reactions: Leonardo Machado
DrClaude said:
It works with cin. or any istream. Each >> will read data until it reaches a blank space, so columns are easy to take care of.

I'm sure you can find good examples online.

Got it, thanks mate, it worked for me.

Just for finishing, how do i reset the command ? For start to read the first line again ?
 
Leonardo Machado said:
Just for finishing, how do i reset the command ? For start to read the first line again ?
Close the file and open it again. The position-in-file pointer will now be at the start of the file.

The C standard library (as opposed to the C++ Standard Library) has a rewind() function. With this function you don't have to close and then reopen the file.

The C++ basic_filebuf class has a seekpos() function. You can use it to move the position-in-file pointer to a selected location in the file -- to the beginning or to the end, or to some point within the file. There is also a seekoff() function that can be used for a similar purpose.
 
Last edited:
  • Like
Likes   Reactions: QuantumQuest and Leonardo Machado
Mark44 said:
Close the file and open it again. The position-in-file pointer will now be at the start of the file.

The C standard library (as opposed to the C++ Standard Library) has a rewind() function. With this function you don't have to close and then reopen the file.

The C++ basic_filebuf class has a seekpos() function. You can use it to move the position-in-file pointer to a selected location in the file -- to the beginning or to the end, or to some point within the file. There is also a seekoff() function that can be used for a similar purpose.

Thanks for the attentions, Mark44 and DrClaude. I'm grateful.
 
Personally, I'd read the entire thing into a buffer, then parse it into a data structure. That way you can later do something like:

Code:
item = chart.x(0);
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K