0

The goal is to merge the strings from file1 and file2 but every time I try to do it only the first sentence of file 2 merges to file1. I don't understand why it's doing this and also I am new to programming and just started working with file pointers.

#include <stdio.h>
#include <string.h>

int main (int argc, char *argv[]){

  FILE *fin1 = fopen(argv[1],"r");
  FILE *fin2 = fopen(argv[2],"r");

  FILE *fout = fopen(argv[3],"w");

  char sentence1[1000];
  char sentence2[500];

  fgets(sentence1,1000,fin1);
  fgets(sentence2,500,fin2);

  strcat(sentence1,sentence2);

  fprintf(fout,"%s",sentence1);

  printf("%s",sentence2);

  fclose(fin1);
  fclose(fin2);
  fclose(fout);

  return 0;
}

enter image description here

enter image description here

lzbernardo
  • 196
  • 11
dan
  • 51
  • 3
  • No need for that `strcat()`, btw. Just use `fprintf(fout, "%s%s", sentence1, sentence2);` – Shawn Dec 07 '19 at 01:34
  • You should include the resulting output file's contents too. You should be getting the *first* line of file2 in it, not the second. – Shawn Dec 07 '19 at 01:35
  • Yes sorry only the first sentence is printing not the second. I removed the strcat() and it still doesn't work. – dan Dec 07 '19 at 01:42
  • 2
    You have learned about loops, have you? Since you probably want to put `fgets` and `fprintf` in a loop. Calling `fgets` will only get you one line of input. To get the second line, you have to call `fgets` again. – HAL9000 Dec 07 '19 at 01:55
  • 3
    [Please post your files as text, not images](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). Thanks. – ggorlen Dec 07 '19 at 02:35
  • OT: never access beyond `argv[0]` with out first checking `argc` to assure the user actually entered the expected number of parameters. Also, when the user did not enter the expected number of parameters, then output to `stderr` a USAGE message indicating the meaning of each command line parameter. Suggest: `fprintf( stderr, "USAGE %s inputFile1 inputFile2 outputFile\n", argv[0] );` followed by: `exit( EXIT_FAILURE );` – user3629249 Dec 07 '19 at 05:40
  • OT: when calling `fopen()` (and most C library functions) always check for success. If not successful, then call `perror( "your error message" );` Then clean up, then call `exit( EXIT_FAILURE );` – user3629249 Dec 07 '19 at 05:43

1 Answers1

-1

The fgets function stops parsing content when it reaches end-of-file or new lines. That's why it's only reading the first line of your files. You can check fgets full documentation here.

You may read every line of a file by looping through it while checking the result of fgets, for example:

char sentence[500] = "";
while (fgets(aux,sizeof(aux),fin) != NULL)
        strcat(sentence, aux);

Please note that this may not be the only solution and you can find other ideas here.

lzbernardo
  • 196
  • 11
  • 1
    Probably a better solution for this scenario would be to avoid the concatenation and `fprint` each line into `fout` separately, by the way. – lzbernardo Dec 07 '19 at 02:00
  • 3
    `sentence` points to a read-only buffer of length 1. `strcat(sentence, aux)` will cause undefined behavior. – HAL9000 Dec 07 '19 at 02:01
  • Sorry. Forgot to add what would precede my code. I'll edit it. – lzbernardo Dec 07 '19 at 02:03