尝试从输入文件中获取字符
Attempting to get character from an input file
我正在尝试从输入文件中获取字符,但我无法真正让它工作,有人可以帮助我吗?我提前为格式道歉,这让我感到困惑。
open_input_and_output_file
基本上检查您是否可以打开文件,在 OTP 中,我尝试将每个字符从一个文件获取到另一个文件。由于我无法让它工作,我首先尝试在控制台应用程序中显示这些字符,但这也不起作用。
任何帮助将不胜感激,我希望提供的信息足够。
bool open_input_and_output_file(ifstream& infile, ofstream& outfile)
{
//Precondition: True
assert(true);
//Postcondition: Inputfile and outputfile have either been opened succesfully or you have been notified of it not opening succesfully.
string inputfile;
string outputfile;
cout<<"nPlease enter an input-file name (no spaces): ";
cin>>inputfile;
cout<<"NOTE: Input-file name and output-file name can NOT be the same!"<<endl;
cout<<"Please enter an output-file name (no spaces): ";
cin>>outputfile;
if(inputfile != outputfile)
{
cout<<"Input-file name and output-file name are not the same! Good job on reading!"<<endl;
ifstream infile(inputfile.c_str());
if(infile)
cout<<"Input-file: "<<inputfile<<" was opened succesfully!"<<endl;
if(!infile)
cout<<"Inputfile: "<<inputfile<<" could not be opened!"<<endl;
ofstream outfile(outputfile.c_str());
if(outfile)
cout<<"Output-file: "<<outputfile<<" was opened succesfully!"<<endl;
if(!outfile)
cout<<"Outputfile: "<<outputfile<<" could not be opened!"<<endl;
}
else
{
cout<<"Input-file name and output-file name are the same!"<<endl;
cout<<"Opening has failed!"<<endl;
}
return 0;
}
void OTP(ifstream& infile, ofstream& outfile)
{
int choice;
char character;
unsigned int r;
srand(r);
cout<<"nPlease enter 0 to encrypt or 1 to decrypt: ";
cin>>choice;
if(open_input_and_output_file(infile,outfile))
{
infile.get(character);
cout<<character;
}
}
我会说错误在这里
cout<<"Input-file name and output-file name are not the same! Good job on reading!"<<endl;
ifstream infile(inputfile.c_str());
应该是
cout<<"Input-file name and output-file name are not the same! Good job on reading!"<<endl;
infile.open(inputfile.c_str());
你在输出文件上犯了同样的错误。
ofstream outfile(outputfile.c_str());
应该是
outfile.open(outputfile.c_str());
您将 infile 和 outfile 作为参数传递给 open_input_and_output_file
函数,但随后在函数中再次声明它们。因此,当您打开文件时,您没有使用传递给open_input_and_output_file
的流,而是使用该函数本地的流。传递给open_input_and_output_file
的溪流保持关闭。
相关文章:
- 有没有办法通过使用十进制 ASCII 代码自动类型扣除来获取字符?
- 如何获取C++字符数据类型的地址
- CPP:如何获取字符的最后一个索引
- 试图在重载函数中获取字符和字符串的输入
- C++使用第二个函数获取字符数组的长度
- 如何获取字符*的实际长度?strlen 和 sizeof(target)/sizeof(target*)之间的区别?
- 如何获取字符并将其转换为字符串?C++
- 如何在C++中从字符串指针中获取字符
- 获取字符数组地址,然后转换为指针字符
- 使用指针在字符串中获取字符的位置
- 获取字符数组C++数
- 使用scanf()在数组中获取字符(不与空格分开)
- "Error: expected a '{' introducing a lambda body"尝试从地图<字符,字符中获取字符时> C++
- 从文本文件输入以获取字符C++
- 使用getchar()获取字符并将其保存到每次C 的新行中的文件中
- 需要帮助从文件中获取字符和整数数据
- 如何获取字符C++像素
- 如何获取字符*(字符数组)的真实长度和总长度
- 从文件而不是getchar中获取字符
- 从流 C++ 获取字符