I’m new to C and am trying to use the read function. I want to take what’s in the buffer (tempChar) and put it in another char array (str). This is so I can run the read function again and add on to str later (because tempChar will be rewritten by the 2nd read function). Like this:
char tempChar;
read(0, &tempChar, 10);
char *str;
str= (char*) malloc(10);
memcpy(str, &tempChar, fileSize); /*I'm doing something wrong here*/
All this so I can rerun:
read(0,&tempChar, 1);
str= realloc(str, 11);
str[10]=tempChar;
It compiles fine, but it gives me a segmentation fault when I actually try to run it.
Any ideas? Thanks a bunch.
you need to have enough storage to store the 10 characters you are reading
you declared
which can hold 1 character.
Instead declare tempChar as