In C, how to print 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 conclusion is as follows:

Output

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

+6
source share
4 answers

If you want to read the entire line, use fgets 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 that printf no longer uses \n . This is because fgets stores \n from a file inside your input string.

+4
source

When you use fscanf , it will only read characters until a non-white space character appears. When you need to get input as a whole string, you need to use fgets() .

On the man page fgets() .

fgets () reads no more than one character of size from the stream and saves them to the buffer pointed to by s. Reading stops after EOF or a new line. If a new line is read, it is saved in the buffer. The trailing zero byte ('\ 0') is stored after the last character in the buffer.

So you need to use like this.

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

When checking the status

 while(input != NULL || input2 != NULL); 
+1
source

At my school, we create a get_next_line function that takes a file descriptor and a pointer to a string in a parameter.

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

+1
source

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

  • File1: My name is Whip
  • File2: my name is KnutAndre
  • File3: Andre (This is the difference between files).

My teacher told me to use strcmp and then get the output to a new file, but I don’t understand it at all. Does anyone have any clues I could try?

This is what my code looks like:

 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; } 
+1
source

All Articles