By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Seg Fault. help please

P: 9
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;
}
Nov 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: 9
figured it out....thx anyways
Nov 6 '06 #2

P: 2
figured it out....thx anyways
Its working fine
just execute this code.
#include <stdio.h>
#include <stdlib.h>

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;
}
int main()
{

int result;
char str[]="text";
char *ptr;
ptr = ReadString(str,&result);
printf("ptr=%s\n",ptr);

}
Nov 7 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.