用用户输入打开不同目录下的文件
C++ Opening files in different directories with user input
我一直在编写一个c++程序,它将最终从文本文件中删除某些不需要的格式。遗憾的是,我离这个目标还有很长的路要走。
我目前的问题是我似乎无法打开位于给定(用户输入)目录中的文本文件。这是我到目前为止所做的,剥离了最不需要的块:
//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!nn";
}
//reset variables to default values
quotePos = -1;
slashPos = -1;
contQuote = true;
contSlash = true;
cout << "> ";
getline(cin, command);
}
我实际上不确定输入字符串是否应该有引号-我无法让它们以任何方式工作。我还尝试执行filePath.find('')来查找反斜杠(这样我就可以在filePath中添加第二个反斜杠以进行正确的解析),但最终没有成功。
你们知道我该如何补救这种情况吗?
谢谢!
我建议您在调用fopen时查看filePath包含的内容。然后你就能知道哪里出了问题。
两种方法:
-
在调用fopen之前打印出值
-
使用调试器,在fopen调用上设置断点并检查文件路径
从\
判断,我猜你是在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
}
用这个函数尝试输入字符串。如果返回false,告诉用户他是盲人:)
看起来像是权限问题。您在其他子目录中尝试过吗?我添加了一些代码来检查fopen()返回时的errno值-注意下面的"Permission Denied":
> 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:cygwincygwin.bat
File opened!
>
相关文章:
- 以非特权用户身份查询 NTFS 特殊文件的元数据?
- 用户输入字符串的文件附加问题..C++
- MAKE:找不到包含的用户定义的头文件?
- 从标准输入读取文件后读取用户输入
- 限制用户可以在 QFileDialog 中选择的文件数量
- 如何在公共头文件中向库的用户公开枚举,同时在内部使用它?
- Windows 后台服务(系统配置文件)无法使用 C++ 访问 win 10 上的用户 appData 文件夹?
- 如何让用户在 tinyxml2 中输入 XML 文件的路径
- Arduino-Makefile:编译器无法编译用户库 cpp 文件
- 将所有 *.txt 文件合并到一个文本文件中,其名称由用户使用 C++ 提供
- 如何在 c++ 中将密码和用户名保存到 .txt 文件中.如果用户尝试登录,我仍然希望能够检索它们
- 尝试将文本文件中的整数替换为用户输入的整数 [C++]
- 尝试读取.txt文件并根据用户输入的字母拉出特定行
- Win7 C++ - 从以用户身份登录的服务启动可执行文件的问题
- 可以将Boost消息队列文件重定向到用户指定的位置
- 在 Win32 应用中,如果未选择文件夹并且用户单击"确定",则文件夹选择对话框将关闭
- 是否可以用类似C/C++(或任何语言)的语言,从作为用户输入的字符串或文件中创建用户定义的数据类型
- 读取用户必须提供一些参数的文件
- g++ 生成依赖项文件缺少用户定义的标头
- 用户使用C++中的删除功能删除文件