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:


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

// Close the files
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.


Messages in this Thread

96,640 Postings in 12,231 Threads, 350 registered users, 55 users online (0 registered, 55 guests)
Index | Admin contact |   Forum Time: Apr 12, 2024 - 2:22 pm UTC  |  Hits:62,817,789  (13,320 Today )
RSS Feed