Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
char tdfilename[4][58];
char idfilename[4][58];


Function that causes program to crash during execution:
(alphabet characters added for anonymity)

C++
void ReadEightFileNames()
	{
	// this function will read a text file with eight lines (file names) in it. ("filenames.txt")
	// NOTE: filenames.txt must be in the same directory as this executable.
	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);
	// Prefix all eight files with folder name (folder must exist!)
	// string base_path = "c:\\ABCD\\ABCDEFGHIJ\\ABCDEFGHI\\";
	for(d=0;d<=3;d++)
		{
		for(i=0;i<=28;i++)
			{
			tdfilename[d][i] = idfilename[d][i] = base_path.at(i);
			}
		}
	// Read eight lines from filenames.txt and store characters to idfilename[4][58] and tdfilename[4][58]
	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();
	// Include null character to all eight file names
	for(d=0;d<=3;d++)
		{
		tdfilename[d][57] = idfilename[d][57] = '\0';
		}
	// Display files on screen for verification
	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):
C++
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. :)
Posted
Updated 21-Feb-12 15:58pm
v2
Comments
Sergey Alexandrovich Kryukov 21-Feb-12 18:25pm    
True, this is not much of C++. You should start from explanation of you goal and basic description of your data structures, semantically, not just technically. You should also explain your problem.

The code as is not quite suitable even for analysis. I would start with the following: remove each and every immediate constant like 57, 29, "filenames.txt" (why this is 8 files? this is just one...), and turn it all in a set of explicitly declared constants. If you start from hard-coding, you have a risk to get sunk into hopeless support problems and never come to the end. What of you need to change your 29 into 27? What, will you change it throughout the code? Besides, the eyes get sick to see this...

Fix it, also fix the formatting (look at the result; probably you forget to escape some HTML entities) and come back. Use "Improve question". Please answer all the questions.

--SA
CPallini 22-Feb-12 6:09am    
"By changing "char line[28]" to "char line[29]" the function no longer causes program crash. But now to understand why.."
Because of (from documentation):
The ending null character that signals the end of a c-string is automatically appended to s after the data extracted.

1 solution

You have declared line as
char line[28]

then
why 29 in the following line?
inFile.getline(line,29);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900