-1

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:
  • 1
    `std::istream::read` does not return an `int`. – Sam Varshavchik Jul 30 '22 at 17:22
  • Looks like a bug in Dev C++. – Eljay Jul 30 '22 at 17:23
  • Remove "conio.h" and forget about it. It's a thing of the past and not portable. – Ulrich Eckhardt Jul 30 '22 at 17:23
  • 1
    Your code did not run in VS code and not in dev c++. Your code can be compiled with a compiler and your IDE will use one or more. And after compile and link, the generated executable will run under your OS and also not on your ide. You should first start with a better wording to describe the real problem you have. Question: Did your code not compile? If so, please provide the error message. Or have you only some syntax higlighting problems? Or did your application crash later... – Klaus Jul 30 '22 at 17:24
  • 2
    Dev-C++ is an old, outdated and unmaintained IDE. It also comes with an old and outdated compiler. There are free modern, maintained and up to date compilers and IDE available to download. – Some programmer dude Jul 30 '22 at 17:26
  • 1
    Also please take some time to read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) I also recommend you invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Jul 30 '22 at 17:27
  • 2
    And also using "using namespace std;" is bad practice... And writing a memory dump to disk and read it back and believe it is a perfect initialized object is wrong, it is UB! Even if that will work on all known compilers and OS, it is bad practice. And it heavily depends on padding, OS and compiler versions and maybe on the moon phase. It is in general a bad idea... yes, we know, it works, ... mostly :-) – Klaus Jul 30 '22 at 17:32
  • Use `std::string` instead of `char` arrays. The `cin >>` works nicely with `std::string`. There is no method to tell `operator>>` how many characters to read in or the limit. Use `std::getline` for inputting names. – Thomas Matthews Jul 30 '22 at 18:07
  • When outputting your member variables, you'll want to have some kind of separation, like space, comma or newline. Otherwise everything is appended together. – Thomas Matthews Jul 30 '22 at 18:08
  • The code is perfectly fine if it can run in dev c++. The problem is in vscode. @infinitezero – sorry myan Jul 30 '22 at 18:10
  • 1
    Just because it ran fine in one place does not mean the other place is wrong. Research which one is obeying what standard. – TheUndeadFish Jul 30 '22 at 18:12

1 Answers1

0

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.

john
  • 85,011
  • 4
  • 57
  • 81