我想编写一个简单的程序,它从用户输入中收集数据以保存到 txt 文件中。我找到了几种收集和保存数据的方法,但我找不到将用户输入中的不同实例写入 txt 文件的同一行的方法。这是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
char book[30];
char author[30];
char quote[64];
ofstream myfile;
myfile.open ("myfile.txt", ios::in | ios::ate);
if (myfile.is_open())
{
cout << "Enter the name of the Book: ";
fgets(book, 30, stdin);
cout << "Enter the name of the Author: ";
fgets(author, 30, stdin);
cout << "Type the quote: ";
fgets(quote, 64, stdin);
myfile << ("%s;",book) << ("%s;",author) << ("%s;",quote);
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
文件上的输出是:
Book01
Author01
"This is the quote!"
我想在同一行:
Book01; Author01; "This is the quote!"
感谢您的帮助和关注!