没有在C ++中没有换行符的情况下无法打印字符串 [英] String is not printing without new line character in C++

查看:80
本文介绍了没有在C ++中没有换行符的情况下无法打印字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打开文件,并从中获取行。
第一行应该说有多少个变量,以及它们的名字。
第二行应该是使用这些变量的逻辑方程式。
的任务是让它打印出变量和方程式的真值表。

I'm opening a file, and getting lines from it. The first line should say how many variables there are, and what their names are. The second line should be a logic equation using these variables. The assignment is to have it print out a truth table for the variables and equation.

程序插入的第一行在没有我插入的情况下不会打印一个换行符。我尝试转换为字符串并同时使用printf和cout。

The first line the program is taking in is not printing without me inserting a new line character. I tried converting to a string and using both printf and cout.

输入所有内容的主文件:

Main file that inputs everything:

#include "truthTable2.h"

int main(int argc, const char* argv[]){
  ifstream inFile;
  if(argc != 2){
    cout << "Enter an input file name: ";
    char *inFileName = "";
    cin >> inFileName;
    inFile.open(inFileName);
  }
  else
    inFile.open(argv[1]);
  TruthTable tTable;
  while(!inFile.eof()){
    char variableLine[256];
    inFile.getline(variableLine, 256);
    printf("%s ", variableLine);
    string variable(variableLine);
    tTable.setVariables(variable);
    char formulaLine[256];
    inFile.getline(formulaLine, 256);
    cout << formulaLine << "\n";
    string formula(formulaLine);
    tTable.setFormula(formula);
    tTable.printTable();
  }
  inFile.close();
  return 0;
}

样本输入:

2 x y
( \wedge x ( \not y ) )

从此输出:

 ( \wedge x ( \not y ) )

我认为造成该问题的原因在整个程序的其余部分也给我带来了麻烦。在对variableLine进行标记之后,如果没有换行符,它将不会打印,并且在计算公式时不会找到第二个变量。

I think whatever is causing this is giving me problems throughout the rest of the program as well. After I tokenize the variableLine it does not print without a new line character and it does not find the second variable when evaluating the formula.

推荐答案

std :: ostream 的输出需要刷新。通常在写入换行符 \n 时会自动刷新。如果要强制刷新流,则可以使用 std :: flush 操纵器,如下所示:

An std::ostream's output needs to be flushed. It is normally flushed automatically when a line-feed \n is written. If you want to force the stream to flush, you can use the std::flush manipulator like so:

std::cout << "foo" << std::flush;

编辑:尽管我的帖子清楚地回答了为什么我的台词除非我输出 \n 字符,否则不会显示?您说这不能回答您的问题,所以我将尝试一些心灵阅读尝试回答您的真实问题。

Edit: Although my post clearly answers the question "Why does my line not show up unless I output a \n character?" You said this does not answer your question, so I will attempt some mind reading to try and answer your real question.

由于我不知道,您真正想知道的是什么,我将在此处指出您的代码有误的几处内容,这可能会帮助您找到问题或澄清您的问题。

Since I have no idea what you really want know, I'll point out several things here that are wrong with your code and it might help you find your problem or clarify your question.

首先,如果使用从 std :: cin 输入的文件名,则当 argc <2 ,您将100%保证会导致您的应用程序失败。原因是 inFileName 指向的字符缓冲区包含一个字节,为终止的空字符保留。如果有人输入任何文本任何内容,您将获得缓冲区溢出。如果有人输入一个空字符串,您的程序将不会打开任何文件,并且 inFile.open(...); 将返回一个您未检查的错误代码,因此您的

First, if you are using the file name input from std::cin, when argc<2, you will, a 100% guaranteed, cause a failure in your application. The reason is that the character buffer pointed to by inFileName contains a single byte, reserved for the terminating null character. If someone enters any text whatsoever, you will get a buffer overrun. If someone enters an empty string, your program will open no file and inFile.open(...); will return an error code that you don't check, so your program won't crash, but still won't work.

第二,其他行的输入不必要地限制为256个字符,并且同样危险(例如,行长于256个字符将导致缓冲区溢出。由于您最终会在内容之外创建 std :: string 实例,因此您应该只使用 std :: getline()

Second, the other line inputs are needlessly limited to 256 characters and are just as dangerous (i.e. lines longer that 256 characters will cause a bufer overrun). Since you eventually create std::string instances out of the content, you should just plainly use std::getline(). It is shorter to type, more general and safer.

第三,对问题的描述是除非添加 \,否则不会生成任何输出。 \n 字符。正如我所解释的,这是完全正常的。通过重新阅读您的帖子,我可以理解,您不会理解为什么输入文件中已经有一篇文章,所以您必须添加一篇。之所以需要添加它,是因为 getline()函数会丢弃 \n 字符。它不会插入到行的缓冲区中。

Third, the description of your problem is that no output is generated unless you add a \n character. As I explained, this is perfectly normal. From re-reading your post, I can understand that you don't unhderstand why you should have to add one given that there was already one in the input file. The reason you need to add it is because the getline() functions discard the \n character. It is not inserted into your line's buffer.

我已经清理了一些代码,以向您展示一些明显的改进。从这段代码中,您将能够了解程序的结构,它也应该反映您输入的结构。

I've cleaned up some of your code to show you some clear improvements. From this code you will be able to understand the structure of your program, which should also reflect the structure of your input.

#include "truthTable2.h"

int main(int argc, const char* argv[]){
  std::ifstream inFile;
  if(argc != 2){
    cout << "Enter an input file name: ";
    std::string inFileName;
    std::getline(std::cin, inFileName);
    inFile.open(inFileName.c_str());
  }
  else {
    inFile.open(argv[1]);
  }
  if ( !inFile.is_open() ) {
      // Did not successfully open a file. Print error message and exit!
  }
  TruthTable tTable;
  for (std::string variables; std::getline(inFile,variables); )
  {
    std::cout << variables << std::endl;
    tTable.setVariables(variable);
    std::string formula std::getline(formula);
    std::cout << formula << std::endl;
    tTable.setFormula(formula);
    tTable.printTable();
  }
  return 0;
}

由此,我有一个问题:您的输入结构如何?您的输入文件仅包含2行吗?这些线对是否有多套?是否有单行包含变量和一堆方程式?这三种情况将导致我以下列方式之一重新构建程序:

From this, I have a question:how is your input structured? Is your input file only consisted of 2 lines? Are there multiple sets of these line pairs? Is there a single line with variables and a bunch of equations? These three cases will lead me to re-structure the program in one of the following fashions:

仅2行

ThruthTable table;
std::string variables, equation;
std::getline(file, variables);
std::getline(file, equation);
// ...

多套

while ( !inFile.eof() )
{
    ThruthTable table;
    std::string variables, equation;
    std::getline(file, variables);
    std::getline(file, equation);
    // ...
}

多个方程式

ThruthTable table;
std::string variables;
std::getline(variables);
for ( std::string equation; std::getline(file, equation); )
{
    std::getline(file, equation);
    // ...
}

这篇关于没有在C ++中没有换行符的情况下无法打印字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆