Hey fellow programmers
i programmed a connect 4 game in c++ using an array for the field.
I have a function that saves after the input of a player the whole array in a text document
I can also succesfuly read the file, but i am having problems converting string to int. (i used atoi and other stuff but in didn't work.)
So the programm has to read the content of the file and put every number to 1 field in the 2 dimensional array.
Spielstand= save state
sZahlen = string numbers
code to write:
Code:
void vSpielstandSpeichern(int iFeld[GR_ARRAY][GR_ARRAY])
{
ofstream outfile;
outfile.open ("viergewinntSpielstand.txt");
for (int x = 1; x < GR_ARRAY; x++){
for (int y = 1; y < GR_ARRAY; y++){
outfile<<iFeld[x][y]<<endl;
} // end for
} // end for
outfile.close();
}
here is the code to read:
Code:
void vSpielstandLaden(int iFeld[GR_ARRAY][GR_ARRAY])
{
// Define constants.
int iZahl=0;
// Open input file.
ifstream Spielstand; // Declare an input file object.
Spielstand.open("viergewinntSpielstand.txt", ios::in); // Open the input file in project folder.
if (!Spielstand) // See if the file was opened sucessfully.
{
cout << "file can not be read!\n";
}
cout << "Spielstand wird gelesen...\n";
string sZahlen;
getline(Spielstand, sZahlen);
while (!Spielstand.fail())
{
for (int x = 1; x < GR_ARRAY; x++){
for (int y = 1; y < GR_ARRAY; y++){
iZahl= atoi( sZahlen);
iFeld[x][y]= iZahl;
getline(Spielstand, sZahlen);
}}
}
if (Spielstand.fail() && !Spielstand.eof())
{
cout << "Error while reading file. Aborting!\n";
Spielstand.close();
}
i am very gratefull for any help'