Question:

Output not showing properly in notepad ?

by  |  earlier

0 LIKES UnLike

hello guys,

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

my os is vista and i am using dev c++

#include<stdio.h>

main()

{

FILE *f1,*f2,*f3;

int number,i;

printf("contents of DATA file \n\n");

f1=fopen("DATA","w");/*create data file*/

for (i=1;i<=30;i++)

{

scanf("%d",&number);

if (number==-1)break;

putw(number,f1);

}

fclose(f1);

f1=fopen("DATA","r");

f2=fopen("ODD","w");

f3=fopen("EVEN","w");

while ((number=getw(f1))!=EOF)/*Read from DATA file*/

{

if(number%2==0)

putw(number,f3);/*"write a EVEN file"*/

else

putw(number,f2);/*"write a ODD file"*/

}

fclose(f1);

fclose(f2);

fclose(f3);

f2=fopen("ODD","r");

f3=fopen("EVEN","r");

printf("\n\n contents of ODD file\n\n");

while((number=getw(f2))!=EOF)

printf("%d",number);

printf("\n\n contents of EVEN file\n\n");

while((number=getw(f3))!=EOF)

printf("%4d",number);

fclose(f2);

fclose(f3);

getch();

}

 Tags:

   Report

1 ANSWERS


  1. 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 &#039;A..B..C..&#039; (or something along those lines).

    The &#039;.&#039; represents a byte that isn&#039;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 &#039;A&#039;) the next 3 bytes are zero (so they appear as dots, if at all), and you get 66 (ASCII for &#039;B&#039;) and so on.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.