C++中读取.dat文件失败问题求助
C++ File I/O: ifstream Won't Open .dat File in Same Directory (No Error Output)
I'm a complete C++ newbie diving into File I/O right now. I copied the textbook example code word for word, but it wouldn't work at all—plus there were no error messages to help me figure out why. I adjusted the code to add a check for whether the file opens successfully, but file.is_open() keeps returning false, and I'm stuck.
Here's the code I'm using now:
#include <fstream> #include <iostream> using namespace std; string f = "marks.dat"; ifstream file; int main() { int first = 0; file.open(f); if(file.is_open()){ file >> first; cout << first; } else { cout << "file has failed to open"; } return 0; }
The marks.dat file is in the same Assignment3.1 folder where my .cpp file is compiled. The full path to this folder is /Users/stjohn/Documents/cpp/Assignment3.1. I've already initialized first to 0 and added the file.is_open() check, but I still can't figure out why the file won't open.
内容的提问来源于stack exchange,提问作者YinGroen
相关产品推荐
相关产品推荐

