简体   繁体   English

尝试将多个输出写入.txt文件C ++

[英]Trying to write multiple outputs to a .txt file c++

I want to output what is in the inputtwo.h into a txt document. 我想将inputtwo.h中的内容输出到txt文档中。 The document is able to be named fine. 该文档可以很好地命名。 but when I type in the inputs as asked, the return in the txt is one line of of text but in the wrong format 但是当我按要求输入输入内容时,txt中的返回内容是一行文本,但格式错误

so say I did two inputs, named it test.txt, then put in "dog cat" as the inputs, then the output in the txt is "dogcatcatdog" where it should be "dog" "cat" "dogcat" "catdog" each should be on their own line in the txt 假设我做了两个输入,将其命名为test.txt,然后将“ dog cat”作为输入,然后txt中的输出为“ dogcatcatdog”,其中应为“ dog”“ cat”“ dogcat”“ catdog”每个都应该在txt中单独显示

The following code is my main file 以下代码是我的主文件

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() 
{
char q1[20];
char q2[20];

int x;

printf("Select number of keywords (integer between 1-10): \n");
scanf("%d", &x);

switch(x){

    case 1:{
        string fileName;
    printf("Input Filename\n");
    cin >> fileName;
    fileName += ".txt";
    ofstream createFile;
    createFile.open(fileName.c_str(), ios::app);

    printf("Input One Keywords: \n");
    scanf("%s", q1);
    createFile << ("%s\n", q1);
    createFile.close();
    return 0;
}
    case 2:{
        string fileName2;
    printf("Input Filename\n");
    cin >> fileName2;
    fileName2 += ".txt";
    ofstream createFile;
    createFile.open(fileName2.c_str(), ios::app);

    printf("Input Two Keywords: \n");
    scanf("%s %s", q1, q2);
#include "inputtwo.h"
    createFile.close();
    return 0;
} 

the include inputtwo.h is 包含inputtwo.h是

#ifndef INPUTTWO_H
#define INPUTTWO_H

    createFile << ("%s\n", q1);
    createFile << ("%s\n", q2);
    createFile << ("%s" "%s\n", q1, q2);
    createFile << ("%s" "%s\n", q2, q1);

#endif /* INPUTTWO_H */

In inputtwo.h you are writing to file using C++ output (file) stream, which doesn't understand format specifiers in the form of "%s\\n". 在inputtwo.h中,您正在使用C ++输出(文件)流写入文件,该流无法理解“%s \\ n”形式的格式说明符。 ("%s\\n", q1) is an expression, where comma , is an operator (see this ). ("%s\\n", q1)是一个表达式,其中逗号,是操作者(见 )。 The result of this operator will be q1 and it is written to output stream (file). 该运算符的结果将为q1并将其写入输出流(文件)。

The overall result is that you write q1, q2, q1, q2, q2 and q1 without any separators between them. 总体结果是,您写q1,q2,q1,q2,q2和q1之间没有任何分隔符。

To fix, stop mixing C and C++ style output. 要解决此问题,请停止混合使用C和C ++样式的输出。 Use C++ style like this: 使用C ++样式,如下所示:

createFile << q1 << std::endl;
createFile << q2 << std::endl;  
createFile << q1 << " " << q2 << std::endl;
createFile << q2 << " " << q1 << std::endl;

Extending this to the rest of the program one would replace char q1[20]; 将其扩展到程序的其余部分将替换char q1[20]; with std::string q1; std::string q1; (same for q2) and then replace (与q2相同),然后替换

printf("Input One Keywords: \n");
scanf("%s", q1);

with

std::cout << "Input One Keyword: " << std::endl;
std::cin >> q1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM