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

  • SEARCH
  • Home
  • 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 7066071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:02:25+00:00 2026-05-28T05:02:25+00:00

I am trying to read floating point numbers from a file into a dynamically

  • 0

I am trying to read floating point numbers from a file into a dynamically allocated 2D array. I am using c++.

In my .txt file the elements of a row are separated by a tab space each and each row starts on a new line.

My question is –

Can I increase the size of my array based on the no. of elements (rows and columns) present in the file? If so, please suggest a way to do it.

I thought of writing the no. of rows and columns on the first line of the text file and reading them in the beginning to set the limits of the loop which will allocate the space for my array. Is there a better way to do it, perhaps based on the formatting of the text file? This way, I think it will be closer to a streaming data kind of a scenario.

Thanks in advance.

Akhil

  • 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-05-28T05:02:26+00:00Added an answer on May 28, 2026 at 5:02 am

    Rather than using an array, consider using a std::vector, which automatically resizes. In C++, this is strongly preferred to using a raw array, since it’s safer and hides the resource management.

    In fact, using a std::vector in conjunction with the stream libraries, it’s very easy to read a file of tab-separated floating-point values:

    ifstream input("my-file.txt");
    vector<float> myValues;
    
    for (float f; input >> f; )
        myValues.push_back(f);
    

    Or, alternatively:

    ifstream input("my-file.txt");
    vector<float> myValues;
    
    myValues.insert(myValues.begin(),
                    istream_iterator<float>(input),
                    istream_iterator<float>());
    

    However, the above code will read a 1D array of floats, rather than the 2D array you wanted. To read a 2D array, one option is to read one line of the file at a time, then use a modification of the above code to break that line into individual floats. For example:

    ifstream input("my-file.txt");
    vector< vector<float> > myValues;
    
    for (string line; getline(input, line); ) {
        stringstream lineStream(line);
    
        vector<float> thisLine;
    
        thisLine.insert(thisLine.begin(),
                        istream_iterator<float>(lineStream),
                        istream_iterator<float>());
        myValues.push_back(thisLine);
    }
    

    Hope this helps!

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

Sidebar

Related Questions

am trying to read few lines from a txt file using JS,and i have
When trying to read a RSA private key from a file using the method
I'm trying to decipher this regexp formal definition of floating point numbers (from php.net)
I'm trying to read a .doc file into a database so that I can
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
Here is my problem, I'm trying to read in data from a file beginningbalance.dat
Trying to read into an object the following XML file (can be changed) into
Im trying to read a text file, x bytes at a time into a
Im trying to read an alarm structure from a Beckhoff - PLC into a
I'm trying to read a 16-bit greyscale TIFF file (BitsPerSample=16) using a small C

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.