入力を受け取ってテキストファイルに出力し、デジタルジャーナルのように、作成された日付の名前を付けるプログラムを作成しています。ただし、VSのf5がエラーなしで起動し、すべてが順調であるかのように実行され、ファイルが作成されているかどうかを確認すると、projectsフォルダーのどこにも見つかりません。助けてくれてありがとう。
string date;
time_t now;
struct tm nowlocal;
now = time(NULL);
nowlocal = *localtime(&now);
int year = nowlocal.tm_year + 1970;
date = to_string(nowlocal.tm_mday) + ":" + to_string(nowlocal.tm_mon) + ":" + to_string(year) + ".txt";
char write[900];
ofstream file;
file.open(date);
cout << "input text to write to the journal(900chars max):";
cin >> write;
file << write << endl;
file.close();
あなたの主な問題はあなたの命名規則だったと思います。:
ファイル名で許可されていないため、ファイルは作成されていませんでした。他にもいくつかエラーがありました。また、char配列をいじる必要があります。
#include <iostream>
#include <ctime>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string date;
time_t now;
struct tm nowlocal;
now = time(NULL);
nowlocal = *localtime(&now);
int year = nowlocal.tm_year + 1970;
date = to_string(nowlocal.tm_mday) + "_" + to_string(nowlocal.tm_mon) + "_" + to_string(year) + ".txt"; //changed all ':' to '_'
//char write[900];
string write;
cout << date << endl;
ofstream file(date);
//file.open(date); //this was also causing an error
//your file is already open
cout << "input text to write to the journal(900chars max): ";
getline(cin, write);
//cin >> write;
file << write << endl;
file.close();
system("PAUSE");
return 0;
}
いくつかのエラーを修正しました。今は動いているようです。使用していた配列の代わりに文字列を使用する必要がありました。これをクラスで行ったので、私のコードでいくつかのエラーが予想されます。うまくいけば、これが役に立った!幸運を!
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加