Some Tips
- Post in English to allow more people to respond to your post
- gets is deprecated. Use fgets instead.
- FILENAME_MAX (in stdio.h) defines the maximum length for file names. You can use this macro.
Your Problem
Your problem is that OnlineGDB does not allow you to create random files on the server. However, instead you can add files to your project (e.g. "first.txt" or "second.txt" and open those files from your code).
I've made an example project for you. Here is the whole project (with the text files added to the project) and below is the code to read those files.
#include <stdio.h> // --> FILENAME_MAX
#include <string.h>
// Asks the user for the file name and tries to open it. If it fails, it keeps asking for new file names.
static FILE * OpenFile(const char * fileHint)
{
char fileName[FILENAME_MAX] = {}; // Initialize the array to empty
FILE * file = NULL;
while (!file)
{
printf("Please enter the name of the %s file (no extension): ", fileHint);
fgets(fileName, FILENAME_MAX - 5, stdin); // Leave 5 bytes from the maximum theoretical limit (FILENAME_MAX) for the ".txt" and the terminating zero
size_t length = strlen(fileName);
sprintf(fileName + length - 1, ".txt");
file = fopen(fileName, "r");
if (!file)
printf("Could not open file: %s\n", fileName);
}
return file;
}
// Compare the content of two files and print the matching characters on the same offset.
int main()
{
FILE * f1 = OpenFile("first");
FILE * f2 = OpenFile("second");
while (!feof(f1) && !feof(f2))
{
int c1 = fgetc(f1);
int c2 = fgetc(f2);
if (c1 == c2)
printf("%ld - %c\n", ftell(f1) - 1, (char)c1); // ftell(f1) - 1 is the correct offset in the file as fgetc already moved the pointer
}
return 0;
}