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 1066703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:03:22+00:00 2026-05-16T20:03:22+00:00

I am having trouble undoing this method, that dumps essentially a matrix of numbers

  • 0

I am having trouble “undoing” this method, that dumps essentially a matrix of numbers of variable size into a text file:

void vectorToFile(char *name, vector<vector<double>>* a){
    FILE* fp = fopen(name, "w");
    for(int i=0;i<a->size();i++){
        for(int j=0;j<a->at(i).size();j++){
            fprintf(fp, "%f ", a->at(i).at(j));
        }
        fprintf(fp, "\n");
    }
    fclose(fp);
}

I am having trouble implementing the reverse:

vector<vector<double>> fileToVector(char *name){ ??? }

I am guaranteed that the numbers in the file form a “rectangle”, i.e. the sizes of inner vectors are all equal, but I don’t know how to figure out the number of entries per row, and the number of columns.

Can anyone point me in the right direction? Every example I found so far implements something much easier, with hardcoded size, or sizes given in first row (which I cannot afford to do unfortunately)

  • 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-16T20:03:23+00:00Added an answer on May 16, 2026 at 8:03 pm

    I’m new at C++ so I’m not sure if this is a good approach or not, but I would open the file, read in the input line by line, parsing each line as I read it. Here’s some example code (untested, uncompiled):

    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    std::vector<std::vector<double> > fileToVector(const char *name)
    {
        std::vector<std::vector<double> > result;
        std::ifstream input (name);
        std::string lineData;
    
        while(getline(input, lineData))
        {
            double d;
            std::vector<double> row;
            std::stringstream lineStream(lineData);
    
            while (lineStream >> d)
                row.push_back(d);
    
            result.push_back(row);
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble rewriting the name of a flash file: /flash/shell.1257347618.swf (the numbers are a
Having trouble with an .htaccess file on WAMP. Works on the live server, but
Having trouble framing this assertion in Rspec for my rails app. A User has
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
Having trouble getting this syntax right: SELECT DISTINCT id FROM metadata WHERE (meta_key =
Having trouble with this -- a couple of other related posts out there, but
Having trouble with an sql statement to retrieve a record between two timestamps. This
Having trouble determining if a file name conforms to a specific convention as follows.
Having trouble with this code, wondering if I'm trying to achieve something impossible. I'm
Having trouble trying to turn an XDocument into an list of objects, in particular

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.