ユーザー入力からデータを収集して 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!"
助けてくれてありがとう!