How do i load a whole file in to an array?
Printable View
How do i load a whole file in to an array?
I didn't even need to read the post, your thread subject told me EVERYTHING I needed to know about your problem.Quote:
Probs
Read the rules and post properly next time.
Look up the ReadFile() function.Quote:
How do i load a whole file in to an array?
How do you want your array set up? You could have every element of the array be a character, a line, or a paragraph. You could even have something else depending on how your file is formatted. You need to be a helluva lot more specific. Posting code would be very helpful.
i'm assuming your data file is set up the way i have mine for the battle system... in that case...just modify that to fit your needs ;)Code:ifstream infile("stats.dat"); //open stats file
while(infile.getline(filename,20,' ')) //while more usernames, take it in
{
if(!strcmpi(filename,username)) //if filename matches
{
if(debug)
{
cout<<username<<"::"<<filename<<endl; //test string
system("pause"); //test pause
}
infile.ignore(100,'h'); //ignore password field
infile>>hp>>null>>atk>>null>>def>>null>>lvl>>null>>exp>>null
>>mon>>null>>points;
break; //end loop
}
else
infile.ignore(1000,'\n'); //ignore rest of line
}
infile.close(); //close file
Here is some code I've used before to read in a file into an array...maybe you'll find it usefull somehow:
Code:void readFromInputFile( char *text, // large array to store input text
long &index, // number of input file characters
char fileName[])// filename to use for input
{
ifstream inStream; // Input file stream
ofstream outStream; // Output file stream
// Set value of index to start at 0, in case we're calling this function a second time
index = 0;
// open input file
inStream.open(fileName); // associate the actual file name with "inStream"
if ( inStream.fail() ) {
cout << "Input file opening for " << fileName << " failed.\n"
<< "The file either doesn't exist or is not in the current directory.\n"
<< "Exiting...\n\n" << endl;
exit(-1);
}
/*
// open output file for debugging purposes
outStream.open("output.txt"); // associate the actual file name with "inStream"
if ( outStream.fail() ) {
cout << "Output file opening for output.txt failed.\n"
<< "The file either doesn't exist or is not in the current directory.\n"
<< "Exiting...\n\n" << endl;
exit(-1);
}
*/
// Read in the characters into our big array
while ( inStream.get(text[ index++]) ) {
if ( index >= MaxSize) {
cout << "MaxSize of " << MaxSize << " was reached.\n";
break;
}
// outStream << text[ index-1]; // for debugging
}
// close the input file
inStream.close();
}