Home > Software design >  this c code of file handling runs in dev c but not in vs code
this c code of file handling runs in dev c but not in vs code

Time:07-31

newbie here my code doesn't seem to compile in vscode. It give me the desired output while using dev c . It gives me error while reading from file, writing to a file no problem. I have posted error message below the code.

    #include <iostream>
    #include <fstream>
    #include <string.h>
    
    using namespace std;
    class student{
        private:
            char name[25];
            int id;
            int age;
        public:
            void get(){
                cin>>name>>id>>age;
            }
            void show(){
                cout<<name<<id<<age;
            }
            void write2file(){
                ofstream outfile("student.dat",ios::binary|ios::app);
                get();
                outfile.write(reinterpret_cast<char*>(this),sizeof(*this));
            }
            void readfromfile(){
                ifstream infile("student.dat",ios::binary|ios::in);
                while(!infile.eof()){
                    if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0){
                        show();
                    }
                }
            }
    };
    int main(){
        student s;
        s.write2file();
        s.readfromfile();
        return 0;
    
    
    }

Here is the error I got when I run the program in VS code, but same program run perfectly in dev c .

awd.cpp: In member function 'void student::readfromfile()':
awd.cpp:26:76: error: no match for 'operator>' (operand types are 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} and 'int')        
   26 |                 if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0){
      |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
      |                               |                                             |
      |                               |                                             int
      |                               std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}
awd.cpp:26:76: note: candidate: 'operator>(int, int)' (built-in)
   26 |                 if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0){
      |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
awd.cpp:26:76: note:   no known conversion for argument 1 from 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} to 'int'
In file included from C:/msys64/mingw64/include/c  /12.1.0/string:47,
                 from C:/msys64/mingw64/include/c  /12.1.0/bits/locale_classes.h:40,
                 from C:/msys64/mingw64/include/c  /12.1.0/bits/ios_base.h:41,
                 from C:/msys64/mingw64/include/c  /12.1.0/ios:42,
                 from C:/msys64/mingw64/include/c  /12.1.0/ostream:38,
                 from C:/msys64/mingw64/include/c  /12.1.0/iostream:39,
                 from awd.cpp:1:

CodePudding user response:

Better code would be this

        void readfromfile(){
            ifstream infile("student.dat",ios::binary|ios::in);
            while (infile.read(reinterpret_cast<char*>(this),sizeof(*this)) {
                show();
            }
        }

As already pointed out istream::read does not return an integer, which is what the original code seems to be assuming.

Instead, like most I/O functions, the stream itself is returned. In a boolean context this can be used to see if the stream is in a good state. If it is not then the previous I/O operation failed.

  • Related