私はtxtファイルでC++で動作するのに問題があります。まず、クラスと関数を持つ .cppと.hファイルを持つプログラムを作りたいと思います。テキストの5行(選手名)が含まれていファイルを扱うC++
例えば、私はtxtをしているファイル:
は、だからここに私の問題です。ですから、私はそのtxtのすべての行を文字列の変数にしたいと思います。しかし、私がその新しい変数を使用したい限り、それらはうっかり消えます。ここで
は、プログラムコードです:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
int i;
string player[5];
ifstream myfile ("1-Efes Pilsen.txt");
if (myfile.is_open())
{
while (myfile.good())
{
for (i=0;i<5;i++)
{
getline (myfile,line);
player[i] = line;
}
// after this point I still can use new variables
}
}
else cout << "Unable to open file";
cout << player[1]; // <--- NOT WORKING. WHY?
myfile.close();
}
「作業していない」とはどういう意味ですか? –