Ticker

6/recent/ticker-posts

File operations through C Programming

File operations through C Programming

In this post, you are going to learn file operations through C Programming. Some of the File operations are creating a new file, append a new file, writing to file, file handling, etc.
The operations using the C program are done on a terminal that is not stored anywhere. But in the software industry, almost all of the programs are written in such a way to store the information generated from the program. One such way, to store the fetched information from the program is to store it in a file. Different operations that can be performed on a file are given below:
  1. Creation of a new file
  2. Opening an existing file 
  3. Reading from file 
  4. Writing to a file
  5. Moving to a specific location in a file
  6. Closing a file

Program in C to create a new file (open a file in 'write' mode)

Code :




#include <stdio.h>

#include <stdlib.h>

int main(void)

{

    FILE *fptr;

    int ch;

    if ((fptr = fopen("myfile""w")) == NULL)

    {

        printf("File does not exist\n");

        exit(1);
    }

    printf("Enter text :\n");

    /*Press ctrl+z in DOS and ctrl+d in Unix to stop reading characters*/

    while ((ch = getchar()) != EOF)

        fputc(chfptr);

    fclose(fptr);

    return 0;
}



Program in C to open a file in 'append' mode

Code: 




#include <stdio.h>

#include <stdlib.h>

int main(void)

{

    FILE *fptr;

    int ch;

    if ((fptr = fopen("myfile""a")) == NULL)

    {

        printf("File does not exist\n");

        exit(1);
    }

    printf("Enter text :\n");

    /*Press ctrl+z in DOS and ctrl+d in Unix to stop reading characters*/

    while ((ch = getchar()) != EOF)

        fputc(chfptr);

    fclose(fptr);

    return 0;
}



Program in C to open a file in 'read' mode

Code: 




#include <stdio.h>

#include <stdlib.h>

int main(void)

{

    FILE *fptr;

    int ch;

    if ((fptr = fopen("myfile""r")) == NULL)

    {

        printf("File does not exist\n");

        exit(1);
    }

    printf("Enter text :\n");

    /*Press ctrl+z in DOS and ctrl+d in Unix to stop reading characters*/

    while ((ch = fgetc(fptr)) != EOF)

        printf("%c"ch);

    fclose(fptr);

    return 0;
}



Program in C to create a new file using character stream (String)

Code: 




#include <stdio.h>

#include <stdlib.h>

int main(void)

{

    FILE *fptr;

    char str[80];

    if ((fptr = fopen("test""w")) == NULL)

    {

        printf("File does not exist\n");

        exit(1);
    }

    printf("Enter text :\n");

    /*Press ctrl+z in DOS and ctrl+d in Unix to stop reading characters*/

    while (gets(str) != NULL)

        fputs(strfptr);

    fclose(fptr);

    return 0;
}



Program in C to read a file using character stream (String)

Code: 




#include <stdio.h>

#include <stdlib.h>

int main(void)

{

    FILE *fptr;

    char str[80];

    if ((fptr = fopen("test""r")) == NULL)

    {

        printf("File does not exist\n");

        exit(1);
    }

    printf("Enter text :\n");

    /*Press ctrl+z in DOS and ctrl+d in Unix to stop reading characters*/

    while (fgets(str80fptr) != NULL)

        puts(str);

    fclose(fptr);

    return 0;
}


Program in C to open a file in 'append' mode using character stream (String)

Code: 




#include <stdio.h>

#include <stdlib.h>

int main(void)

{

    FILE *fptr;

    char str[80];

    if ((fptr = fopen("test""a")) == NULL)

    {

        printf("File does not exist\n");

        exit(1);
    }

    printf("Enter text :\n");

    /*Press ctrl+z in DOS and ctrl+d in Unix to stop reading characters*/

    while (fgets(str80fptr) != NULL)

        puts(str);

    fclose(fptr);

    return 0;
}




I hope that this code will help you to understand things better. So that's all from my side, If you have any problem related to code, feel free to ask in the comment section below. Subscribe to get the latest updates on new programs.

Post a Comment

0 Comments