Hi,
I've written this simple program to get some data from a text file. It reads in data to a string, an int array, another int array, and a double array. The reason there are 2 int arrays is because one of them was originally going to be for unsigned ints only. I changed it back to int because I wasn't sure if it was causing a problem with the program (although I don't think it would now).
Ultimately, the string and the arrays are just temporary, obviously. All of the data read in will be used to build an object of a player class after each of the variables has been filled.
Anyway basically the reason I'm posting is because I get this bug every time I run the program. It says "the stack around the variable data1 is corrupted". Anyone know why?
Also, are there many ways I could make this program better? I'm guessing there are ways to not use c strings as much.
Thanks.
The "players.txt" text file is the following:
Quote:
List of players in the game and their attributes.
1. Arnold, 0, 0, 0, 0, -1, 1, 4, 5, 0, 0
2. Barry, 1, 480, 240, 2, 5, 8, 1, 1, 8, 10.5
3. Colin, 2, 480, 240, 2, 9, 1, 7, 8, 2, 10.5
4. Dave, 3, 480, 240, 2, 3, 8, 5, 2, 9, 10
5. Edmund, 4, 480, 240, 2, 9, 7, 8, 1, 9, 10
Code:#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream file_in("players.txt");
cout << "Reading player data from players.txt" << endl << endl;
char line_from_file[499];
char data_from_str[7];
string str_from_file;
string name;
int data1[3];
int data2[6];
double data3;
int pos = 0;
int j = 0;
while (!file_in.eof())
{
for (pos = 0; pos < 498; pos++)
line_from_file[pos] = ' ';
file_in.getline(line_from_file, 499); // terminates the string with '\0'
// Check if line_from_file begins with a number. If not, don't check for data.
if (line_from_file[0] >= 48 && line_from_file[0] <= 57)
{
str_from_file.clear();
name.clear();
// If necessary, skip past the second digit of the number at the start of each line.
// Pos ends up being 3 (1 digit numbers/ 0-9) or 4 (2 digit numbers/ 10+).
if (line_from_file[1] >= 48 && line_from_file[1] <= 57)
pos = 4;
else
pos = 3;
while (line_from_file[pos]) { // space=32, endofstring='\0'=0
str_from_file += line_from_file[pos];
pos++;
}
pos = 0;
// remove all the white space from str_from_file
while (str_from_file.find(' ') != string::npos) // gets rid of spaces
str_from_file.erase(str_from_file.find(' '), 1);
while (str_from_file.find(9) != string::npos) // gets rid of tabs
str_from_file.erase(str_from_file.find(9), 1);
// Read upto the first comma- the name.
while (str_from_file[pos] != ',')
{
name += str_from_file[pos];
pos++;
}
cout << name << " ";
// Read upto each of the following commas- the rest of the data.
pos = 0;
for (int i = 0; i < 10 && str_from_file[i]; i++)
{
while (str_from_file[pos] != ',' && str_from_file[pos])
pos++;
pos++;
data_from_str[0] = '\0';
for (j = 0; j < 7 && str_from_file[pos+j] != ',' && str_from_file[pos+j]; j++)
data_from_str[j] = str_from_file[pos+j];
// if a non-number is found, terminate data_from_str with a null.
if (str_from_file[pos+j] < 48 || str_from_file[pos+j] > 57)
data_from_str[j] = '\0';
cout << data_from_str << " "; //output what was read into the char array.
if (i <= 2)
data1[i] = atoi(data_from_str);
else if (i <= 8)
data2[i] = atoi(data_from_str);
else if (i == 9)
data3 = atof(data_from_str);
}
cout << endl;
}
}
file_in.close();
file_in.clear();
}