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

The Archive Base Latest Questions

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

I have the current code that readings a text file into memory: std::streampos fsize

  • 0

I have the current code that readings a text file into memory:

std::streampos fsize = 0;
std::ifstream file(fileName, std::ios::binary); // open file

if(!file.good()) {
    std::cout << "Error opening file";
    return 0;
}

// get length of file
file.seekg(0, ios::end);
fsize = file.tellg();

// allocate memory
char *memory = new char[fsize];

// read data as a block
file.seekg (0, ios::beg);
file.read (memory, fsize);

file.close(); // close file

return fsize;

Now i have code that iterates over it. If the line starts with ‘v’ it reads the 3 preceding float values, and same with if it starts with ‘n’, but into different arrays.

char* p = memory;       // pointer to start of memory
char* e = memory + fsize;   // pointer to end of memory

while (p != e) {
    if (memcmp(p, "v", 1) == 0) { 
        sscanf(p, "v %f %f %f", &a[vI], &b[vI], &c[vI]);
        vI++;
    } else if (memcmp(p, "n",  1) == 0) {
        sscanf(p, "v %f %f %f", &d[nI], &e[nI], &f[nI]);
        nI++;           
    while (*p++ != (char) 0x0A);
}

I know there must be a better/safer way to do this.

  • 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-28T07:40:25+00:00Added an answer on May 28, 2026 at 7:40 am

    I assume you have a text-file there. This can be done a lot simpler. First, don’t open the file in binary-mode, and simply read linewise. The following is a possible implementation:

    template<class output_iterator>
    void read_file(std::istream &input, output_iterator v1, output_iterator v2,
                   output_iterator v1) {
        std::string line_buffer;
    
        while(std::getline(input, line_buffer)) { // read each line of text
            if(line_buffer[0] == 'v') {
                std::stringstream line_stream(line_buffer.substr(1)); // drop the 'v'
                // read three consecutive floats
                line_stream >> *v1++ >> *v2++ >> *v3++; 
            }
        }
    }
    

    This code assumes that lines starting with 'v' are well-formed. You can use it like this:

    std::vector<float> values1, values2, values3;
    std::fstream input_file(fileName);
    
    read_file(input_file, std::back_inserter(values1), std::back_inserter(values2),
              std::back_inserter(values3));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
I have a code sample that gets a SEL from the current object, SEL
I have some code that is accessing Membership.GetUser() to get the current logged in
I am learning the basics of reading from text files. I have code that
I have a text editor that uses code almost identical to the below for
As a beginner in Ocaml, I have this current working code: ... let ch_in
I am having a bit of difficulty with this current code I have set
I have this code: Dim pathString As String = HttpContext.Current.Request.MapPath(Banking.mdb) Dim odbconBanking As New
The JavaScript code window.print() can print the current HTML page. If I have a
We have a scenario where some .NET code is attempting to access the current

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.