C++ C++;从命令行读取文件名的应用程序,使用read()方法读取文件';内容
我有这段代码,但在从命令行输入名称后,它开始工作,但输出是一个永无止境的奇怪字符数组。 我试图解决的问题是:编写一个C++应用程序,它使用Read()方法读取文件的内容。获得的数据显示在屏幕上。每次读取操作后检查系统状态。从命令行读取文件名。 我的代码是:C++ C++;从命令行读取文件名的应用程序,使用read()方法读取文件';内容,c++,file,command-line,C++,File,Command Line,我有这段代码,但在从命令行输入名称后,它开始工作,但输出是一个永无止境的奇怪字符数组。 我试图解决的问题是:编写一个C++应用程序,它使用Read()方法读取文件的内容。获得的数据显示在屏幕上。每次读取操作后检查系统状态。从命令行读取文件名。 我的代码是: #include<iostream> #include<fstream> using namespace std; int main(int argc, char* argv[]) { char arr[15
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
char arr[15];
ifstream file;
file.open(argv[1], ios::in); //filename read from the command line
if (argc == 1)
{
cout << "Enter file name from command line!";
}
int readState = 0;
while (!file.eof())
{
file.read(arr, 10); //reading the file's content
if (file.good())
{
arr[10] = '\0';
cout << arr;
readState++; //checking the system's state after each read()
}
else
{
cout << arr;
}
}
file.close();
return 0;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
char-arr[15];
ifstream文件;
file.open(argv[1],ios::in);//从命令行读取文件名
如果(argc==1)
{
cout这个版本正在运行,
看看我看到的变化
首先
在尝试打开之前,我正在检查是否有路径
秒
检查文件是否打开,如果打开
编辑
将读取更改为
编辑
将std::ios::out添加到打开模式,以创建不存在的文件
#include <stdio.h>
#include <iostream>
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
char arr[15];
ifstream file;
if (argc == 1)// <- moved it before opening the file
{
cout << "Enter file name from command line!";
return 0;
}
file.open(argv[1], ios::in|std::ios::out); //filename read from the command line and also create the file if it dosent exist
if(!file.is_open()) // <- second change
{
std::cout << "file not opening\n";
return 0;
}
int readState = 0;
std::string line;
while ( std::getline(file, string ) ) // <-- third fix
{
cout << arr;
readState++; //checking the system's state after each read()
}
else
{
cout << arr;
}
}
file.close();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
char-arr[15];
ifstream文件;
如果(argc==1)/没问题,看起来我可以解决所有的bug。下面是好代码:
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char* argv[]) //using command line arguments
{
char arr[15];
ofstream wfile;
if (argc == 1)
{
cout << "Enter file name from command line!";
return 0;
}
cout << "Enter an array:";
cin >> arr; //reading the array from KBD
wfile.open(argv[1], ios::out); //filename read from the command line
if (!wfile)
{
cout << "File can't be opened!";
exit(1);
}
else
cout << "File created" << endl;
wfile.write(arr, strlen(arr)); //write the array into file
wfile.close(); //closing file
ifstream file;
file.open(argv[1], ios::in); //opening the file to read from it
if (!file.is_open()) //if file is not opened displays a message
{
cout << "File not opening\n";
exit(1);
}
int readState = 0; //initializing the system state
char *array;
array = new char[10]; //allocating memory
while (!file.eof())
{
file.read(array, 10); //using read() method
for(int i=0;i<10;i++)
cout << array[i]; //display the array from the file
readState++;
}
cout <<"\nSystem state is:"<< readState; //checking system state
delete[]array; //eliberating memory
file.close(); //closing file
return 0;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])//使用命令行参数
{
char-arr[15];
流文件;
如果(argc==1)
{
cout arr;//从KBD读取数组
open(argv[1],ios::out);//从命令行读取文件名
如果(!wfile)
{
如果file.good()
为false,您试图打印一个不是以null结尾的字符串…@NateEldredge我试图在那里显示一条简单的消息,输出是一样的..并且您总是在数组中的第十个字符之后终止,而不是查看实际读取的字符数。文件内容是否至少有10个字符,以及10个字符的倍数?RegaRunLube你应该修复这个bug。它迟早会给你带来的。注意:这个文件在某种程度上失败了,不是EOF,你有一个无限循环。很难处理未格式化的读,但是仍然值得保护你自己。这修正了我先前标记的问题,但是发问者的指令是写一个C++应用程序来读取文件。的内容使用read()方法。虽然在大多数情况下,getline
是一个非常好的主意,但赋值似乎禁止做聪明的事情。我也尝试了这段代码,现在输出的只是消息“文件未创建”…我的命令行有问题吗?或者我不明白为什么不工作。从调试属性,我转到调试,并在命令行参数框中输入了一个名称。如果文件不存在,打开该文件不会创建该文件。如果要创建该文件,请打开该文件进行写入,