Why does it print junk characters ?
-
The following code snippet read data from keyboard and write into one file; and read that file and display it on screen....But it displays junk characters.... What may be the problem ...? // main.c #include int main(void){ char ch; FILE *fp; fp = fopen("test_file_src","w"); printf("\n Eneter the data (type Ctrl^D at end :\n"); if (fp != NULL) { while(ch = getchar() != EOF) fputc(ch ,fp); } else { printf("\n Unable to create file \n"); } fclose(fp); printf("\n\n Reading from file\n\n "); fp = fopen("test_file_src","r"); if (fp != NULL) { while(!feof(fp)) { // Move up to end of file ch = fgetc(fp); putchar(ch); } } else { printf("\n Unable to process file \n"); } fclose(fp); // Close file printf("\n\n\n"); return 0; } /* Output Enter the data (type Ctrl^D at end : 11111111111111222 2222222222222222222 22222222222222222 Reading from file
-
The following code snippet read data from keyboard and write into one file; and read that file and display it on screen....But it displays junk characters.... What may be the problem ...? // main.c #include int main(void){ char ch; FILE *fp; fp = fopen("test_file_src","w"); printf("\n Eneter the data (type Ctrl^D at end :\n"); if (fp != NULL) { while(ch = getchar() != EOF) fputc(ch ,fp); } else { printf("\n Unable to create file \n"); } fclose(fp); printf("\n\n Reading from file\n\n "); fp = fopen("test_file_src","r"); if (fp != NULL) { while(!feof(fp)) { // Move up to end of file ch = fgetc(fp); putchar(ch); } } else { printf("\n Unable to process file \n"); } fclose(fp); // Close file printf("\n\n\n"); return 0; } /* Output Enter the data (type Ctrl^D at end : 11111111111111222 2222222222222222222 22222222222222222 Reading from file