C/C++ Extracting information of data in column format using C++

AI Thread Summary
To extract specific data from a file in C++, the discussion highlights the use of the `>>` operator, which functions similarly to `cin` by reading data until it encounters a space, making it suitable for columnar data. To focus on a specific column, users can utilize this operator in conjunction with file streams. For resetting the read position to the beginning of the file, closing and reopening the file is one method, but alternatives like the C standard library's `rewind()` function or C++'s `seekpos()` and `seekoff()` functions are also effective. Additionally, one participant suggests reading the entire file into a buffer for easier parsing and data manipulation, allowing for more flexible access to the data structure.
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 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 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

Back
Top