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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:34:17+00:00 2026-06-15T16:34:17+00:00

This is my code: #include <iostream> #include <fstream> #include <string> using namespace std; struct

  • 0

This is my code:

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

struct car
{
    string name, model;
    int year;   
};

void search_car(int CarYear)
{
    cout<<"1";
    ifstream in;
    cout<<"2";
    car c1;
    cout<<"3";
    in.open("Cars.txt",ios::binary|ios::in);
    cout<<"4"<<endl;
    while(!in.eof())
    {
        cout<<" 5";
        in.read((char *) &c1, sizeof(car));
        cout<<" 6.Car Year: "<<c1.year<<endl;
        if(c1.year == CarYear)
        {
            cout<<" 7>>> ";
            cout<<c1.name<<" "<<c1.model<<" "<<c1.year;
            cout<<" <<<8"<<endl;
        }
    }
    cout<<" 9";
    in.close();
    cout<<" 10";    
}

void main()
{
    car c[100];
    int carNum, menuAct = 0, CarYear = -1, cycle = 1;
    ofstream out;
    while (cycle == 1)
    {
        //clrscr();
        cout<<endl<<endl<<"1.Enter New car"<<endl<<"2.Search"<<endl<<"3.Exit"<<endl;
        cin>>menuAct;
        cout<<"   Menu Action: "<<menuAct<<endl;
        if(menuAct == 1)
        {
            cout<<"Enter Num OF Cars: ";
            cin>>carNum;
            out.open("Cars.txt",ios::binary|ios::out|ios::app);
            for(int i = 0; i < carNum; i++)
            {
                cout<<"Enter Name OF Car: ";
                cin>>c[i].name;
                cout<<"Enter model OF Car: ";
                cin>>c[i].model;
                cout<<"Enter year OF Car: ";
                cin>>c[i].year;     
                out.write((char *) &c[i], sizeof(car));
            }
            out.close();
        }
        else if(menuAct == 2)
        {
            cout<<"Enter Car Year: ";
            cin>>CarYear;
            cout<<" 0";
            //cout<<" Y: "<<CarYear;
            search_car(CarYear);
            cout<<" 11";
            //menuAct = 0;
        }
        else if(menuAct == 3)
        {
            cycle = 0;
        }
    }   
}

Error:

http://s3.picofile.com/file/7580464836/cpp_err11.jpg

What is happened?
I`m used some cout to trace what is happening and code is stopped at number 10.

Also last car is printed twice!!!

  • 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-15T16:34:19+00:00Added an answer on June 15, 2026 at 4:34 pm

    I’m not surprised you’re having problems! You’re saving the bytes of the struct literally, and then when you read them back from the file, you’re hoping you’ll get a std::string back again. It doesn’t work that way at all.

    The problem is that the car struct doesn’t contain all the data it references: the std::string members are actually just pointers to a dynamic array containing the actual string data. You’re writing out the car structures as raw bytes, so the strings are never going to file. There’s no way they could ever be read back out of it.

    Worse, when you read the structs back in, you’re setting the pointers in the std::string to garbage values. You can’t possibly hope that the memory they happen to point to contains what you want.

    You need to define serialisation functions for the car struct, that send it to an outstream using a deep copy, and read it back in safely. Never write raw pointer values to file.

    Example code

    ostream& operator <<(ostream& os, const car& c) {
        return os << c.name << endl << c.model << endl << c.year << endl;
    }
    istream& operator >>(istream& is, car& c) {
        is >> c.name;
        is >> c.model;
        is >> c.year;
        return is;
    }
    

    Change in.read((char *) &c1, sizeof(car)); to in >> c1;.

    Change out.write((char *) &c[i], sizeof(car)); to out << c[i];.

    Much neater! PS. As an excellent general rule, don’t ever cast to char* until you understand what it does and how strings are handled!

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

Sidebar

Related Questions

I have this simple code: #include <iostream> #include <fstream> using namespace std; int main(void)
This is the C++ code: #include<iostream> using namespace std; int a=8; int fun(int &a)
In the following code : #include <iostream> #include <fstream> #include <string> using namespace std;
#include <iostream> #include <fstream> using namespace std; int main() { ifstream stream1(source.txt); string line
#include<iostream> #include<windows.h> #include<string> #include<fstream> using namespace std; class linklist //linked list class { struct
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
When trying to compile this code: #include <iostream> #include <vector> using namespace std; class
I'm having a problem with this code: #include <iostream> using namespace std; class A
Look at this code please: #include <iostream> using namespace std; class Ratio { public:
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout

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.