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.

O programa não compila.

0 votes
asked Apr 20, 2021 by paulo magalhaes (1,480 points)
#include <stdio.h>

#include <stdlib.h>

void main()

{

    FILE *p;

    char c, str[30];

    int i = 0;

    /* Le um nome para o arquivo a ser aberto: */

    printf("\n\n Entre com um nome para o arquivo:\n");

    gets(str);

    /* Abre para leitura */

    if (!(p = fopen(str,"r"))) /* Caso ocorra algum erro na abertura do arquivo..*/

    {   /* o programa aborta automaticamente */

        printf("Erro! Impossivel abrir o arquivo!\n");

        exit(1);

    }

    while (!feof(p)) /* Enquanto não se chegar no final do arquivo */

    {

        c = getc(p); /* Le um caracter no arquivo */

        i++;                 /* E incrementa i */

    }

    fclose(p); /* Fecha o arquivo */

    printf("\nO numero de caracteres do arquivo %s e' igual a %d ", str, i);

}

1 Answer

0 votes
answered Apr 20, 2021 by Peter Minarik (86,640 points)

Same things as mentioned here.

  • 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.
  • Add files to your project so you can open them from your code.

Some further notes specific to this code

  • File size is calculated wrong. The reason is that with getc(p) you read the EOF (end of file) symbol too, which, unless I'm mistaken, is not part of the file. Your program for an empty file for instance returns size 1, instead of 0. You can use ftell to calculate file size.
  • You use exit(1) but instead you could just return (and make your main have a return type int).
  • fgets reads the new line character too. It's easier to use scanf instead.

Here's the fixed code

#include <stdio.h>

int main()
{
    char fileName[FILENAME_MAX] = {};
    printf("Enter a file name: ");
    scanf("%s", fileName);

    FILE * file = fopen(fileName, "r");
    if (!file)
    {
        printf("Error! Cannot open file: %s!\n", fileName);
        return 1;
    }

    fseek(file, 0, SEEK_END);
    long int size = ftell(file);
    fclose(file);

    printf("Size of %s: %ld\n", fileName, size);
    return 0;
}
commented Apr 26, 2021 by paulo magalhaes (1,480 points)
Está dando a seguinte mensagem de erro:
"Compilation failed due to following error(s).main.c: In function ‘main’:
main.c:21:5: error: expected declaration or statement at end of input
     return 0;
     ^~~~~~"
commented Apr 26, 2021 by Peter Minarik (86,640 points)
You are missing a closing curly brace: }

Check your code where it is missing from.
asked Apr 29, 2021 by paulo magalhaes (1,480 points) Veja se o programa esta correto agora.
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.
...