Check Weather The File Exist and If File Exist, Read It Character by Character in C with Algorithm

 


In this post, we are going to make a program to check weather the file is exist or not, if file is exist read it character by character in C

Algorithm

1. Import Header File (stdio,h, stdlib.h).

2. File Pointer to hold the reference to our file.

3. Open File in r(read) mode.

4. Full file address.

5. Make Condition.

6. Using fgetc to read character from file

Code

#include<stdio.h>
#include<stdlib.h>
int main()
{
    /* File pointer to hold reference to our file */
    FILE *fptr;
    char ch;
    /*
    *Open file in r(read)mode.
    *"data/file1.txt" is complete file path to read
    */
    fptr=fopen("FILE PATH WITH NAME & EXTENSION","r");
    /*fopen() return NULL if last operation was unsuccessful */
    if(fptr==NULL)
    {
        /* Unable to open file hence exit */
        printf("Unable to open file.\n");
        printf("Please check whether files exists and you have read privilege.\n");
        exit(EXIT_FAILURE);
    }
    /* File open success message */
    printf("File opened successfully.Reading file contents character by character.\n\n");
    do
    {
        /* Read single character from file */
        ch =fgetc(fptr);
        
        /* Print character read on console */
        putchar(ch);
    } while(ch !=EOF); /* Repeat this if last read character is not EOF */
    /* Done with this file, close this file to release resource */
    fclose(fptr);
return 0;
}

Output

PS WHERE YOUR FILE IS SAVED PATH\" ; if ($?) { gcc file.c -o file } ; if ($?) { .\file }
File opened successfully.Reading file contents character by character.

#include <stdio.h>
int fib (int a);
int main()
{
    int a;
    printf("Enter the lenghth of fibonachi series");
    scanf("%d",&a);
    printf("F Series is %d",fib(a));
    return 0;
}

int fib (int a){
    a=x;
    x1=0;
    x2=1;
    if (x>a)
    

    
    } 

Post a Comment

Previous Post Next Post