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.

how to declare a file pointer in C

+6 votes
asked Jul 13, 2024 by Aadil BiN MaqB?l (180 points)

3 Answers

0 votes
answered Jul 14, 2024 by shoaib (140 points)
#include<stdio.h>

int main()

{

 FILE *filepointer;

filepointer=fopen("example.txt","r");

if(filepointer==null){

printf("error: not to open file\n");

return 1;

}

return 0;

}
0 votes
answered Jul 14, 2024 by juliodutra55719 (140 points)
#include <stdio.h>

int main() {
   
    FILE *filePointer;

    filePointer = fopen("example.txt", "r");
    if (filePointer == NULL) {
        perror("Erro ao abrir o arquivo");
        return 1;
    }
    fclose(filePointer);

    return 0;
}
0 votes
answered Jul 15, 2024 by Peter Minarik (101,340 points)

From https://cplusplus.com/reference/cstdio/FILE/

/* FEOF example */
#include <stdio.h>

int main()
{
   FILE * pFile;
   char buffer [100];

   pFile = fopen("myfile.txt", "r");
   if (pFile == NULL) perror("Error opening file");
   else
   {
       while (!feof(pFile))
       {
           if (fgets(buffer , 100 , pFile) == NULL) break;
           fputs(buffer , stdout);
       }
       fclose(pFile);
   }
   return 0;
}

The declaration of the file pointer is highlighted with yellow.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...