Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:41:48+00:00 2026-06-12T08:41:48+00:00

i would like to C++-read a file line by line, and split each line

  • 0

i would like to C++-read a file line by line, and split each line w.r.t to "\t" character so as to fill in a matrix. my code would go like this

    ifstream data_x;
        double** test_data = new double*[100];
        for(int j = 0 ; j <  ; j++)
             test_data[j] = new double[4]; 

    data_x.open("X.txt");

    int i = 0;
    if (data_x.is_open()) 
    {
        while (!data_x.eof()) 
        {
            char** split = data_x.split("\t") 
            for(int k = 1 ; k < 4 ; k++)
                 test_data[i][k];
            i++;
        }
    }

    data_x.close();

    ifstream data_y;
    data_y.open("Y.txt");

    i = 0;
    if (data_y.is_open()) 
    {
        while (!data_y.eof()) 
        {
            data_y >> test_data[i][0];
            i++;
        }
    }

    data_y.close(); 

where syntax

 char** split = data_x.split("\t") 
 for(int k = 1 ; k < 4 ; k++)
      test_data[i][k];

is approximative. how to make this properly with C++ ?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:41:49+00:00Added an answer on June 12, 2026 at 8:41 am

    Assuming that your file only contains numbers, here’s the standard C++ idiom:

    #include <vector>
    #include <string>
    #include <sstream>
    #include <fstream>
    
    std::ifstream infile("data.txt");
    
    std::vector<std::vector<double>> matrix;
    
    for (std::string line; std::getline(infile, line); )
    {
         std::istringstream iss(line);
         std::vector<double> row;
    
         for (double d; iss >> d; )
         {
             row.push_back(d);
         }
    
         matrix.push_back(row);
    }
    

    If you know the size of the matrix, you can add the relevant reserve calls to avoid vector reallocations. You can also add tests for whether there was any unrecognized data on a line, but for now this should get you started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In java i would like to read a file line by line and print
I need to read a line like this from a file: 5 Chair 12.49
I have a file which i will read it line by line. Split each
I have some code to read the lines from a file, I would like
I would like to read a file in R as a table for a
I would like to read a text file and input its contents into an
I would like to read a DICOM file in C#. I don't want to
I would like to read the last 1 megabyte of a MP3 file and
I would like to know how is possible to read a file placed in
I have a file with data in it that I would like to split

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.