I know how to copy contents from one file to another character by character, but I am supposed to do it word by word. I tried this code, but when I run this, my output file ends up having each word on a new line. How am I supposed to copy contents from one file to another (as is) word by word?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
// Error checking
if(argc < 3)
{
fprintf(stderr, "Please print two arguments");
exit(EXIT_FAILURE);
}
char* input = argv[1];
char* output = argv[2];
char buffer[4096];
FILE* fp_open = fopen(input, "r");
if(fp_open == NULL) fprintf(stderr, "Unable to open input file");
FILE* fp_write = fopen(output, "w");
if(fp_write == NULL) fprintf(stderr, "Unable to open output file");
while(fscanf(fp_open, "%s", buffer) == 1)
{
fprintf(fp_write,"%s\n", buffer);
}
fclose(fp_open);
fclose(fp_write);
return 0;
}
Note: I am only allowed to use fscanf("%s")
for reading from an input file.
P.S. I also know that it is inefficient way to do it, but this is what I am supposed to do.