output not showing properly in notepad ?

Discussion in 'Code Forum' started by C, Aug 25, 2008.

  1. C

    C New Member

    hello guys, <br />
    <br />
    here is another problem on filing. it creates the intended files and it is saving as well. but the problem is the numbers become invisible this time in those files. it is showing . . . (dots) instead of even numbers or odd numbers. Anyone has got any idea why it is showing like that? here is the log <br />
    my os is vista and i am using dev c++ <br />
    <br />
    #include<stdio.h><br />
    main()<br />
    {<br />
    FILE *f1,*f2,*f3;<br />
    int number,i;<br />
    printf("contents of DATA file \n\n");<br />
    f1=fopen("DATA","w");/*create data file*/<br />
    for (i=1;i<=30;i++)<br />
    {<br />
    scanf("%d",&number);<br />
    if (number==-1)break;<br />
    putw(number,f1);<br />
    }<br />
    fclose(f1);<br />
    f1=fopen("DATA","r");<br />
    f2=fopen("ODD","w");<br />
    f3=fopen("EVEN","w");<br />
    <br />
    while ((number=getw(f1))!=EOF)/*Read from DATA file*/<br />
    {<br />
    if(number%2==0)<br />
    putw(number,f3);/*"write a EVEN file"*/<br />
    else <br />
    putw(number,f2);/*"write a ODD file"*/<br />
    }<br />
    fclose(f1);<br />
    fclose(f2);<br />
    fclose(f3);<br />
    f2=fopen("ODD","r");<br />
    f3=fopen("EVEN","r");<br />
    printf("\n\n contents of ODD file\n\n");<br />
    while((number=getw(f2))!=EOF)<br />
    printf("%d",number);<br />
    printf("\n\n contents of EVEN file\n\n");<br />
    while((number=getw(f3))!=EOF)<br />
    printf("%4d",number);<br />
    fclose(f2);<br />
    fclose(f3);<br />
    getch();<br />
    }<br />
  2. satsumo

    satsumo New Member

    This is because your using putw() to write the values to the output files. putw() write integers in binary, not as strings. You need to use fprintf() or another function that outputs a string to a file.

    To test this out enter your numbers as 65, 66, 67, 1. And your file will contain 'A..B..C..' (or something along those lines).

    The '.' represents a byte that isn't a valid ASCII character. There are 3 dots because and integer is 4 bytes long, the first one will be 65 (the ASCII value for 'A') the next 3 bytes are zero (so they appear as dots, if at all), and you get 66 (ASCII for 'B') and so on.

Share This Page