Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why does it print junk characters ?

Why does it print junk characters ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krish_kumar
    wrote on last edited by
    #1

    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 

    O 1 Reply Last reply
    0
    • K krish_kumar

      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 

      O Offline
      O Offline
      oggenok64
      wrote on last edited by
      #2

      while(ch = getchar() != EOF) should be while((ch = getchar()) != EOF) The assignment operator has lowest precedence of all in C.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups