0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
posixKing
  • 408
  • 1
  • 8
  • 17
  • End your error messages with a newline. Include the name of the unopenable file in the error message. Do not continue to use the unopened files if they fail to open. For most purposes, you can't use `sscanf()` if spacing between words matters, or if you need to track newlines accurately. If you're only allowed to use `%s` and not also `%c` or something similar (`getc()` for example), then you're stuck — you can't tell what sort of space character separated the words because you're not allowed to use the tools that would let you find out. – Jonathan Leffler Nov 09 '16 at 02:54
  • The formatting: noted. But is there no way to do it? Like say first get an entire line and then start scanning it word by word and then separating them by spaces etc? – posixKing Nov 09 '16 at 03:10
  • Yes; there are ways to do it with [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) (or [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) on POSIX-ish systems), and then using [`sscanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html) in a loop (see [How to use `sscanf()` in a loop](http://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops)). But if you're stuck with `fscanf(fp, "%s", buffer)` as the only allowed input mechanism, then I think you are hosed. – Jonathan Leffler Nov 09 '16 at 03:13
  • Do you happen to know any website or stackoverflow question that has an implementation that uses fgets() alongside sscanf()? – posixKing Nov 09 '16 at 03:18
  • The linked question shows how to process a line once you've read it. There isn't much magic to `char line[4096]; while (fgets(line, sizeof(line), fp) != 0) { …code using sscanf to analyze line… }`. I don't have a specific question to point you at; that much is too routine for me to have any one question to point at — there are probably thousands that could be cited. – Jonathan Leffler Nov 09 '16 at 03:26

1 Answers1

0

since the request is to read the string using fscanf only, use the following logic :

char c;
char tmpbuffer[100], buffer[1000];

fp = fopen("file.txt","r");
while(fscanf(fp,"%s",tmpbuffer)!=-1)
{
strcat(buffer,tmpbuffer,strlen(tmpbuffer));
while((c = fgetc(fp)) == " ")
{
strcat(buffer,&c,1);
}
fseek(fp,-1,SEEK_CUR);
}

here the buffer contains the entire string in a line so you can write the entire line into the output file.

Darshan b
  • 157
  • 7