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

VSCode无法读取当前目录文件求助(附配置及测试代码)

问题求助

我查阅了相关资料,尝试修改过多组launch.json与tasks.json配置,但均未解决问题。我的C++程序通过控制台读取数据时完全正常,说明问题不在代码本身,但测试多个程序后发现均无法读取文件,希望能得到解决办法。

我的launch.json配置

{
    "version": "0.2.0",
        "configurations": [
        {
          "name": "C/C++: gcc.exe build and debug active file",
          "type": "cppdbg",
          "request": "launch",
          "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${fileDirname}",
          "environment": [],
          "externalConsole": false,
          "MIMode": "gdb",
        "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
        "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
       ],
     
      }
      ],
      "preLaunchTask": "C/C++: gcc.exe build active file"
      }

我的tasks.json配置

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\CodeBlocks\\MinGW\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\Program Files\\CodeBlocks\\MinGW\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

测试案例

文件bac.in包含数据:1 7 2 1 1 2 1 7 2

运行以下程序时,控制台输出0而非预期的144:

#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("bac.in");
int a[10];

int main()
{ 
    int S=0, i;
    while ( fin >> i )
    {
        if ( a[i] == 0 )
        {
            a[i]= 1;
            S += 10;
        }
        S += i*i;
    }
    cout << S;
    return 0;
}

内容的提问来源于stack exchange,提问作者PikaCiung

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 21:57:34