×

Loading...
Ad by
Ad by

哎,好人做到底吧,进内

本文发表在 rolia.net 枫下论坛#include <iostream>
#include <fstream>
#include <cstring>
#include <stdio.h>
using namespace std;

class Car
{
char model[30];
char name[20];
char phone[20];
char description[50];
//char date_recent[10];
int year_recent;
int month_recent;
int day_recent;
int number_service;
int year_next;
int month_next;
int day_next;

public:
Car()
{
strcpy(model, " ");
strcpy(name, " ");
strcpy(phone, " ");
strcpy(description, " ");
//strcpy(date_recent, " ");
year_recent=0;
month_recent=0;
day_recent=0;
year_next=0;
month_next=0;
day_next=0;
};

friend ostream &operator << (ostream &out, Car C)
{
//out<<C.model<<" "<<C.name<<" "<<C.phone<<" "<<C.description<<" "<<C.year_recent<<" "<<C.month_recent<<" "<<C.day_recent<<" "<<C.number_service<<" "<<C.year_next<<" "<<C.month_next<<" "<<C.day_next<<endl;
out << "Car " << C.model << " " << C.name << "\n";
return out;
};

friend istream &operator >> (istream &in, Car &C)
{
cout<<"\nCar Model => ";
//in.getline(C.model,30);
in>>C.model;

cout<<"Owner's name => ";
//in.getline(C.name,30);
in>>C.name;
fflush(stdin);
#if 0

fflush(stdin);
cout<<"Telephone Number => ";
in>>C.phon;

fflush(stdin);
cout<<"Service Descritption => ";
in>>C.description;

cout<<"Year of the most recent service => ";
in>>C.year_recent;
cout<<"Month of the most recent service => ";
in>>C.month_recent;
cout<<"Day of the most recent service => ";
in>>C.day_recent;

cout<<"Number of Services Per Year => ";
in>>C.number_service;

cout<<"Year of the next service => ";
in>>C.year_next;
cout<<"Month of the next service => ";
in>>C.month_next;
cout<<"Day of the next service => ";
in>>C.day_next;
#endif
return in;
};
}; //class




int
main()
{
int x;
cout<<"\n Number of Cars => ";
cin>>x;
Car *pcars = new Car[x];

if (!pcars)
{
cout<<"\nMemory Allocation Error!";
exit(1);
}

for (int i=0; i<x; i++)
cin>>pcars[i];

ofstream file1("CARS.DAT", ios::binary | ios::out);
//ofstream file1("CARS.DAT", ios::app);
if (!file1)
{
cerr<<"Error in opening file for output!";
exit(1);
}

file1.write((char *)pcars, sizeof(Car)*x);
file1.close();
//ifstream file2("CARS.DAT");
ifstream file2("CARS.DAT", ios::binary | ios::in);
if (!file2)
{
cerr<<"Error in opening file for input!";
exit(1);
}
#if 1

//Car temp;
Car *temp = new Car[x];
cout<<"read file"<<"\n";

file2.read((char *)temp, sizeof(Car)*x);

for (int i=0; i<x; i++) {
cout<< i+1 <<" : " << temp[i] << "\n";
}

delete []temp;
#endif
file2.close();

delete []pcars;

return 0;
}

运行结果

Administrator@MAX-20121108DXF ~/test
$ g++ test.cpp

Administrator@MAX-20121108DXF ~/test
$ ./a
Number of Cars => 2

Car Model => s
Owner's name => s

Car Model => d
Owner's name => d
read file
1 : Car s s

2 : Car d d更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / C++ I can not read .DAT file and output to screen ------- file2.read((char *)&temp, sizeof(Car)*x);
    本文发表在 rolia.net 枫下论坛Do you have somebody tell me where I make mistake?
    Car *temp = new Car[x];
    for (int i=0;i<x; i++)
    { cout<<"read file";
    file2.read((char *)&temp, sizeof(Car)*x);
    cout<<(i+1)<<"Car="<<temp;
    }


    -----------------------------------------------------------------------------
    void main()
    {
    int x;
    cout<<"\n Number of Cars => ";
    cin>>x;
    Car *pcars = new Car[x];

    if (!pcars)
    {
    cout<<"\nMemory Allocation Error!";
    exit(1);
    }

    for (int i=0;i<x; i++)
    cin>>pcars[i];

    ofstream file1("CARS.DAT", ios::out);
    if (!file1)
    {
    cerr<<"Error in opening file for output!";
    exit(1);
    }

    file1.write((char *)pcars, sizeof(Car)*x);
    file1.close();

    ifstream file2("CARS.DAT");
    //ifstream file2("CARS.DAT", ios::in);
    if (!file2)
    {
    cerr<<"Error in opening file for input!";
    exit(1);
    }

    //Car temp;
    Car *temp = new Car[x];
    for (int i=0;i<x; i++)
    { cout<<"read file";
    file2.read((char *)&temp, sizeof(Car)*x);
    cout<<(i+1)<<"Car="<<temp;
    }更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • 你的for loop是想循环x编,但你的file2.read()一次就read了sizeof(Car)*x个data,这样你相当于read了x * x个car数据,超出文件范围了吧。
      • cancel loop. Is it right?
        you mean don't use loop, like below,

        Car *temp = new Car[x];
        file2.read((char *)&temp, sizeof(Car)*x);
        cout<<temp;

        Can output all car information to screen
      • Help me change it. I don't have idea.
        How I change?
        First-chance exception at 0x10306e6f (msvcp100d.dll) in file.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
        Unhandled exception at 0x10306e6f (msvcp100d.dll) in file.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.

        //Car temp;
        Car *temp = new Car[x];

        cout<<"read file";
        file2.read((char *)&temp, sizeof(Car));
        cout<<"Car="<<temp;
        • 两个方案。
          1.
          for (int i=0;i<x; i++)
          { cout<<"read file";
          file2.read((char *)&temp[i++], sizeof(Car));
          cout<<(i)<<"Car="<<temp;
          }

          2.
          cout<<"read file";
          file2.read((char *)temp, sizeof(Car)*x);

          for (int i=0;i<x; i++) {
          cout<<(i+1)<<"Car="<<temp[i];
          }
      • why output to screen ....===>? Car=00504CC0. I think still have some wrong. see content text
        I change to below

        Car *temp = new Car[x];

        cout<<"read file";
        for (int i=0;i<x; i++)
        {
        file2.read((char *)&temp[i++], sizeof(Car));
        cout<<(i)<<"Car="<<temp;
        }


        -------------------output---------------------------------
        Number of Cars => 1

        Car Model => 111
        read file1Car=00504CC0
        • 注意指针
          cout<<"read file";
          for (int i=0;i<x; i++)
          {
          file2.read((char *)&temp[i], sizeof(Car));
          cout<<(i+1)<<"Car="<<temp[i];
          }
      • cout<<(i+1)<<"Car="<<temp[i];
        temp[i] ---- Here wrong when I compiling
      • wrong sentence for cout<<....... Pls. get help
        • 再帮下去,我就只能写出来,compile完了再说了,可是没环境啊。
          • Run problem-------- do you understand void __CLR_OR_THIS_CALL gbump(int _Off) { // alter current position in read buffer by _Off *_IGcount -= _Off; *_IGnext += _Off; }
            本文发表在 rolia.net 枫下论坛I give you whole code. try to compiler in your PC.

            #include "StdAfx.h"
            #include <iostream>
            #include <fstream>
            #include <cstring>
            #include <stdio.h>
            #include <conio.h>
            using namespace std;

            class Car
            {
            char model[30];
            char name[20];
            char phone[20];
            char description[50];
            //char date_recent[10];
            int year_recent;
            int month_recent;
            int day_recent;
            int number_service;
            int year_next;
            int month_next;
            int day_next;
            public:
            Car()
            {
            strcpy(model, " ");
            strcpy(name, " ");
            strcpy(phone, " ");
            strcpy(description, " ");
            //strcpy(date_recent, " ");
            year_recent=0;
            month_recent=0;
            day_recent=0;
            year_next=0;
            month_next=0;
            day_next=0;
            }

            friend istream &operator>>(istream &in, Car &C)
            {
            fflush(stdin);
            cout<<"\nCar Model => ";
            in.getline(C.model,30);
            /*
            fflush(stdin);
            cout<<"Owner's name => ";
            in.getline(C.name,30);

            fflush(stdin);
            cout<<"Telephone Number => ";
            in.getline(C.phone,30);

            fflush(stdin);
            cout<<"Service Descritption => ";
            in.getline(C.description,30);

            cout<<"Year of the most recent service => ";
            in>>C.year_recent;
            cout<<"Month of the most recent service => ";
            in>>C.month_recent;
            cout<<"Day of the most recent service => ";
            in>>C.day_recent;

            cout<<"Number of Services Per Year => ";
            in>>C.number_service;

            cout<<"Year of the next service => ";
            in>>C.year_next;
            cout<<"Month of the next service => ";
            in>>C.month_next;
            cout<<"Day of the next service => ";
            in>>C.day_next;
            */
            return in;
            }

            friend ostream &operator<<(ofstream &out, Car &C)
            {
            //out<<C.model<<" "<<C.name<<" "<<C.phone<<" "<<C.description<<" "<<C.year_recent<<" "<<C.month_recent<<" "<<C.day_recent<<" "<<C.number_service<<" "<<C.year_next<<" "<<C.month_next<<" "<<C.day_next<<endl;
            out<<C.model<<" ";
            return out;
            }

            }; //class


            void main()
            {
            int x;
            cout<<"\n Number of Cars => ";
            cin>>x;
            Car *pcars = new Car[x];

            if (!pcars)
            {
            cout<<"\nMemory Allocation Error!";
            exit(1);
            }

            for (int i=0; i<x; i++)
            cin>>pcars[i];

            //ofstream file1("CARS.DAT", ios::binary | ios::app);
            ofstream file1("CARS.DAT", ios::app);
            if (!file1)
            {
            cerr<<"Error in opening file for output!";
            exit(1);
            }

            file1.write((char *)pcars, sizeof(Car)*x);
            file1.close();

            ifstream file2("CARS.DAT");
            //ifstream file2("CARS.DAT", ios::in);
            if (!file2)
            {
            cerr<<"Error in opening file for input!";
            exit(1);
            }

            //Car temp;
            Car *temp = new Car[x];
            cout<<"read file";
            for (int i=0; i<x; i++)
            {
            file2.read((char *)&temp, sizeof(Car));
            cout<<(i+1)<<"Car="<<temp;
            //temp++;
            //cout<<*temp;
            //cout<<(i+1)<<"Car=";
            }

            file2.close();
            delete []pcars;
            // delete []temp;
            getch();
            }更多精彩文章及讨论,请光临枫下论坛 rolia.net
            • 家里电脑没有visual studio。
              看了你的code, 你是先把pcars[i]写到文件里,在试图读文件把数据放在 temp[i]里,再输出到 cout。

              作为试验,你可以在读文件前先试试

              for (int i=0; i<x; i++)
              cout<<pcars[i];
          • I give you whole code
      • you can download C++ software online
        • 哎,好人做到底吧,进内
          本文发表在 rolia.net 枫下论坛#include <iostream>
          #include <fstream>
          #include <cstring>
          #include <stdio.h>
          using namespace std;

          class Car
          {
          char model[30];
          char name[20];
          char phone[20];
          char description[50];
          //char date_recent[10];
          int year_recent;
          int month_recent;
          int day_recent;
          int number_service;
          int year_next;
          int month_next;
          int day_next;

          public:
          Car()
          {
          strcpy(model, " ");
          strcpy(name, " ");
          strcpy(phone, " ");
          strcpy(description, " ");
          //strcpy(date_recent, " ");
          year_recent=0;
          month_recent=0;
          day_recent=0;
          year_next=0;
          month_next=0;
          day_next=0;
          };

          friend ostream &operator << (ostream &out, Car C)
          {
          //out<<C.model<<" "<<C.name<<" "<<C.phone<<" "<<C.description<<" "<<C.year_recent<<" "<<C.month_recent<<" "<<C.day_recent<<" "<<C.number_service<<" "<<C.year_next<<" "<<C.month_next<<" "<<C.day_next<<endl;
          out << "Car " << C.model << " " << C.name << "\n";
          return out;
          };

          friend istream &operator >> (istream &in, Car &C)
          {
          cout<<"\nCar Model => ";
          //in.getline(C.model,30);
          in>>C.model;

          cout<<"Owner's name => ";
          //in.getline(C.name,30);
          in>>C.name;
          fflush(stdin);
          #if 0

          fflush(stdin);
          cout<<"Telephone Number => ";
          in>>C.phon;

          fflush(stdin);
          cout<<"Service Descritption => ";
          in>>C.description;

          cout<<"Year of the most recent service => ";
          in>>C.year_recent;
          cout<<"Month of the most recent service => ";
          in>>C.month_recent;
          cout<<"Day of the most recent service => ";
          in>>C.day_recent;

          cout<<"Number of Services Per Year => ";
          in>>C.number_service;

          cout<<"Year of the next service => ";
          in>>C.year_next;
          cout<<"Month of the next service => ";
          in>>C.month_next;
          cout<<"Day of the next service => ";
          in>>C.day_next;
          #endif
          return in;
          };
          }; //class




          int
          main()
          {
          int x;
          cout<<"\n Number of Cars => ";
          cin>>x;
          Car *pcars = new Car[x];

          if (!pcars)
          {
          cout<<"\nMemory Allocation Error!";
          exit(1);
          }

          for (int i=0; i<x; i++)
          cin>>pcars[i];

          ofstream file1("CARS.DAT", ios::binary | ios::out);
          //ofstream file1("CARS.DAT", ios::app);
          if (!file1)
          {
          cerr<<"Error in opening file for output!";
          exit(1);
          }

          file1.write((char *)pcars, sizeof(Car)*x);
          file1.close();
          //ifstream file2("CARS.DAT");
          ifstream file2("CARS.DAT", ios::binary | ios::in);
          if (!file2)
          {
          cerr<<"Error in opening file for input!";
          exit(1);
          }
          #if 1

          //Car temp;
          Car *temp = new Car[x];
          cout<<"read file"<<"\n";

          file2.read((char *)temp, sizeof(Car)*x);

          for (int i=0; i<x; i++) {
          cout<< i+1 <<" : " << temp[i] << "\n";
          }

          delete []temp;
          #endif
          file2.close();

          delete []pcars;

          return 0;
          }

          运行结果

          Administrator@MAX-20121108DXF ~/test
          $ g++ test.cpp

          Administrator@MAX-20121108DXF ~/test
          $ ./a
          Number of Cars => 2

          Car Model => s
          Owner's name => s

          Car Model => d
          Owner's name => d
          read file
          1 : Car s s

          2 : Car d d更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • 今大虾原来是C++大拿啊。
            • 不敢不敢,略知一二。
      • You solve the problem for this time input record and output this time record information. If I want to append record to the previous file and output all records from the files(including old and new record )? How I modify the code?
        • 免费帮你做整个作业,恐怕不太现实,你还得自己努力呀。你的问题其实很简单,注意用ifstream的fseek就行了。