试图从命令行打开文件
Trying to open a file from command line
,所以我想编写一些像命令行中的type
一样起作用的代码
含义当我编写文本文件名时,它显示了它的内容。
我写了这篇文章:
int main(int argc, char* argv[])
{
FILE *t;
t = fopen(argv[1], "r"); // tring to open file from command line
if (t == NULL){
cout << "the file doesnt existsn";
return 0;
}
else{
fseek(t, 0, SEEK_END);
int size = ftell(t);
fseek(t, 0, SEEK_SET);
char* x = new char[size];
fread(x, size, 1, t);
for (int i = 0; i<size; i++)
{
cout << x[i];
}
delete[] x;
}
return 0;
}
我得到了调试断言失败错误
Exppression:file!=NULL <br>
在使用argv
的参数之前,请确保有足够的需求:
if (argc > 1) {
// We have enough args in argv, go for it
t = fopen(argv[1], "r");
} else {
/* do something else that doesn't need argv[1] i.e. ask the user */
}
相关文章:
- 通过命令行在Visual Studio中编译单个独立文件
- 是否可以在命令行中将输入参数传递给可执行文件
- 如何从命令行使用 cmake 为有根的安卓设备制作可执行文件?
- 使用 MinGW 在 Windows 上的命令行编译C++文件
- 程序运行,但文件无法打开.C++中的命令行
- 选择特定版本的 Visual Studio 命令行工具包,并根据特定版本的C++运行时环境编译文件
- 编写一个将 LLVM IR 文件作为命令行参数的程序
- 通过命令行参数获取llvm ir文件时面临问题
- 如果在命令行c++中给出了一个文件,那么如何读取该文件
- 如何在 Boost Program_Options 中使用命令行和分层配置文件的选项
- 如何在不使用文件扩展名的情况下使用命令行参数打开C++中的文本文件?
- 如何处理错误"E1696命令行错误:无法在Visual Studio 2017中打开元数据文件"mscorlib.dll"?
- 如何在窗口上的命令行中读取输入 .in 文件
- C++使用命令行输入 txt 文件,但打开文件失败
- Intel-Pin:如何将命令行论证传递给二进制文件
- boost::P rogram_options 一种判断值是从命令行还是 ini 文件的方法?
- 使用命令行参数从TXT文件中提取并在C 中运行特定类
- 试图从命令行打开文件
- 使用提升程序选项通过配置文件/命令行解析自定义对象
- MSI 文件命令行参数解析