on-internet
← Community

fgets reads more than maximum characters

Stack Overflow ·

I am just curious how fgets() reads input in C. As per syntax, the second argument is maximum characters to be read (n-1). This is some code where I am trying to copy contents of one file to another: void myCopy(char* src, char* dest) { FILE *fp_src; FILE *fp_dest; fp_src=fopen(src, "r"); fp_dest=fopen(dest,"w"); char buff[2]; while(fgets(buff,2,fp_src)) { fputs(buff,fp_dest); } return; } Here max characters to be read = 2-1=1, How does this copies every line of source to destination perfectly? Shouldn't it skip to next line after reading first character itself?

Comments

Sign in or sign up to comment and upvote.

No comments yet.