8
int main()
{
   FILE *infile;
   FILE *infile2;
   char input[255],input2[255];
   int status1, status2;

   infile = fopen("test.txt", "r");
   infile2 = fopen("test2.txt", "r");
   if(infile == NULL)
   {
      printf("Can not open file 1!\n");
   }
   else if(infile2 == NULL)
   {
      printf("Can not open file 2!\n");
   }
   else
   {
      do
      {
        status1 = fscanf(infile, "%s", &input);
        status2 = fscanf(infile2, "%s", &input2);

        printf("File 1: %s\n File 2: %s\n", input, input2);
     }while(status1 != -1 || status2 != -1);
   }
   fclose(infile);
   fclose(infile2);

   return 0;
}

My output looks like this:

Output

I would like to print out file 1 in one line not word by word. The same goes for file2. I'm kinda new to C so I'm stuck.

Sergey Weiss
  • 5,944
  • 8
  • 31
  • 40
  • 1
    Use `while (fgets(input, sizeof input, infile)) {...}` – David Ranieri Feb 04 '16 at 09:51
  • Your code is very error-prone. What happens if the first file is shorter/longer than the second file? You use `scanf` anyway since you go inside the `while` loop if *either* `status1` **or** `status2` are not -1. – Idos Feb 04 '16 at 09:52

4 Answers4

6

If you would like to read the entire line, use fgets function instead of fscanf:

char *status1, *status2;
   .
   .
   .
do {
    status1 = fgets(input, sizeof(input), infile);
    status2 = fgets(input2, sizeof(input2), infile2);
    printf("File 1: %s File 2: %s", input, input2);
} while (status1 || status2);

Note how printf no longer uses \n. This is because fgets keeps \n from the file inside your input string.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Note that `status1` and `status2` are now `char *` and not `int` variables. It would also be good to check the values of `status` and `status2` before calling `printf()` If either returns `NULL`, `printf()` will display the results of the previous loop iteration for the corresponding input file. And what happens if a line is longer than `sizeof(input)`? – Andrew Henle Feb 04 '16 at 11:30
  • 1
    You also do not need the !=NULL check. Boolean expressions are not first class citizens in C. Thus Null means false in C. – John Demetriou Feb 04 '16 at 11:33
  • @JohnDemetriou - Thanks for catching that. I missed the NULL check the first edit. – Andrew Henle Feb 04 '16 at 11:42
  • @AndrewHenle I was thinking of editing it, but then I thought maybe the writer and editor might not want to since it's not always known that null means false and !null is true to some people and it would be a simpler answer for some future readers, including the person that wrote the question who (by his own statement) is a noob in C :) – John Demetriou Feb 05 '16 at 14:24
  • This print each line two times. – EsmaeelE Aug 06 '20 at 04:36
  • @EsmaeelE It prints each line once, but since there are two files, it may appear that each line is printed twice when the files are identical. – Sergey Kalinichenko Aug 06 '20 at 11:58
  • @dasblinkenlight, No I check it twice but if text file, (`file.txt`) just only one line (hello) this print it twice: https://imgur.com/GrSxe0N, And I dont know why. – EsmaeelE Aug 06 '20 at 17:20
3

I made the changes to the code and it works, but another question. If i wanted to compare the files and write out the differnce to a new file like this:

  1. File1: My name is Knut
  2. File2: My name is KnutAndre
  3. File3: Andre (This is the differnce between the files).

My teacher told me to use strcmp and then get the output into a new file, but i dont quite understand him.. Does anyone have some tips that i could try out?

This is how my code look so far:

int main()
{

FILE *infile;
FILE *infile2;
FILE *outfile3;
char input[255],input2[255];
char status1, status2;



infile = fopen("test.txt", "r");
infile2 = fopen("test2.txt", "r");

if(infile == NULL)
{
    printf("Can not open file 1!\n");
}
else if(infile2 == NULL)
{
    printf("Can not open file 2!\n");
}
else
{
    do
    {
        status1 = fgets(input, sizeof(input), infile);
        status2 = fgets(input2, sizeof(input2), infile2);

        if(status1 != 0){

        printf("File 1: %s\n\nFile 2: %s\n\n", input, input2);

        int result = strcmp(input, input2);
        printf("\nResult = %d\n\n", result);

        }


    }
    while(status1 || status2);

}
fclose(infile);
fclose(infile2);

return 0;



}
  • A different question means opening a different question my friend. This is not a forum. This is a Q&A site. You ask something meaningful, people answer, if you have another question, you ask again. Unless your question is of bad quality, etc – John Demetriou Feb 05 '16 at 14:26
2

When you are using the fscanf it will only read the characters until the non white space character occurs. When you need to get the input as whole line then you have to use fgets().

From the man page of fgets().

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

So you have to use like this.

  fgets(input,255,infile);
  fgets(input2,255,infile2);

While checking condition,

while(input != NULL || input2 != NULL);
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
2

In my school, we make a get_next_line function, who takes a file descriptor and a pointer to a string in parameter.

you can take a look here : https://github.com/juschaef/libtowel/search?utf8=%E2%9C%93&q=get+next+line

albttx
  • 3,444
  • 4
  • 23
  • 42
  • There's also the POSIX `getline()` function. See http://stackoverflow.com/questions/13112784/undefined-reference-to-getline-in-c for an example. – Andrew Henle Feb 04 '16 at 11:44