1, a pointer (here "pointer") points to a data stream to a file is located. 2, A variable (in this case "m)", then how many numbers there are. 3 m to retire pointer back to the beginning point also. 4 data output. But the code does not work as-planned. The is the first value only. Who can help me? Thanks Code: # include <stdio.h> # Include <stdlib.h> int main () ( char * pointer, unsigned char m = 0; FILE * file, file = fopen ("data.txt", "rt") while ((fscanf (file, "% d", pointer))! = EOF) ( pointer + +; m + +; ) fclose (file); pointer = pointer - m, / / Pointer to withdraw while ((* pointer)! = NULL) ( printf ("% d \ t", * pointer) / / output data pointer + +; ) )
pointer has no memory! should you allocate memory with malloc, since otherwise the pointer showing nirvana! nirvana and the data write is not good
Did you "pointer = (char *) malloc (sizeof (char ));"? I have used, but that does not help. Can you give me the full code? Thanks
malloc (sizeof (char)) provides you space for a char ... you need but probably more ... So it must malloc (n * sizeof (char)) where n is the number is called.
char instead of int Why? You read% d end of ints. Have you ever thought of using a tuned array? Why do you break from the output at 0? Why do you write NULL instead of 0? sizeof (char) is always a way, and should never be explicitly written out. The return value of malloc should not cast according to some experts, are also, in any case, it's at least useless.
Hi, I have rewritten the program. Seems to be better. The only problem is I get a unendlose loop. So the "\ 0" is definitely not true, but what is the right one? What is the difference btwn NULL, EOF and "\ 0"? Code: # include <stdio.h> # Include <stdlib.h> int main () ( int * pointer, unsigned char m = 0; FILE * file, int character file = fopen ("data.txt", "rt") while ((fscanf (file, "% d", & sign) ) = EOF) ( (pointer + m) = sign *, m + +; ) fclose (file); m = 0; while ((pointer + m)! = '\ 0') ( printf ("% d \ t" , * (pointer + m)) m + +; ) )
hand still has no memory: ( Hoer times n bit Bashar. NULL is the same as (void *) 0 - and is usually used to say that this pointer is invalid '. " EOF - End Of File, indicates that the file is zueende. "\ 0" is a string (char *) with the following contents, '\ 0' "\ 0 'well what do you think is more "\ 0 'which has the value 0 and indicates the end of the strings!