2011-12-01 85 views
1

我一直在编写一个C++程序,最终将从文本文件中删除某些不需要的格式。不幸的是,我还远远达不到这个目标。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!\n\n"; 

    } 

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

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

我没有实际确认输入的字符串是否应该有报价或不 - 我不能让他们的工作无论哪种方式。我也尝试做一个filePath.find('\\')来查找反斜杠(这样我就可以在filePath中添加第二个反斜杠以进行正确的解析),但最终没有运气。

你们有什么想法我可以解决这种情况吗?

谢谢!

+0

您正在使用哪个平台? – FailedDev

+0

Windows 7 64位。 –

+0

然后我的解决方案应该工作:) – FailedDev

回答

1

我建议你看看什么filePath包含当你打电话给fopen。然后你将能够看到发生了什么问题。

两种方法:

  1. 打印出来fopen的调用您调用fopen

  2. 使用调试器之前的价值,地方的断点,并检查文件路径

+0

是的,我也想到了 - 我已经做到了,无数次。当我输入C:\ test.txt时,它打印出C:\ test.txt。 –

+0

那好像很好。文件是否存在? – ravenspoint

+0

是的,文件存在。如果我在与可执行文件相同的目录中使用文件,代码工作正常。 –

1

我想你是在从\\判断的窗口。

试试这个功能:

#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,告诉你的用户他是盲人:)

+0

它返回true - 因此它看到该文件。然后不确定发生了什么问题。 :)编辑:哇,多么尴尬。我忘了以admin身份运行我的可执行文件。谢谢您的帮助! –

+0

@ 13ack.Stab没问题:) .. – FailedDev

0

貌似内容许可问题。你在其他子目录中尝试过吗?当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:\cygwin\cygwin.bat 

File opened! 

> 
相关问题