code:

std::cout << "enter file to write\n> "; std::string filename; std::cin >> filename; std::ofstream myfile(filename, std::ofstream::app | std::ofstream::binary); if (myfile){ std::streamsize size = sizeof(city); myfile.write(temp, size); std::cout << "SAVED!\n"; } myfile.close(); 

City structure fields:

 struct city { char name[32]; int citizens; float territory; int startyear; int schools; }; 

mistake:

 test.cpp: In function 'int main()': test.cpp:61:36: error: no matching function for call to 'std::basic_ofstream<char>::write(city&, std::streamsize&)' myfile.write(temp, size); ^ In file included from /usr/include/c++/7/ostream:693:0, from /usr/include/c++/7/iostream:39, from test.cpp:2: /usr/include/c++/7/bits/ostream.tcc:182:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::streamsize = long int] basic_ostream<_CharT, _Traits>:: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/7/bits/ostream.tcc:182:5: note: no known conversion for argument 1 from 'city' to 'const char*' 

Closed due to the fact that off-topic participants VTT , 0xdb , aleksandr barakin , Kromster , Enikeyschik January 16 at 10:33 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - VTT, 0xdb, Kromster, Enikeyschik
If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    In this case, it’s not the structure fields that matter, but what you pass on. I suspect that tmp is not the address of the city object, but a link to it.

    While the function is waiting for a pointer to char .

    • I want to write a structure to a file. - Kolyan Ivantsov
    • temp is my copy of the city structure - Kolyan Ivantsov
    • How to make copy of structure by line? (char *) temp didn't roll xD - Kolyan Ivantsov
    • Pass (char*)&temp . And this is not a string, this is the address from which to take the recorded data. And immediately, so that it is not unexpected - if you replace in the future, for example, the char name[32]; on string name; - then such an entry in the file will immediately become incorrect. - Harry
    • i got it To write a structure to a file, I need to pass a character pointer to the write method to the address of the beginning of the structure instance and the actual size of the structure. So? - Kolyan Ivantsov