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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:34:32+00:00 2026-05-27T14:34:32+00:00

#include <iostream> #include <fstream> using namespace std; class info { private: char name[15]; char

  • 0
#include <iostream>
#include <fstream>

using namespace std;

class info {

private:
    char name[15];
    char surname[15];
    int age;
public:
    void input(){
        cout<<"Your name:"<<endl;
            cin.getline(name,15);
        cout<<"Your surname:"<<endl;
        cin.getline(surname,15);
        cout<<"Your age:"<<endl;
        cin>>age;
        to_file(name,surname,age);
    }

    void to_file(char name[15], char surname[15], int age){
        fstream File ("example.bin", ios::out  | ios::binary | ios::app);
    // I doesn't know how to fill all variables(name,surname,age) in 1 variable (memblock) 
        //example File.write ( memory_block, size ); 

File.close();
    }

};

int main(){

info ob;
ob.input();

 return 0;
}

I don’t know how to write more than 1 variable into a file, please help, I included an example 😉 Maybe there are better ways to write to a file, please help me with this, it’s to hard for me to solve.

  • 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-27T14:34:32+00:00Added an answer on May 27, 2026 at 2:34 pm

    For a text file, you could easily output one variable per line using a similar << to the one you use with std::cout.

    For a binary file, you need to use std::ostream::write(), which writes a sequence of bytes. For your age attribute, you’ll need to reinterpret_cast this to const char* and write as many bytes as is necessary to hold an int for your machine architecture. Note that if you intend to read this binary date on a different machine, you’ll have to take word size and endianness into consideration. I also recommend that you zero the name and surname buffers before you use them lest you end up with artefacts of uninitialised memory in your binary file.

    Also, there’s no need to pass attributes of the class into the to_file() method.

    #include <cstring>
    #include <fstream>
    #include <iostream>
    
    class info
    {
    private:
        char name[15];
        char surname[15];
        int age;
    
    public:
        info()
            :name()
            ,surname()
            ,age(0)
        {
            memset(name, 0, sizeof name);
            memset(surname, 0, sizeof surname);
        }
    
        void input()
        {
            std::cout << "Your name:" << std::endl;
            std::cin.getline(name, 15);
    
            std::cout << "Your surname:" << std::endl;
            std::cin.getline(surname, 15);
    
            std::cout << "Your age:" << std::endl;
            std::cin >> age;
    
            to_file();
        }
    
        void to_file()
        {
            std::ofstream fs("example.bin", std::ios::out | std::ios::binary | std::ios::app);
            fs.write(name, sizeof name);
            fs.write(surname, sizeof surname);
            fs.write(reinterpret_cast<const char*>(&age), sizeof age);
            fs.close();
        }
    };
    
    int main()
    {
        info ob;
        ob.input();
    }
    

    A sample data file may look like this:

    % xxd example.bin
    0000000: 7573 6572 0000 0000 0000 0000 0000 0031  user...........1
    0000010: 3036 3938 3734 0000 0000 0000 0000 2f00  069874......../.
    0000020: 0000                                     ..
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <fstream> using namespace std; class binaryOperators { public: int i; binaryOperators
#include <iostream> #include <fstream> using namespace std; class Integer { public: int i; Integer
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int
#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
#include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; class Dict {
Say I do this (a contrived example): #include <iostream> #include <fstream> using namespace std;

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.