D
dough
Heres a snippet of my code. I am trying to dynamically allocate memory
for reading in strings from a file.
FILE *f; /* file to read */
char *s; /* string being read */
f = "somefile.txt";
s = malloc(sizeof(char*)); /* allocates mem of string */
while( fscanf(f, "%s", s) != EOF )
{ /* read from FILE */
realloc(s, strlen(s)+1); /* reallocate that mem */
foo(s) /* do something with s */
}
free(s);
When complied, I get a segmentation error which means my allocation is
invalid somewhere. Please take a look and provide any comments.
Thanks.
for reading in strings from a file.
FILE *f; /* file to read */
char *s; /* string being read */
f = "somefile.txt";
s = malloc(sizeof(char*)); /* allocates mem of string */
while( fscanf(f, "%s", s) != EOF )
{ /* read from FILE */
realloc(s, strlen(s)+1); /* reallocate that mem */
foo(s) /* do something with s */
}
free(s);
When complied, I get a segmentation error which means my allocation is
invalid somewhere. Please take a look and provide any comments.
Thanks.