나는 텍스트 파일에서 특정 원하지 않는 서식을 결국 제거 할 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에 추가 할 수 있도록) 끝에 행운이 없었습니다.
이 상황을 어떻게 해결할 수 있는지 알고 계십니까?
감사합니다.
어떤 플랫폼을 사용하고 있습니까? – FailedDev
Windows 7 64 비트. –
그럼 내 솔루션이 작동해야합니다 :) – FailedDev