0

This is the code snippet, wherein "s" is a array of class objects.

for(int i =0; i<4; i++) 
    {
        cout<<"Student "<<i+1<<": "<<endl; 
        cout<<"Enter regno: "; 
        cin>>regno; 
        cout<<"Enter name: "; 
        cin>>name; 
        cout<<"Enter cgpa: "; 
        cin>>cgpa; 

        s[i].setregno(regno); 
        s[i].setname(name); 
        s[i].setcgpa(cgpa); 

        size[i] = s[i].getname().size(); 

        fout.write( (const char*) s[i].getregno(), sizeof(int)); 
        fout.write((const char* ) s[i].getcgpa(), sizeof(float));
        fout.write(reinterpret_cast<char *>(&size[i]), sizeof(int));
        fout.write(s[i].getname().c_str(), size[i]);

    }

I am getting the following error message while compiling: invalid cast from type 'float' to type 'const char*'

If I include & after the typecasting inside of write, I get this error: lvalue required as unary '&' operand

ChaoS Adm
  • 715
  • 1
  • 5
  • 12
  • 2
    You cannot learn C++ by just guessing at syntax. The language is *much* too complicated for that to ever work. You need to read [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list#388282), take classes, practice for a few years, etc. It's a fiendishly difficult language; you cannot learn it properly by just guessing at stuff - you have to learn and *know* the details. – Jesper Juhl Jun 04 '20 at 15:26
  • Yeah I guess. It always feels confusing in C++ as soon as file handling starts. Way more complex compared to Python xoxo – ChaoS Adm Jun 04 '20 at 16:16
  • The `write` function is expecting a pointer as the first argument. Does `getregno()` return a pointer. I can't see from here. – Thomas Matthews Jun 04 '20 at 16:51

1 Answers1

1

You were close.

Save the return value of s[i].getregno() to a variable:

int regno = s[i].getregno();

Then, pass that variable to write:

fout.write((const char *)&regno, sizeof regno); 

Do the same thing for .getcgpa().

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • How is this any different? I am sorry, I come from a Python background. I thought we could just operate on the returned data from a function and that wont make a difference compared to storing it in another variable – ChaoS Adm Jun 04 '20 at 16:00
  • @ChaoSAdm In C++ expressions are divided into categories (so-called *value categories*): rvalues (normally those refer to temporary objects) and lvalues (everything else). A number returned from a function is an rvalue, and a variable is an lvalue. As a safety measure, you can't take an address of an rvalue (because it would be way to easy to take an address, and then use it after the object it points to is destroyed; in your case it wouldn't happen, but the safety mechanism gets in the way regardless). – HolyBlackCat Jun 04 '20 at 16:14
  • That was useful information! – ChaoS Adm Jun 04 '20 at 16:15
  • I tried this out. The error is gone. However, the float and integer values are not being written in the file as it's supposed to. The file is getting written with some garbage values where the float and int values should be. – ChaoS Adm Jun 04 '20 at 16:46
  • @ChaoSAdm How did you determine that the values are garbage? You're writing them to the file in binary, so a regular text editor would display them as garbage. – HolyBlackCat Jun 04 '20 at 16:47
  • But then strings are perfectly visible when I open the file in Notepad as the ones I input? – ChaoS Adm Jun 04 '20 at 19:44
  • @ChaoSAdm Strings don't have distinct "text" and "binary" representations. For numbers, you can represent them either as binary (which is how they're normally stored in the memory) or as text (i.e. as a sequence of character codes for their textual representation (one character per digit), storing each code in binary (1 byte per character or more, depending on encoding)). Strings, on the other hand, *are* sequences of character codes. There's only one way to print them. – HolyBlackCat Jun 04 '20 at 20:10
  • But there must be some way to read binary file in a way which makes sense to the end user right? If I am writing an int to the file in binary format, I would expect to read an int while reading from the same. Otherwise, file handling wont make sense? – ChaoS Adm Jun 05 '20 at 04:24
  • @ChaoSAdm Yeah, you can read an int from binary, but a text editor is not the right tool. You could use a hex editor, or write a program that reads the integer and prints it. – HolyBlackCat Jun 05 '20 at 09:31
  • I didn't mean editor based reading. If I am trying to read from file using my c++ code, I am not capturing the int and float values. – ChaoS Adm Jun 05 '20 at 13:23
  • @ChaoSAdm Then you should ask a new question about it, and include the necessary details. – HolyBlackCat Jun 05 '20 at 13:24