Hi guys/gals,
First post, read the faq etc... anyways... sorry for my lack of knowledge in C... I haave a "CSV" file
that I am trying to read and locate text from... so far the reading part is not the problem.. according to valgrind all is well... untill I decide to use strstr() for my word allocation.... now this is were it keeps failing... I understand the error.. but I can not ping point how to fix it it... here is what I get from valgrind
Code:
==5492== Invalid read of size 1
==5492== at 0x4C25EC2: strlen (mc_replace_strmem.c:275)
==5492== by 0x4E92EDF: puts (in /lib64/libc-2.9.so)
==5492== by 0x4008EC: main (fieldFingers.c:49)
==5492== Address 0x0 is not stack'd, malloc'd or (recently) free'd
and here is my code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct fp_fields {
char *f1;
};
int resetFile (FILE *fp)
{
int z;
fseek (fp, 0, SEEK_END);
z = ftell (fp);
fseek (fp, 0, SEEK_SET);
return z;
}
int main (void)
{
int fsize;
char *f1, *buffer;
char *fname = "users.CSV";
FILE *fpntr;
if ((fpntr = fopen (fname, "r")) == NULL)
{
printf ("Error: %d\n", errno);
return EXIT_FAILURE;
}
fsize = resetFile (fpntr);
buffer = malloc (fsize * sizeof (int) + 1);
while (fgets (buffer, 1024, fpntr) != NULL)
{
printf ("%s\n", strstr (buffer, "USERS"));
//rfp_fields (buffer);
}
printf ("total bytes: %d\n", fsize);
free (buffer);
fclose (fpntr);
buffer = '\0';
return EXIT_SUCCESS;
}
anyways...I decided to use malloc on buffer cause I though that maybe that would help, but I guess not... sorry for my stupid code, its something that I am trying to do for work and trying to get in to C at the same time... as I did long ago... I also have a K&R book.. that is great finished reading it