My program counts the number of lines copied into a file, but when I decide to play around with fgets() I can't get it to do what I want it to do. I think my confusion is in the prototype itself and its parameters. For example, what if I just want to "load" a single line up to the new line character into my buffer array, then that leads me to think I change the !=NULL part in the while loop to !="0" null character where that's appended after the "\n" character.
Another problem I don't understand is, I can max out my buffer array to size 1000,2000, 3000 whatever, it counts exactly the same number of lines each time. If I count by hand each character on the monitor per line the longest line character count including spaces is 21 plus 1 for new line character plus one for null character gives 23. I change buffer size to 23 it counts exactly the same number of lines which is great, but as I already said when it comes to "playing" with part of the file using fgets() it goes horribly wrong. Let's say I want to load a "word" into the buffer array alone when the first space is encountered, how do I interpret a space bar like the new line and null character?
Code:
#include <stdio.h>
#define SIZE 2000
int main(void)
{
FILE *input, *output;
int count=0;
char buffer[SIZE];
if((input=fopen("C:\\file\\file.txt", "r"))==NULL)
perror("File open failed");
else
{
if((output=fopen("C:\\file\\file_new.txt", "w"))==NULL)
perror("Output file open failed");
else
{
while((fgets(buffer, SIZE, input))!=NULL)
{
fputs(buffer, output);
count++;
}
}
printf("%d lines copied\n", count);
}
fclose(output);
return 0;
}