I will preface this by saying that I am an amateur programmer, and do not currently have a well-rounded knowledge of C++. So please go easy on me.. :)
I've been writing a small program, part of which reads file name strings from a file and stores them to global char arrays.
global char arrays:
char tdfilename[4][58];
char idfilename[4][58];
Function that causes program to crash during execution:
(alphabet characters added for anonymity)
void ReadEightFileNames()
{
using namespace std;
char fname[14] = {'f','i','l','e','n','a','m','e','s','.','t','x','t','\0'};
char line[28];
int i,d;
ifstream inFile;
inFile.open(fname);
for(d=0;d<=3;d++)
{
for(i=0;i<=28;i++)
{
tdfilename[d][i] = idfilename[d][i] = base_path.at(i);
}
}
for(d=0;d<=3;d++)
{
inFile.getline(line,29);
for(i=0;i<=27;i++)
{
idfilename[d][i + 29] = line[i];
}
inFile.getline(line,29);
for(i=0;i<=27;i++)
{
tdfilename[d][i + 29] = line[i];
}
}
inFile.close();
for(d=0;d<=3;d++)
{
tdfilename[d][57] = idfilename[d][57] = '\0';
}
cout << "EIGHT FILE NAMES:" << endl;
for(d=0;d<=3;d++)
{
for(i=0;i<=56;i++)
{
cout << idfilename[d][i];
}
cout << endl;
for(i=0;i<=56;i++)
{
cout << tdfilename[d][i];
}
cout << endl;
}
}
The contents of "filenames.txt" is exactly eight lines, always formatted for 28 characters per line, and looks like this (Xs added for anonymity):
XXXX_XX_XXX_XX_XXXXXXXXX.txt
XXXX_XX_XXX_XX_XXXXXXXXX.txt
XXXX_XX_XXX_XX_XXXXXXXXX.txt
XXXX_XX_XXX_XX_XXXXXXXXX.txt
XXXX_XX_XXX_XX_XXXXXXXXX.txt
XXXX_XX_XXX_XX_XXXXXXXXX.txt
XXXX_XX_XXX_XX_XXXXXXXXX.txt
XXXX_XX_XXX_XX_XXXXXXXXX.txt
Visual C++ 2010 Express command-line compiler (which I'm using with a batch file/make file for compiling and linking) throws no errors during compilation.
However, the entire function executes, but crashes upon returning to the calling function. I cannot find the reason why it does this.
(I realize that my coding method is very antiquated and reflects a lack of knowledge of some basic C++ practices. If you have any suggestions, please tell me.)
Compilation Platform: WinXP Pro SP3
Targeted Platforms: WinXP,Vista,7
IDE: MS VS/C++ 2010 Express
EDIT NOTES:
1) Added comment prefix to line that begins "string base_path = " to avoid HTML confusion.
2) Character array "eight_files" renamed to "fname". (more appropriate, less dubious)
3) Will work on changing from immediate constants to explicitly declared constants (using const int).
4) Will work on explaining intent of program and data structures.
ERROR FOUND:
1) By changing "char line[28]" to "char line[29]" the function no longer causes program crash. But now to understand why.. No doubt using constants instead of hard-coded numbers would have avoided this. I see your point SAKryukov.. Thank you for your patient and tactful reply. :)