无法加载C++文本文件

C++ text file cannot be loaded

本文关键字:文本 文件 C++ 加载      更新时间:2023-10-16

所以我的驱动程序看起来像这样:

#include "problem2.h"
#include "problem1.h"
#include "problem3.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
using namespace std;
template <typename T>
T convertString (std::string str){
    T ret;
    std::stringstream ss(str);
    ss >>ret;
    return ret;
}
int main()
{
    ifstream infile("text.txt", ios::in);
    if(!infile)
    {
        cerr <<"File could not be opend"<<endl;
    }
    SortedLinked mylist;
    int a;
    int b;
    string c;
    string d;
    string e;
    int f;
    char g;
    string h;
    string mystr;
    int mymin;
    int mysec;
    while(infile>>a>>b>>c>>d>>e>>f>>g>>h)
    {
        mystr = a+b;
        mymin = convertString<int>(e.substr(0,2));
        mysec = convertString<int>(e.substr(3, 4));
        replace(h.begin(), h.end(), '_', ' ');
        Runner M(mystr, f, mymin, mysec);
        mylist.additem(M);
    }
    return 0;
}

我将text.file放在同一个文件夹中,它在我几天前刚刚尝试的另一台PC上工作。现在,每次我尝试运行它时,它都会直接给我"文件无法打开"。我不知道出了什么问题。我甚至把一个文本.txt放在与 c++.sln 相同的目录中,另一个放在与我的 .cpp 和 .h 文件相同的目录中,只是为了确保有一个文本.txt可以打开。

首先,我尝试通过Xcode,我将文本.txt放在项目目录和.cpp和.h目录中。无法打开文件。

然后,我

通过可视化工作室感到疲倦,我确实.txt与我的.exe放在同一个目录中。"CMD.EXE 以上述路径作为当前目录启动。不支持 UNC 路径。默认为 Windows 目录。文件无法打开"

答案在此错误消息中:

"CMD.EXE 以上述路径作为当前目录启动。北卡罗来纳大学 不支持路径。默认为 Windows 目录。文件可以 不开门"

您正在从网络共享和 cmd 启动.exe.exe将工作目录更改为 %WINDOWS% 目录。然后程序尝试按相对文件名打开文件,但肯定失败。将exe和文本文件复制到本地文件夹,它可以正常工作。