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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:16:24+00:00 2026-06-13T01:16:24+00:00

I have the following block of code that i am using to read a

  • 0

I have the following block of code that i am using to read a text file of the following format:

firstname lastname id mark
firstname lastname id mark

Following is the block of code.

void DBManager::ReadFile(void){
fstream myfile; /*fstream object that will be used for file input and output operations*/
char* fn;       /*pointer to the storage which will hold firstname*/
char* ln;       /*pointer to the storage which will hold lastname*/
int id;         /*integer var to hold the id*/
float mark;     /*float var to hold the mark*/

/*read in the filename*/
g_FileName = new char[1024];                /*allocate memory on the heap to store filename*/
cout << "Please enter the filename:";
    cin >> g_FileName;

/*open file*/
myfile.open(g_FileName, ios::in | ios::out);

if(myfile.is_open()){   /*check if the file opening is successful*/
    cout << "File reading successful !\n";

    /*read information from the file into temporary variables before passing them onto the heap*/
    while (!myfile.eof()) {

        fn=(char*) new char[1024];
        ln=(char*) new char[1024];
        myfile >> fn >> ln >> id >> mark;
        cout << fn << " " << ln << " " << id << " " << mark << " " << endl;

    }
    myfile.close();
}
else{                   /*else print error and return*/
    perror("");
    return;
}

}

The above block of code works ! 🙂
But I am surprised as to how myfile knows it is supposed to hold one line at a time and how its being smart enough about setting the four variables.

I am new to C++ , and hence this might be covered in some sort of documentation. But i would be happy to have some insight from you’ll or a link to somewhere i can understand fstream objects better.

  • 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-13T01:16:25+00:00Added an answer on June 13, 2026 at 1:16 am

    In C++, std::fstream is a type of stream which works specifically for files. When reading from a file, the interface for std::fstream is almost identical to std::cin. Input streams are programmed to read the next word or number when asked with the >> operator. They know where words and numbers are because they are separated by white space. In the default locale, spaces, tabs and newlines are considered to be white space. You can change the locale to include other characters, like commas, and have those be skipped while reading from a file. Basically, when reading with input streams, newlines and spaces are treated the same.

    Some nice explanation for learning about streams is here: http://www.cprogramming.com/tutorial/c++-iostreams.html

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

Sidebar

Related Questions

I have the following block of jQuery code that I'm using to copy some
I have the following markup / code block in the ASPX file. The binding
I have a file that was encrypted using AES. I use the following NSData
I'm using MVC3 and in my view I have the following code block; Response.Redirect(@Url.Action(index,home,
I have a block of code that will take a block of text like
I'm using agsXMPP in my .NET project (WinForms) I have the following Code block,
I have the following block of code in my bean class - HttpServletResponse response
i have the following block of code: $(#contact_container form, #contact_details form).live( submit, function(event) {
Hi I have following code block public class Driver { static String x =
I have the following block of code which works fine; var boughtItemsToday = (from

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.