You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:31:15