如果没有C++中的换行符,则不会打印字符串

And*_*der 2 c++ printf cout char ifstream

我正在打开一个文件,并从中获取行.第一行应该说明有多少变量,以及它们的名称是什么.第二行应该是使用这些变量的逻辑方程.赋值是打印出变量和方程的真值表.

程序进入的第一行不是在没有插入新行字符的情况下打印.我尝试转换为字符串并使用printf和cout.

输入所有内容的主文件:

#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;
}
Run Code Online (Sandbox Code Playgroud)

样本输入:

2 x y
( \wedge x ( \not y ) )
Run Code Online (Sandbox Code Playgroud)

输出来自:

 ( \wedge x ( \not y ) )
Run Code Online (Sandbox Code Playgroud)

我认为造成这种情况的任何原因都会给我带来问题.在我对variableLine进行标记后,它不会在没有换行符的情况下打印,并且在评估公式时找不到第二个变量.

And*_*ron 5

一个std::ostream的输出需要被刷新.通常在\n写入换行符时自动刷新.如果要强制流刷新,可以std::flush像这样使用操纵器:

std::cout << "foo" << std::flush;
Run Code Online (Sandbox Code Playgroud)

编辑:虽然我的帖子清楚地回答了这个问题"除非输出一个\n字符,否则为什么我的线条不显示?" 你说这不能回答你的问题,所以我会尝试一些心灵阅读,试着回答你真实的问题.

由于我不知道你真正想知道什么,我会在这里指出你的代码有些错误,它可能会帮助你找到你的问题或澄清你的问题.

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().键入更短,更通用,更安全.

第三,问题的描述是除非添加\n字符,否则不会生成输出.正如我解释的那样,这是完全正常的.从重新阅读你的帖子,我可以理解你不会理解为什么你应该添加一个,因为输入文件中已经有一个.您需要添加它的原因是因为getline()函数会丢弃该\n字符.它没有插入你的行的缓冲区.

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

#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;
}
Run Code Online (Sandbox Code Playgroud)

由此,我有一个问题:您的输入结构如何?您的输入文件只包含2行吗?这些线对有多组吗?是否有一行包含变量和一堆方程式?这三个案例将引导我以下列时尚之一重新构建该计划:

仅限2行:

ThruthTable table;
std::string variables, equation;
std::getline(file, variables);
std::getline(file, equation);
// ...
Run Code Online (Sandbox Code Playgroud)

多套:

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

多个方程式:

ThruthTable table;
std::string variables;
std::getline(variables);
for ( std::string equation; std::getline(file, equation); )
{
    std::getline(file, equation);
    // ...
}
Run Code Online (Sandbox Code Playgroud)