Here is a code I am writing. I keep geting segmentation fault.
I'm not sure what i'm doing wrong.
Can you explain it to me in PLAIN ENGLISH??
void ReadString (char *filename, int *lengthPtr, char *textFilePtr)
{
FILE *ifp;
char *text;
char txtFile[FILELENGTH];
char c;
int x = 0, y = 0, items;
text = txtFile;
printf ("Enter the name of the file to analyze : ");
fgets (filename, FILES, stdin);
printf ("%s", filename);
ifp = fopen(filename, "r");
text = malloc(FILELENG TH * sizeof(char)); **I keep
getting Segmentation fault here**
if(ifp == NULL)
{
fprintf (stderr, "Error opening file\n");
exit (-2);
}
while (fscanf (ifp, "%c", &c)!= EOF)
{
sprintf (txtFile, "%c", c);
y++;
}
for (x = 0; x < FILELENGTH; x++);
{
printf ("%c", txtFile[x]);
}
fclose (ifp);
}
I'm not sure what i'm doing wrong.
Can you explain it to me in PLAIN ENGLISH??
void ReadString (char *filename, int *lengthPtr, char *textFilePtr)
{
FILE *ifp;
char *text;
char txtFile[FILELENGTH];
char c;
int x = 0, y = 0, items;
text = txtFile;
printf ("Enter the name of the file to analyze : ");
fgets (filename, FILES, stdin);
printf ("%s", filename);
ifp = fopen(filename, "r");
text = malloc(FILELENG TH * sizeof(char)); **I keep
getting Segmentation fault here**
if(ifp == NULL)
{
fprintf (stderr, "Error opening file\n");
exit (-2);
}
while (fscanf (ifp, "%c", &c)!= EOF)
{
sprintf (txtFile, "%c", c);
y++;
}
for (x = 0; x < FILELENGTH; x++);
{
printf ("%c", txtFile[x]);
}
fclose (ifp);
}
Comment