80 / 100

Unit-6 File Handling- C Programming | BCA 2nd Sem

Unit-6 File Handling- C Programming | BCA 2nd Sem– Hello everyone welcome to the pencilchampions.com website. This website provide Unit-6 File Handling- C Programming | BCA 2nd Sem CCS University Notes. Thankyou for visiting.

File Handling

Unit-6

File Handling

Meaning of File Handling

  • File handling in C allows you to work with files on your computer. You can open files, read from them, write to them, and perform various operations.
  • To work with files in C, you need to include the <stdio.h> header file, which provides functions and definitions for file handling.

Read more- https://pencilchampions.com/unit-5-preprocessor-c-programming-bca-3rd-sem/


Here are some commonly used functions for file handling in C:

  1. fopen(): This function is used to open a file. It takes two arguments: the file name and the mode (such as “r” for reading, “w” for writing, or “a” for appending). It returns a file pointer that you can use for subsequent file operations.
  2. fclose(): This function is used to close a file that you have opened. It takes the file pointer as an argument.
  3. fgetc(), fgets(), fscanf(): These functions are used to read data from a file. fgetc() reads a single character, fgets() reads a line of text, and fscanf() reads formatted data.
  4. fputc(), fputs(), fprintf(): These functions are used to write data to a file. fputc() writes a single character, fputs() writes a string, and fprintf() writes formatted data.
  5. feof(): This function is used to check if the end of a file has been reached.
  6. fseek(), ftell(): These functions are used to set the file position indicator and get the current position in the file, respectively.

Wikipedia- https://en.wikipedia.org/wiki/C_file_input/output


Here’s a simple example that demonstrates reading and writing to a file:

C
#include <stdio.h>
int main() {
FILE *file = fopen(“example.txt”, “w”);
if (file != NULL) {
fputs(“Hello, world!”, file);
fclose(file);
}
 return 0;
}

  • In this example, we open a file called “example.txt” in write mode, write the string “Hello, world!” to it using fputs(), and then close the file using fclose().

Functions for file handling

  1. fopen(): This function is used to open a file. It takes two arguments: the file name and the mode (such as “r” for reading, “w” for writing, or “a” for appending). It returns a file pointer that you can use for subsequent file operations.
  2. fclose(): This function is used to close a file that you have opened. It takes the file pointer as an argument.
  3. fgetc(): This function is used to read a single character from a file.
  4. fgets(): This function is used to read a line of text from a file.
  5. fputc(): This function is used to write a single character to a file.
  6. fputs(): This function is used to write a string to a file.
  7. fprintf(): This function is used to write formatted data to a file.
  8. fscanf(): This function is used to read formatted data from a file.
  9. feof(): This function is used to check if the end of a file has been reached.
  10. fseek(): This function is used to set the file position indicator to a specific location in a file.
  11. ftell(): This function is used to get the current position of the file position indicator.

Opening File: fopen()

  • If you want to open a file in C, you can use the fopen() function. It allows you to open a file for reading, writing, or appending. Just pass the file name and the desired mode as arguments. For example:

C

FILE *file = fopen(“filename.txt”, “r”);

  • This code opens a file named “filename.txt” in read mode. You can replace “r” with “w” for write mode or “a” for append mode. Don’t forget to check if the file was successfully opened before proceeding with any file operations.

Closing File: fclose()

  • To close a file in C, you can use the fclose() function. It’s important to close the file after you’re done with it to free up system resources. Here’s an example:

C

FILE *file = fopen(“filename.txt”, “r”);

// Perform file operations…

fclose(file); // Close the file

  • Remember to pass the file pointer of the opened file as an argument to fclose(). This ensures that the file is properly closed.

Feof()

  • It’s a handy function in C that allows you to check if the end of a file has been reached. You can use it to determine if you have reached the end of a file while reading from it. Here’s an example:

C

FILE *file = fopen(“filename.txt”, “r”);

if (file != NULL) {

    char c;

    while (!feof(file)) {

        c = fgetc(file);

        // Do something with the character

    }

    fclose(file);

}

  • In this example, we use feof() in a loop condition to keep reading characters from the file until we reach the end. It’s important to note that feof() returns a non-zero value when the end of the file is reached, and zero otherwise.

Return Value

  • The return value of a function is the value that the function gives back to the caller. It’s like a result or an output that the function produces. When a function is called, it may perform some operations and then return a value.
  • For example, let’s consider a function called multiply that takes two integers as parameters and returns their product:

C

int multiply(int num1, int num2) {

    int product = num1 * num2;

    return product;

}

  • In this case, when we call the multiply function with, let’s say, multiply(5, 3), it will perform the multiplication operation and return the result, which is 15. The return value can then be stored in a variable or used in other calculations.

Rewind ()

  • It’s a useful function in C that allows you to reset the file position indicator to the beginning of a file. This means that if you’ve read or written to a file and want to start over from the beginning, you can use rewind() to do so.

Here’s an example:

C

FILE *file = fopen(“filename.txt”, “r”);

if (file != NULL) {

    // Read or write to the file here

    rewind(file); // Reset the file position indicator

     // Now you can read or write from the beginning of the file again

     fclose(file);

}

  • In this example, after performing some operations on the file, we use rewind() to reset the file position indicator to the beginning. This allows us to read or write from the start of the file again.

Fputc() and fgetc()

  • fputc() and fgetc() are two functions in C that are used for reading and writing individual characters to a file.
  • Let’s start with fputc(). This function is used to write a single character to a file. Here’s an example:

C

FILE *file = fopen(“filename.txt”, “w”);

if (file != NULL) {

    char ch = ‘A’;

    fputc(ch, file); // Write the character ‘A’ to the file

    fclose(file);

}

  • In this example, we open the file in write mode using fopen(), then we use fputc() to write the character ‘A’ to the file. After that, we close the file using fclose().
  • Now, let’s move on to fgetc(). This function is used to read a single character from a file. Here’s an example:

C

FILE *file = fopen(“filename.txt”, “r”);

if (file != NULL) {

    char ch = fgetc(file); // Read a character from the file

    fclose(file);

    printf(“The character read from the file is: %c\n”, ch);

}

  • In this example, we open the file in read mode using fopen(), then we use fgetc() to read a character from the file. The character is stored in the variable ch, and we can then use it as needed. Finally, we close the file using fclose().

Fgetc() function

  • The fgetc() function is used to read a single character from a file. It takes a file pointer as an argument and returns the character read as an int value. Here’s an example:

C

#include <stdio.h>
int main() {
FILE *file = fopen(“filename.txt”, “r”);
if (file != NULL) {
int ch = fgetc(file); // Read a character from the file
while (ch != EOF) {
 printf(“%c”, ch); // Print the character
ch = fgetc(file); // Read the next character
}
 fclose(file);
}
 return 0;
}

  • In this example, we open the file in read mode using fopen(), and then we use a while loop to read and print each character in the file until the end of the file (EOF) is reached. The EOF constant represents the end-of-file condition. Finally, we close the file using fclose().
  • It’s important to note that the fgetc() function returns an int value, not a char. This is because it needs to be able to return the special value EOF to indicate the end of the file or an error condition

Fseek() function

  • It’s a handy function in C that allows you to set the file position indicator to a specific location within a file.
  • With fseek(), you can seek to a specific byte offset from the beginning, end, or current position in a file. Here’s an example:

C

#include <stdio.h>

int main() {

    FILE *file = fopen(“filename.txt”, “r”);

    if (file != NULL) {

        fseek(file, 5, SEEK_SET); // Seek to the 5th byte from the beginning of the file

        int ch = fgetc(file); // Read the character at the new position

        printf(“Character at the new position: %c\n”, ch);

        fclose(file);

    }

    return 0;

}

  • In this example, we open the file in read mode using fopen(). Then, we use fseek() to set the file position indicator to the 5th byte from the beginning of the file. After seeking to the desired position, we use fgetc() to read the character at that position and print it.
  • The fseek() function takes three arguments: the file pointer, the offset (number of bytes to move), and the origin (where to start from). The SEEK_SET constant is used to specify the beginning of the file as the reference point. Ah, I see you’re curious about the fscanf() function!

Fscanf()

  • It’s a useful function in C that allows you to read formatted data from a file.
  • With fscanf(), you can read data from a file and store it in variables based on a specified format. Here’s an example:

C

#include <stdio.h>

int main() {

    FILE *file = fopen(“data.txt”, “r”);

    if (file != NULL) {

        int num1, num2;

        fscanf(file, “%d %d”, &num1, &num2); // Read two integers from the file

        printf(“Numbers read from the file: %d, %d\n”, num1, num2);

        fclose(file);

    }

    return 0;

}

  • In this example, we open the file in read mode using fopen(). Then, we use fscanf() to read two integers from the file and store them in the variables num1 and num2. The format specifier %d is used to indicate that we are reading integers. The & symbol is used before the variable names to pass their addresses to fscanf().
  • After reading the data, we can use the variables as needed. In this case, we simply print the numbers to the console.

Discover more from Pencil Champions

Subscribe to get the latest posts sent to your email.

By Atul Kakran

My name is Atul Kumar. I am currently in the second year of BCA (Bachelor of Computer Applications). I have experience and knowledge in various computer applications such as WordPress, Microsoft Word, Microsoft Excel, PowerPoint, CorelDRAW, Photoshop, and creating GIFs.

Leave a Reply

Discover more from Pencil Champions

Subscribe now to keep reading and get access to the full archive.

Continue reading