尝试从输入文件中获取字符

里克·卢卡森(Rick Lukassen)

我正在尝试从输入文件中获取字符,但是我真的无法使其正常工作,有人可以帮助我解决这个问题吗?我事先为格式表示歉意,这让我感到困惑。

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());

您用outfile犯了同样的错误。

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保持封闭。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从输入文件中获取字符 '\n'?

来自分类Dev

从文件中获取字符串

来自分类Dev

如何从输入字符串中获取字符数?

来自分类Dev

C-在结构中获取字符*的用户输入

来自分类Dev

C-在结构中获取字符*的用户输入

来自分类Dev

如何从输入框中获取字符串?

来自分类Dev

在C中从文件读取字符串输入

来自分类Dev

在python中从文件中获取字符串

来自分类Dev

如何从bash中的文件中获取字符串?

来自分类Dev

从bash中的文件行获取字符串

来自分类Dev

骆驼从文件中获取字符串

来自分类Dev

使用Regex从.java文件中获取字符串

来自分类Dev

如何使用php从文件中获取字符串

来自分类Dev

从文件中获取字符串的 PowerShell 问题

来自分类Dev

如何在文本文件中读取字符并获取字符长度?

来自分类Dev

尝试从Jframe中的表中获取字符串时,为什么会出现错误?

来自分类Dev

除非找到空字符串,否则在 C 中获取字符串输入

来自分类Dev

从输入类型获取字符串 Base64:文件?

来自分类Dev

NullPointerException 尝试从数组中获取字符串元素时

来自分类Dev

在JSX / React中从Flux设计模式中的输入文本字段获取字符串

来自分类Dev

根据一列中的输入从一列中获取字符

来自分类Dev

在c中的二维数组中获取字符串输入

来自分类Dev

尝试从文本文件中读取字符串

来自分类Dev

如何在输入按钮属性jQuery中获取字符串数据

来自分类Dev

从输入中获取字符串并在AngularJS中提出AJAX请求

来自分类Dev

如何在GNU Guile中读取字符串以获取用户输入?

来自分类Dev

如何从C中具有领先空格的stdin获取字符串输入?

来自分类Dev

Python Webscraping:如何从输入代码的value属性中获取字符串?

来自分类Dev

使用 make 命令获取字符串输入以用作 C 程序中的主要参数

Related 相关文章

  1. 1

    如何从输入文件中获取字符 '\n'?

  2. 2

    从文件中获取字符串

  3. 3

    如何从输入字符串中获取字符数?

  4. 4

    C-在结构中获取字符*的用户输入

  5. 5

    C-在结构中获取字符*的用户输入

  6. 6

    如何从输入框中获取字符串?

  7. 7

    在C中从文件读取字符串输入

  8. 8

    在python中从文件中获取字符串

  9. 9

    如何从bash中的文件中获取字符串?

  10. 10

    从bash中的文件行获取字符串

  11. 11

    骆驼从文件中获取字符串

  12. 12

    使用Regex从.java文件中获取字符串

  13. 13

    如何使用php从文件中获取字符串

  14. 14

    从文件中获取字符串的 PowerShell 问题

  15. 15

    如何在文本文件中读取字符并获取字符长度?

  16. 16

    尝试从Jframe中的表中获取字符串时,为什么会出现错误?

  17. 17

    除非找到空字符串,否则在 C 中获取字符串输入

  18. 18

    从输入类型获取字符串 Base64:文件?

  19. 19

    NullPointerException 尝试从数组中获取字符串元素时

  20. 20

    在JSX / React中从Flux设计模式中的输入文本字段获取字符串

  21. 21

    根据一列中的输入从一列中获取字符

  22. 22

    在c中的二维数组中获取字符串输入

  23. 23

    尝试从文本文件中读取字符串

  24. 24

    如何在输入按钮属性jQuery中获取字符串数据

  25. 25

    从输入中获取字符串并在AngularJS中提出AJAX请求

  26. 26

    如何在GNU Guile中读取字符串以获取用户输入?

  27. 27

    如何从C中具有领先空格的stdin获取字符串输入?

  28. 28

    Python Webscraping:如何从输入代码的value属性中获取字符串?

  29. 29

    使用 make 命令获取字符串输入以用作 C 程序中的主要参数

热门标签

归档