したがって、ファイル名を受け取る関数を含む.cppファイルがあり、ファイルの内容を含む文字列を返す必要があります(実際には内容を変更し、コードを変更してより理解しやすくしましたが、それは何の影響もありません私の問題)。問題は、それf.good()
が戻っfalse
てきて、ファイルを読み取るループが機能していないことです。コード :
#include "StdAfx.h"
#include "Form21.h"
#include <string>
#include <fstream>
#include <iostream>
string ReadAndWrite(char* a){
char filename[8];
strcpy_s(filename,a);
string output;
char c;
ifstream f(filename,ios::in);
output+= "Example text"; // <-- this writes and returns just fine!
c = f.get();
while (f.good())
{
output+= c;
c= f.get();
}
return output;
}
なぜこれが起こっているのか誰にも分かりますか?これは別の.cppファイルであることに関係がありますか(削除してもエラーは発生しません#include <fstream>
)。たぶん、ループを作る別の種類の方法がありますか?これを修正する方法や、目標を達成するための別の方法についての提案をお待ちしております。