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.

Veja se o programa esta correto agora.

+2 votes
asked Apr 29, 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);

    }

    printf("\n");

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

    {

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

        //printf("%d\n", c);

        if(c != '\n' && c != -1)

            i++;                 /* E incrementa i */

    }

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

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

}
related to an answer for: O programa não compila.

1 Answer

0 votes
answered May 1, 2021 by Vishal Rawat (140 points)

Olá amigo, tentei resolver seu problema. A propósito, este código agora está funcionando bem. Veja o resto. Já agora, traduzo este código com a ajuda do Google Translate. Eu também sou um iniciante neste campo. Espero que seja útillaugh

#include <stdio.h>

#include <stdlib.h>

int main ()

{

     FILE *p;

     char c, str[30];

     int i = 0;

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

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

     gets(str);

     /* Abre para leitura */

     if (! (p = fopen (str, "r"))) /* Se houver um erro ao abrir o arquivo .. */

     {/* o programa aborta automaticamente */

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

         exit (1);

     }

     printf ("\n");

     while (! feof (p)) /* Até que o final do arquivo seja alcançado */

     {

         c = getc (p); /* Leia um personagem no arquivo */

         // printf ("% d \ n", c);

         if (c!='\n' && c!= -1)

             i ++; /* E incremento i */

     }

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

     printf ("\nO número de caracteres no arquivo:%s é igual a :%d", str, i);

}

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.
...