-1

I have a set of data in a .txt file that has an arbitrary number of columns, specified by the user in the input. I want to read that file, pick one of the columns and save it in an array. What is the best way to do this?

I have read this, this and this, but they all establish in the code the specific number of columns. I want it to be some input, so that the code is "general" and saves in that array the specific column from the input. Thank you!

EDIT: This is an example of how the input looks like - the total number of Columns (particles) is specified by the user. The output will be some other .txt of data coming from this one.

TIME     PART1       PART2       PART3       PART4  
0    0.0147496  934.902 0.0949583   -1192.37    0.0141576   950.604 0.0905118   -1074.44    
1.66667e-005    0.0147497   2804.7  0.0949583   -3577.12    0.0141576   2851.81 0.0905117   -3223.33    
3.33333e-005    0.0147497   4674.5  0.0949582   -5961.86    0.0141577   4753.02 0.0905116   -5372.21    
5e-005  0.0147498   6544.3  0.094958    -8346.6 0.0141578   6654.22 0.0905115   -7521.09    
6.66667e-005    0.01475 8414.09 0.0949578   -10731.3    0.0141579   8555.41 0.0905114   -9669.96
5
  • specified by the user in the input - show that, please. Commented Jun 16, 2021 at 9:36
  • It can be done in many ways. One could be a cin >> col;, and then I want the program to store the data of that specific column in some dynamical array. Commented Jun 16, 2021 at 9:39
  • I mean, show what the actual input and expected output will look like. Commented Jun 16, 2021 at 9:41
  • @500-InternalServerError Just included. Commented Jun 16, 2021 at 9:45
  • not a duplicate, but it can help you a lot: stackoverflow.com/questions/1120140/…. Also the answers you link should help, to read a single column just read all and ignore all but the one you are interested in. Also note that it is not apparent why the q&as you link do not help to solve your issue. Adding a minimal reproducible example of your attempt and explaining why it fails would help Commented Jun 16, 2021 at 9:48

1 Answer 1

1

I assume the user enters the coumn number over the console. So you can use the built-in cin function to read the input. You can use for loop and string streams to get the values. Code below; although you may have tweek it a little bit as per your needs

Edit: The code below has been edited a little bit. Now it should answer your question.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
   int n;
   cin >>n; //user needs to input the column number
   fstream newfile;
   //newfile.open("file.txt",ios::out);  // open the file to perform write operation using file object; Activate this function if you want to overwrite
   newfile.open("file.txt",ios::in); //open the file to perform read operation using file object
   if (newfile.is_open())
   {
      string line;
      getline(newfile, line); //skipping the first line
      while(getline(newfile, line))//loop throuhg rest of the lines
      { 
            int temp=n;
            while(temp != 0)//loop until you get to the requied column
            {
                getline(newfile, line, '\t'); //get the vaue separted by tab='\t'. Be sure that the last column also ends in '\t'
                temp--;
            }
            cout<<line<<endl; //now line holds the element on the loop-row of the selected column
      }
      newfile.close(); //close the file object.
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

did you mean while(temp--) ? You are currently not using temp
@463035818_is_not_a_number I think you are right
Thank you for your answer! It seems it is what I needed, althought I am still adapting it to my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.