【问题标题】:Code to print out number of lines and integer values打印行数和整数值的代码
【发布时间】:2015-11-01 01:00:09
【问题描述】:

我创建了一个名为 program.txt 的数据文件。我需要创建代码,从该 program.txt
中打印出行数和整数值 这是我写的文字

 1

 35
 45
 87

 9 

 100

program.text 中有这些值

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
  string calc;
  int test;
  int cool;
  int book;

  ifstream myfile;
  ifstream outof;
  myfile.open ("program.txt");
  outof.open("program.txt");

  if (myfile.fail())
  {
    cerr<<"Error Opening file"<<endl;
  }

  if(outof.fail ())
  {
    cerr<<"Error Opening file"<<endl;
  }

  while (!myfile.eof ())
  {
    getline(myfile,calc);
         ++book;      
  }

  cout <<book<<endl;

  while (!outof.eof ())
  {
    outof<<test;// 
    cool++;
  }

  cout<<cool<<endl;
  myfile.close();
  outof.close();
}

在 cerr 之后,我尝试了退出 (1),它说退出未定义。 我是新手,任何帮助将不胜感激。谢谢 这是 C++ 顺便说一句。

【问题讨论】:

    标签: c++ file


    【解决方案1】:

    问题是您使用的是ifstream,它代表INPUT 文件流。您想使用ofstream,即OUTPUT 文件流。您无法写入输入文件流,因此未定义

    此外,您可以在出现错误后使用return 1;,而不是在main 函数中使用return 1;。这将终止程序,返回 1 作为退出代码。

    如果真的要使用exit,需要#include &lt;cstdlib&gt;

    【讨论】:

    • 好的,我更改为 ofstream,但现在我在第 46 行出现错误。它说没有匹配到调用“getline”,这是我得到的唯一错误。再次感谢您的帮助。跨度>
    • 你把它们都改成ofstream了吗?你的输入需要是ifstream,输出需要是stream。
    • 是的,我都改成了ofstream,我猜你的意思是第一个改成ifstream,第二个改成ofstream?我还编辑了原始帖子以显示第 46 行。好的,现在我进行了更正并运行构建,没有错误,但没有输出
    • 是的。如果要输出到文件,则需要 OFSTREAM。如果您正在从文件中读取 INPUT,则需要 IFSTREAM。使用适合每个变量的那个。
    • 那么在这种情况下,我会同时使用 ofstream,我只从我制作的文件中获取信息,不输入任何内容,我正确吗?或者不!
    【解决方案2】:

    您定义的输入和预期输出没有明确定义,所以我不确定您要做什么。但是,这是一个总体思路:

    将文件名放入 fstream 的构造函数将自动尝试打开文件以进行读/写。您不再需要调用 .open() 了。此外,如果您不知道自己在做什么,则不应同时读取和写入同一个文件。

    std::ifstream myInputFile("program.txt");
    std::ofstream myOutputFile("programOut.txt");
    

    不要检查myInputFile.fail(),只需使用重载的布尔运算符。深度讲解:ifstream::is_open vs ifstream::fail?

    if (!myInputFile) {
        //Something went wrong
        std::cerr << "Failed to open input file" << std::endl;
        return 1;
    }
    

    定义您的 std::string 以在您阅读它们时保存行、读取所有输入文件并计算行数。

    std::string line;
    int lineCount = 0;
    while (getline(myInputFile,line)) {
        ++lineCount;
        //Do something with 'line'
    }
    

    也许您需要存储输入文件中的行数,以便在输出文件的开头输出行数,您可能想要#include &lt;vector&gt; 并执行以下操作:

    std::string line;
    std::vector<std::string> linesFromFile;
    
    //Read in all lines of the input file and store them in the vector
    while (getline(myInputFile, line)) {
        linesFromFile.emplace_back(line);
    }
    
    //All lines read, good time to close input file
    myInputFile.close();
    
    //Print number of lines read
    myOutputFile << linesFromFile.size() << std::endl;
    
    //Loop through lines and print them
    for (auto &lineFromFile : linesFromFile) {
        myOutputFile << lineFromFile << std::endl;
    }
    
    //Done outputting, close output file
    myOutputFile.close();
    

    【讨论】:

    • 我试图打印出 program.text 文件有多少行和整数值。我班上的朋友帮助我完成了这个程序。他做对了,所以他帮助我,我们只是将变量更改为随机名称!
    • 我现在做了一些更正它运行良好但没有输出意味着输出是空白的有什么运气吗?看看你是否可以运行这段代码,也许是我做错了。代码看起来很完美!
    • 手术成功但患者死亡!这就是我现在的状态! :( 打破我成为程序员的梦想!
    • 你不能输出到“文件流”(ifstream)
    猜你喜欢
    • 2022-11-30
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2012-01-27
    相关资源
    最近更新 更多