C++;使用用户输入打开不同目录中的文件 我一直在编写一个C++程序,它最终会从文本文件中删除某些不需要的格式。不幸的是,我还远远没有达到这个目标

C++;使用用户输入打开不同目录中的文件 我一直在编写一个C++程序,它最终会从文本文件中删除某些不需要的格式。不幸的是,我还远远没有达到这个目标,c++,fopen,fstream,directory-structure,C++,Fopen,Fstream,Directory Structure,我目前的问题是,我似乎无法打开位于给定(用户输入)目录中的文本文件。这就是我目前所处的位置,去掉了大部分不必要的部分: //header, main function, bulk //variable definitions FILE * pFile; //char filePathBuffer [512]; --unused, from older attempts string currentLine = "", command = "", commandList = "ct", fil

我目前的问题是,我似乎无法打开位于给定(用户输入)目录中的文本文件。这就是我目前所处的位置,去掉了大部分不必要的部分:

//header, main function, bulk

//variable definitions
FILE * pFile;
//char filePathBuffer [512];  --unused, from older attempts
string currentLine = "", command = "", commandList = "ct", filePath = "defaultPath";
int quotePos = 0, slashPos = 0, bufferLength = 0;
bool contQuote = true, contSlash = true;

cout << "> ";
getline(cin, command);

//various exit commands
while(command != "q" && command != "quit" && command != "exit") {

    //check for valid commands stored in a string
    //--definitely not the best way to do it, but it's functional

    if(commandList.find(command) == commandList.npos) {
        puts("\nPlease enter a valid command.\n");
    }
    else if(command == "t") {
        puts("\nPlease enter the file path:\n");
        cout << "> ";
        getline(cin, filePath);

        //rip all quotes out of the entered file path
        while(contQuote == true) {
            quotePos = filePath.find("\"");

            if(quotePos == filePath.npos)
                contQuote = false;
            else
                filePath.erase(quotePos, 1);
        }

        pFile = fopen(filePath.c_str(), "r+");

        //I've also tried doing countless variations directly in the code,
        //as opposed to using user-input. No luck.
        //pFile = fopen("C:\\test.txt", "r+");

        if(pFile!=NULL) {
            cout << "\nFile opened!" << endl << endl;
            fclose (pFile);
        }
        else
            cerr << "\nFile failed to open!\n\n";

    }

    //reset variables to default values
    quotePos = -1;
    slashPos = -1;
    contQuote = true;
    contSlash = true;

    cout << "> ";
    getline(cin, command);
}
//头,主函数,大容量
//变量定义
文件*pFile;
//char文件路径缓冲区[512]--未使用,来自以前的尝试
字符串currentLine=“”,command=“”,commandList=“ct”,filePath=“defaultPath”;
int quotePos=0,slashPos=0,bufferLength=0;
bool contQuote=true,contSlash=true;

\\
判断,我猜你是在windows上

尝试此功能:

#include <windows.h>
bool fileExist(const std::string& fileName_in)
{ 
  DWORD ftyp = GetFileAttributesA(fileName_in.c_str());
  if (ftyp == INVALID_FILE_ATTRIBUTES)
    return false;
  if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
    return false;   // this is a directory
  return true;      // this is a normal file
}
#包括
bool fileExist(const std::string和fileName_in)
{ 
DWORD ftyp=GetFileAttributesA(fileName_in.c_str());
if(ftyp==无效的\u文件\u属性)
返回false;
if(ftyp&文件属性目录)
return false;//这是一个目录
return true;//这是一个普通文件
}

使用此函数尝试输入字符串。如果返回false,请告诉用户他是盲人:)

我建议您在调用fopen时查看filePath包含的内容。然后你就能看到哪里出了问题

两种方法:

  • 在调用fopen之前打印出值

  • 使用调试器,在fopen调用上放置断点并检查filepath的内容


  • 看起来像是权限问题。您在其他子目录中尝试过吗?我添加了一些代码来检查fopen()返回时的errno值-注意下面的“权限被拒绝”:


    你使用的是哪个平台?那么我的解决方案应该可以:)是的,我也想到了——我已经做了无数次了。当我输入C:\test.txt时,它会打印出C:\test.txt。文件存在吗?是的,文件存在。如果我使用与可执行文件位于同一目录中的文件,代码可以正常工作。您对C:/root文件夹有写权限吗?啊。。。是 啊这就是问题所在。我从没想过以管理员的身份运行我的可执行文件。。。真尴尬。facepalm-非常感谢!它返回true-因此可以看到该文件。那么,我不太确定出了什么问题。:)编辑:哇,真尴尬。我忘了以管理员身份运行可执行文件。谢谢你的帮助!
    > t
    
    Please enter the file path:
    
    > .\test.cpp
    
    File opened!
    
    > t
    
    Please enter the file path:
    
    > c:\setup.log
    
    File failed to open with error: Permission denied
    
    > t
    
    Please enter the file path:
    
    > c:\cygwin\cygwin.bat
    
    File opened!
    
    >