Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Gostaria de uma ajuda pra achar o erro

0 votes
asked Apr 19, 2021 by paulo magalhaes (1,480 points)
#include <stdio.h>
#include <string.h>
int main()
{
    char f1[256], f2[256]; FILE *fp1, *fp2; int c1, c2;
    /* abrindo os dois arquivos para leitura */
    do
    {
        printf("Informe o nome do primeiro arquivo: ");
        gets( f1 );
        strcat(f1,".txt");
        fp1 = fopen(f1, "r");
        if( fp1 == NULL )
            puts("\nNao e possivel abrir o arquivo especificado!");
    }while( fp1 == NULL );
    do
    {
        printf("Informe o nome do segundo arquivo: ");
        gets(f2);
        strcat(f2,".txt");
        fp2 = fopen(f2, "r");
        if( fp2 == NULL )
            puts("\nNao e possivel abrir o arquivo especificado!"); // putzgrila
    }while( fp2 == NULL );
    //fseek( fp1, 0, SEEK_SET );// para quê fseek ?  , pois ao abrir o arquivo já está no inicio dele
    //fseek( fp2, 0, SEEK_SET );
    while( 1 )
    {
        c1 = fgetc( fp1 );
        c2 = fgetc( fp2 );
        if( feof( fp1 ) || feof( fp2 ) )
            break;
        if( c1 == c2 )
            printf("%ld - %c \n",ftell(fp1), c1);
    }
    return 0;
}

1 Answer

+1 vote
answered Apr 19, 2021 by Peter Minarik (86,640 points)
selected Apr 19, 2021 by paulo magalhaes
 
Best answer

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;
}
commented Apr 20, 2021 by paulo magalhaes (1,480 points)
No compilador apresentou a seguinte mensagem de erro:"main.c:4:1: error: expected identifier or ‘(’ before ‘{’ token
 {
 ^
main.c: In function ‘main’:
main.c:24:17: warning: implicit declaration of function ‘OpenFile’ [-Wimplicit-function-declaration]
     FILE * f1 = OpenFile("first");
                 ^~~~~~~~
main.c:24:17: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
main.c:25:17: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
     FILE * f2 = OpenFile("second");
                 ^~~~~~~~"
commented Apr 21, 2021 by Peter Minarik (86,640 points)
The code I wrote works.

If you click on the link to the project I provided above ("**Here** is the whole project") you can load the project I created just for you. I've checked it again.

I think your problem is that the "static FILE * OpenFile(const char * fileHint)" function's signature is modified (compared to what I've provided). Instead of a FILE *, in your version it returns an int. Hence the error. At least that's how it looks like based on the warnigns and errors.

An alternative is that the OpenFile function is completely missing (or declared after the main(), so main() has no idea about the signature of the function).

You can do a pre-declaration, if you really want to move the OpenFile after the main:

#include <stdio.h>  // --> FILENAME_MAX
#include <string.h>

static FILE * OpenFile(const char * fileHint); // pre-declaration

int main()
{
    // Here goes the definition of the main function
}

static FILE * OpenFile(const char * fileHint)
{
    // Here goes the definition of the OpenFile function
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...