为什么我的文件不能用 ifstream C++ 打开?
Why is my file is not opening with ifstream C++?
尽管这是一个相当简单的问题,但我无法通过 inputFileStream(inputFilePath)
打开文件。有人能指引我正确的方向吗(我不是来这里接受学校作业的答案的)?
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
using namespace std;
const string ID_LINE = "James McMillan - CS 1336 050 - Assignment 26";
int main(int argc, char *argv[]) {
cout << ID_LINE << endl << endl;
// guard clause - invalid number of arguments
if (argc != 3) {
cout << "Usage: " << argv[0] << "<input file path> <output file path>" << endl;
return 1;
}
//extract arguments
string programPath = argv[0];
string inputFilePath = argv[1];
string outputFilePath = argv[2];
cout << "Program path: " << programPath << endl;
cout << "Input file path: " << inputFilePath << endl;
cout << "Output file path: " << outputFilePath << endl << endl;
cout << "Creating input file stream..." << endl;
ifstream inputFileStream;
cout << "Created input file stream." << endl;
cout << "Opening input file stream: " << inputFilePath << endl;
inputFileStream.open(inputFilePath);
if (!inputFileStream.is_open()) {
cout << "Unable to open input file stream: " << inputFilePath << endl;
return 1;
}
}
您的解决方案可能来自检查文件是否存在,尝试
//Add this at the top
#include <experimental/filesystem>
//somewhere in the body
const auto has_the_file = std::experimental::filesystem::exists(inputFilePath);
if (!has_the_file)
{
std::cout << "Oh No! Can't find" << inputFilePath << std::endl;
}
此外,我不会解析参数,而是手动设置路径以确认您对如何指定文件路径的期望。
尽管这是一个相当简单的问题,但我无法通过 inputFileStream(inputFilePath)
打开文件。有人能指引我正确的方向吗(我不是来这里接受学校作业的答案的)?
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
using namespace std;
const string ID_LINE = "James McMillan - CS 1336 050 - Assignment 26";
int main(int argc, char *argv[]) {
cout << ID_LINE << endl << endl;
// guard clause - invalid number of arguments
if (argc != 3) {
cout << "Usage: " << argv[0] << "<input file path> <output file path>" << endl;
return 1;
}
//extract arguments
string programPath = argv[0];
string inputFilePath = argv[1];
string outputFilePath = argv[2];
cout << "Program path: " << programPath << endl;
cout << "Input file path: " << inputFilePath << endl;
cout << "Output file path: " << outputFilePath << endl << endl;
cout << "Creating input file stream..." << endl;
ifstream inputFileStream;
cout << "Created input file stream." << endl;
cout << "Opening input file stream: " << inputFilePath << endl;
inputFileStream.open(inputFilePath);
if (!inputFileStream.is_open()) {
cout << "Unable to open input file stream: " << inputFilePath << endl;
return 1;
}
}
您的解决方案可能来自检查文件是否存在,尝试
//Add this at the top
#include <experimental/filesystem>
//somewhere in the body
const auto has_the_file = std::experimental::filesystem::exists(inputFilePath);
if (!has_the_file)
{
std::cout << "Oh No! Can't find" << inputFilePath << std::endl;
}
此外,我不会解析参数,而是手动设置路径以确认您对如何指定文件路径的期望。