Junk value from string using C -


i'm not sure what's going on program, have function that's trying read bunch of strings file , passes main further processing. here's code:

char* readfile(file* fpfile) { //  local declaration char *ptr; char temp[1000];  //  statment fgets(temp, sizeof(temp), fpfile); ptr = temp;  return ptr; }// readfile 

the problem occurs once function passes pointer main. tried printing string, first few lines correct , after it's junk. also, if print ptr in function readfile prints fine , pointer that's being passed main prints fine also. there missing? appreciated.

here's output i'm getting

2000 1990 new york-no. nj; 21199865 19549649 los angeles area; 16373645 14531629 chicago area; 9157540 8239820 washington-baltimore; 7608070 6727050 san francisco area; 7039362 6253311 philadelphia-atlantic city area; 6188463 5892937 boston\240\365\277_\377 

there's supposed twice amount of inputs, it's stoping quarter of way.

char temp[1000]; local. when control goes out of readfile(), memory may allocated other purpose. there 2 ways, either storage may global array or dynamically allocated memory using malloc().

char* readfile(file* fpfile) { //  local declaration char *ptr = malloc(1000);  //  statment fgets(ptr, 1000, fpfile);  return ptr; }// readfile 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -