[Error] cannot convert 'std::fstream' {aka 'std::basic_fstream<char>'} to 'FILE*' {aka '_iobuf*'} in assignment
时间: 2023-10-16 10:06:37 浏览: 109
FILE_COPY-and-RENAME.rar_site:www.pudn.com
This error occurs when you try to assign a value of type `std::fstream` to a variable of type `FILE*`. These two types are not compatible with each other, so the compiler generates an error.
To fix this error, you need to use the appropriate type for the variable you are assigning to. If you are working with C++ streams, continue using the `std::fstream` type instead of `FILE*`. If you need to use `FILE*`, you can convert the `std::fstream` to `FILE*` using the `std::fstream::rdbuf()` function, like this:
```
std::fstream file("myfile.txt");
FILE* fp = file.rdbuf()->file();
```
This code extracts the underlying `FILE*` pointer from the `std::fstream` object and assigns it to the `fp` variable. Now you can use the `fp` variable with `fread()`, `fwrite()`, and other C-style file I/O functions.
阅读全文