This is a strage seg fault...i threw some comments in so you can see whats going on. Thanks!
any help would be appreciated.
/************************************************** ****************************
* Function: ReadString
*
* This function reads the entire text file into dynamically allocated memory,
* returns a pointer to the text as it's return value and returns the length
* via the parameter lengthPtr by reference.
*
* Inputs: filename, lengthPtr
* Outputs: arrayPamphlet
*
************************************************** ***************************/
char *ReadString (char* filename, int *lengthPtr)
{
char * arrayPamphlet;
FILE * pFile;
int x;
*lengthPtr = 0;
/*Get filename from user*/
printf("\n\n Please enter a file name:");
scanf("%s",filename);
/*SEG FAULT IS HERE IF A VALID FILENAME IS ENTERED*/
/* Open the file*/
pFile = fopen(filename, "r");
/*Print error if file is empty*/
if (pFile == NULL)
{
printf("Can not open input file \n\n");
exit(-1);
}
/*FILE EXITS HERE IF AN INVALID FILENAME IS ENTERED*/
/*Get the number of characters in the file*/
while (fgetc(pFile) != EOF)
{
*lengthPtr = *lengthPtr + 1;
}
/*use malloc to dynamically allocate space for arrayPamphlet*/
arrayPamphlet = (char *) malloc (*lengthPtr * sizeof(char));
/*Go back to beginning of file*/
rewind(pFile);
/*Read data from file into arrayPamphlet*/
for (x = 0; x < *lengthPtr; x++)
{
arrayPamphlet[x] = fgetc(pFile);
}
return arrayPamphlet;
}