Ok, what I want to do is pretty simple but I'm doing something wrong because it won't work. I have a small text file that looks like so:
032032002
FILENAME01.EXT
FILENAME02.EXT
basically the top line has 3 integer values in it, and the last one is the number of files to read. Then after that line there are filenames listed in the order in which they should be opened. My code currently uses fopen along with fgets to get the appropriate strings. When I run a test program just to see if the correct strings are being read this is what i get for output:
int1: 32
int2: 32
int3: 2
file0: FILENAME01.EXT
file1: FILENAME02.EXT
So i know I'm reading the correct number of bytes, but in the code, when I try and make another call to fopen using one of the filenames from the file, it will give me the error that the file doesn't exist, whereas I know for a fact that it does as well as that if I just type the name of the file in directly it works just fine.
Here's some sample code:
Code:
void testIO(char * filename)
{
FILE * tempfile = 0;
FILE * tempfile2 = 0;
intone = 0;
inttwo = 0;
intthree = 0;
char temptxt[100];
char * temptxt2 = 0;
if ((tempfile = fopen(filename, "r")) != NULL)
{
// read in the first integer
if( fgets( temptxt, 4, tempfile ) != NULL)
{
intone = atoi(temptxt);
printf( "int1: " );
printf( "%i", intone);
printf( "\n" );
}
// read in the second integer
if( fgets( temptxt, 4, tempfile ) != NULL)
{
inttwo = atoi(temptxt);
printf( "int2: " );
printf( "%i", inttwo);
printf( "\n" );
}
// read in the third integer
if( fgets( temptxt, 5, tempfile ) != NULL)
{
printf( "int3: " );
intthree = atoi(temptxt);
printf( "%i", intthree);
printf( "\n" );
}
for(int i=0; i < (intthree); i++)
{
//read the line
temptxt2 = new char[100];
if( fgets( temptxt2, 100, tempfile ) != NULL)
{
//put in the null character at the end
for(int x=0; x<100; x++)
{
if(temptxt2[x] == '\n')
temptxt2[x] = '\0';
}
printf( "file" );
printf( "%i", i);
printf( ": ");
printf( "%s", temptxt2);
printf( "\n" );
//OPEN FILE HERE THIS IS WHERE I GET THE ERROR
if((tempfile2 = fopen(temptxt2, "r")) == NULL)
printf( "ERROR FILE DOESN't EXIST" );
else
//DO SOMETHING WITH THE DATA IN IT
}
delete temptxt2;
temptxt2=0;
}
fclose(tempfile);
}
}
Yeah, I keep getting an error there and I really don't know why. At first I thought it was because there was a whole bunch of unused space at the end of the string(temptxt2) so I changed all the filenames to be the same length and updated the program so it always put the null character in the same place(because the same place in all the strings was the last character in the string). Anyway though, help will be greatly appreciated, and thank you very much in advance.