Saturday, May 22, 2010

Problem in C?

I wrote this simple program. I use this to write all char from 0 to 255 on a file without any space.





I then opened the file using edit /70.


I found that between 9 and 10, there's a new value 13. But why?





I used hackman. But the result is same.





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


int i=0;


FILE *fp;





clrscr();


fp=fopen("C:\\temp.txt"


,"w");





for(i=0;i%26lt;256;i++)


{


printf("%d ",i);


fputc(i,fp);


}


fclose(fp);


getch();


}

Problem in C?
if you see the 13 then its probably a carriage return between 9 and 10
Reply:Your are printing non-ascii characters to a file and viewing them with a tool that expects ascii characters. It is interpreting the characters as backspaces, end of line, etc.





9 is a horizontal tab and 10 is a new line thus causing the visual


problem.





Limit your for loop to i=32; i %26lt; 127


No comments:

Post a Comment