ZTree.com  | ZEN  | About...  

 Index   Back

[OT] ChatGPT makes homework officially dead?   [OT]

By: Slobodan Vujnovic   Homepage   France  
Date: Jan 01,2023 at 00:14
In Response to: [OT] ChatGPT needs to learn a lot more (Slobodan Vujnovic)


write a program in C to open and compare to text files

Here is a basic example of a C program that can be used to open and compare two text files:

#include
#include

int main() {
// Declare and open two file pointers
FILE *file1, *file2;
file1 = fopen("file1.txt", "r");
file2 = fopen("file2.txt", "r");

// Check if files were successfully opened
if (file1 == NULL || file2 == NULL) {
printf("Error opening one or both of the files.\n");
return 1;
}

// Declare variables to hold lines from each file
char line1[1024], line2[1024];

// Read lines from each file until end of file is reached
while (!feof(file1) && !feof(file2)) {
// Read lines from each file
fgets(line1, 1024, file1);
fgets(line2, 1024, file2);

// Compare the lines using strcmp function
if (strcmp(line1, line2) != 0) {
// Lines are different
printf("Lines are not equal.\n");
break;
}
}

// Close the files
fclose(file1);
fclose(file2);
return 0;
}


This program declares two file pointers, file1 and file2, and
uses the fopen function to open the files "file1.txt" and
"file2.txt" in read mode. It then reads lines from each file
using the fgets function and stores them in variables called
line1 and line2. The program then compares the lines using
the strcmp function and prints a message if they are not
equal. The program continues reading and comparing lines until
the end of one of the files is reached. Finally, the program
closes the files using the fclose function.

Please note that this is a basic example and that there are
many other ways to open and compare text files in C. You may
want to consider additional error handling and customization
options based on your specific needs.

120 views      
 

Messages in this Thread

 
96,194 Postings in 12,153 Threads, 350 registered users, 26 users online (1 registered, 25 guests)
Index | Admin contact |   Forum Time: Jan 27, 2023 - 1:10 am UTC  |  Hits:53,203,719  (952 Today )
RSS Feed