c++ - 为什么我的文件没有用 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;
}
另外,我不会解析参数,而是手动设置路径以确认您对如何指定文件路径的期望。
推荐阅读
- android - 如何将捕获的图像发送到 Apache 服务器?
- javascript - 三.js | 导入的 Blender 模型就像以低质量渲染
- mysql - 如何找到只有男性顾客的店铺?
- javascript - 你能解释一下这段 JavaScript 代码的含义吗?
- javascript - 如何使用 javascript 在单独的 html 页面上显示答案?
- javascript - 如何平移、旋转、缩放 GLTF 3D 模型(Three.js)
- excel - 验证完成后自动关闭用户表单
- python - 如何使用 Python Mininet API 发送命令?
- windows - Makefile Windows:.. 更改目录时不被识别为内部或外部命令
- bash - 如何从 $PATH 中删除 git 或更改路径以使其不再包含它(macOS)