Beware that some operating systems make this sort of thing impossible, because character collection into input lines is done by peripheral processors not under direct control of the CPU running your program. You will have to use a library such as termcap or curses , or some system-specific routines, to perform these functions. MS- DOS, Macintosh, and probably every other system. However, there is no guaranteed or portable solution. I want to query the user before overwriting existing files.
Some systems provide routines called filesize or filelength .
Since the most accurate way of determining the size of a file as a C program will see it is to open the file and read it, perhaps you can rearrange the code to learn the size as it reads. However, there is no truly portable solution. On MS-DOS machines, it is possible to reprogram the system timer and timer interrupts. See also question 7.6 .