0

I have written a program to enter, edit, show and search for data with ID. I wrote it using files, the program runs but the data is not shown in the above sections. Is it possible to help me with the problem. I attached the show part

void reportdata() 
{   
    person member;
    fstream fp("company.dat",ios::out|ios::in|ios::binary); 
    fp.read((char*)(&member), sizeof(person))
theduck
  • 2,589
  • 13
  • 17
  • 23
  • Just look at the arguments you're passing to the fstream constructor. That's **one** long odd-looking file name, the opening of which is probably puking all over the place, but you never check your IO operations for success or failure. It should look like `fstream fp("company.dat",ios::out|ios::in|ios::binary);` – WhozCraig Oct 19 '19 at 10:27
  • Does it still not work or was it just this typo? Maybe there is a bug where you show the data? – Lukas-T Oct 19 '19 at 11:04
  • You did not check the file was successfully opened: `if( !fp ) return -1;` Also, replace the file name with the full path. – zdf Oct 19 '19 at 11:15
  • it does not read files. how could I fix this? – user12242548 Oct 19 '19 at 11:48
  • @user12242548: Before calling `fp.read`, you must call [`fp.fail()`](https://en.cppreference.com/w/cpp/io/basic_ios/fail) in order to determine whether the file was opened successfully. If it fails, then one possible reason is that the file is not in your [current working directory](https://en.wikipedia.org/wiki/Working_directory). In that case, you may want to open the file using the full path (including the directory name) instead of only the file name. – Andreas Wenzel Oct 20 '19 at 00:15
  • @user12242548: Alternatively, you can use C++ exceptions for determining whether opening a file was successful. See [this thread](https://stackoverflow.com/questions/9670396/exception-handling-and-opening-a-file) for further information. – Andreas Wenzel Oct 20 '19 at 00:35
  • 1
    @AndreasWenzel [https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool](https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool). – zdf Oct 20 '19 at 03:36

0 Answers0