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)
What are 'lengthPtr' and 'textFilePtr' for? They aren't used anywhere
in this function.
{
FILE *ifp;
char *text;
char txtFile[FILELENGTH];
Where's FILELENGTH defined (and what to)?
char c;
int x = 0, y = 0, items;
What's 'items' good for? It's never used.
printf ("Enter the name of the file to analyze : ");
fgets (filename, FILES, stdin);
Where is FILES defined (and what to)? Is what 'filename' is pointing
to memory you "own" (i.e. it's either an array of chars you defined
in the caller or is memeory you received from malloc() or friends)
and has it at least room for FILES characters?
Are you aware that, if the file name isn't longer than (FILES - 2),
there's going to be a trailing newline character, which rather likely
isn't in the name of the file you want to open?
ifp = fopen(filename, "r");
text = malloc(FILELENGTH * sizeof(char)); **I keep
getting Segmentation fault here**
If you get a segentation fault here then you must have had some error
when dealing with memory somewhere else in your program, probably
accidentally overwriting some of the bookkeeping information the
malloc()-family of functions needs to store. One possible place is
where you use fgets() above (if 'filename' isn't pointing to memory
large enough to hold FILES characters - but it also could be
happening in some other part of the program you're not showing.
And debugging something one can't see is a very difficult task;-)
Moreover, why made you 'text' first point to the 'txtFile' array and
now suddenly assign a different value to it? Moreover, what's the
malloc() good for at all? The 'text' variable is never used in the
following, so you simply can delete this line - all you do here is
create a memory leak since you don't even bother to free() the memory
later on.
if(ifp == NULL)
{
fprintf (stderr, "Error opening file\n");
exit (-2);
}
while (fscanf (ifp, "%c", &c)!= EOF)
{
sprintf (txtFile, "%c", c);
Do you really want to overwrite the first character of the 'txtFile'
array again and again? I have some inkling that you actually meant
to write
sprintf( txtFile + y, "%c", c);
But then you need to make sure that you never try to read more than
FILELENGTH characters from the file, even if the file is longer
than that...
I would recommend to use variable names that make a bit more sense,
here e.g. 'read_count' would help to make things easier to under-
stand.
for (x = 0; x < FILELENGTH; x++);
{
printf ("%c", txtFile[x]);
With your version (i.e. using only the first element of the
'txtFile' array) everything beside the first character you print
out will be garbage. But even if you correct that as pointed out
above and the number of chars you could read from the file was
less than FILELENGTH chars then you will still output garbage if
'x' beomes equal or larger than 'y'
The whole function looks like you have started with something that
didn't work and then you went on fiddling with the code without
understanding why it didn't work. Now it's in a state that there's
one strange thing after another and it's going to be hard to untangle
the mess. I would recommend that you just comment it out, take a
deep breath, write down (in words) exactly what the function is
supposed to do and then start again from scratch, not looking at
what you already have written. Also write a minimal program that
just calls your function, nothing else, so you can test it without
being led astray by possible errors in other parts of the program.
If this new version doesn't work don't go messing around with it
more or less randomly but come back here and let's have a look
(and also post what you wrote down the function should do and the
short program you embedded it in to test it).
Best regards, Jens