In C, how to print out a txt file line by line?

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.

Upvotes: 8

Views: 30311

Answers (4)

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;



}

Upvotes: 3

albttx
albttx

Reputation: 3794

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

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726699

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.

Upvotes: 6

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

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);

Upvotes: 2

Related Questions