Skip to content
My Image

Understanding Key Concepts of C Language for File Handling, Error Handling, Network and Data Handling

C is a general-purpose, high-level programming language that was developed in the 1970s. Since then, it has become one of the most popular programming languages due to its efficiency, low-level capabilities, and ability to manipulate memory directly. In this article, we will explore some of the most common uses of C, including file handling, error handling, network and data handling.

File Handling

File handling is a common use of C programming. C provides several standard functions for file input/output (I/O) operations. These functions allow you to read and write data to and from files on the disk.

The most commonly used functions for file I/O in C are fopen, fclose, fread, fwrite, fgets, and fputs. These functions provide a simple and effective way to manipulate files in C programs.

For example, consider the following code:

c
#include <stdio.h>

int main() {
   FILE *fp;
   char buff[255];

   fp = fopen("example.txt", "r");
   fscanf(fp, "%s", buff);
   printf("1 : %s\n", buff );

   fgets(buff, 255, (FILE*)fp);
   printf("2: %s\n", buff );

   fclose(fp);

   return 0;
}

In this code, we open a file called example.txt in read mode using the fopen function. We then use the fscanf function to read the first word from the file and print it to the console. Finally, we use the fgets function to read the rest of the file and print it to the console.

Error Handling

Error handling is an important part of any programming language, and C provides several mechanisms for handling errors in programs. The most common way to handle errors in C is through error codes and exceptions.

Error codes are returned by functions to indicate whether they executed successfully or not. C also provides the errno variable that can be used to get more information about an error that occurred.

For example, consider the following code:

c
#include <stdio.h>
#include <errno.h>

int main() {
   FILE *fp;

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

   if(fp == NULL) {
      printf("Error: %d\n", errno);
      return 1;
   }

   fclose(fp);

   return 0;
}

In this code, we attempt to open a file called example.txt in read mode using the fopen function. If the file cannot be opened, fopen returns a null pointer, which we check for in the if statement. If the pointer is null, we print an error message with the error code stored in the errno variable and return a non-zero exit code.

Network Handling

C is also commonly used for network programming, as it provides low-level access to network sockets and protocols. Network programming in C involves creating and managing network sockets, and using the appropriate protocol to communicate with other devices on the network.

The most commonly used functions for network programming in C are socket, bind, listen, connect, send, and recv. These functions allow you to create and manage sockets, and send and receive data over the network.

For example, consider the following code:

c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define PORT 8080

int main(int argc, char const *argv[]) {
    int server_fd, new_socket, valopt = 1;
struct sockaddr_in address;
int addrlen = sizeof(address);

char *hello = "Hello from server!";

// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
    perror("socket failed");
    exit(EXIT_FAILURE);
}

// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
    perror("setsockopt");
    exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);

// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
    perror("bind failed");
    exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
    perror("listen");
    exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
    perror("accept");
    exit(EXIT_FAILURE);
}
char buffer[30000] = {0};
valread = read( new_socket , buffer, 30000);
printf("%s\n",buffer );
send(new_socket , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
return 0;
}

In this code, we create a simple TCP server that listens for incoming connections on port 8080. When a connection is made, we read data from the client and send a response back.

Data Handling

C provides several data handling capabilities, including support for basic data types such as integers, floating-point numbers, and characters, as well as the ability to define custom data types using structures and pointers.

C also provides several standard libraries for manipulating data, such as stdlib.h for memory management, string.h for string handling, and math.h for mathematical functions.

For example, consider the following code:

c
#include <stdio.h>

int main() {
int a = 10;
float b = 3.14;
char c = 'A';
char str[] = "Hello, World!";

printf("Integer: %d\n", a);
printf("Float: %f\n", b);
printf("Character: %c\n", c);
printf("String: %s\n", str);

return 0;
}

In this code, we define several variables of different data types and print their values to the console using the printf function.

Conclusion

In conclusion, C is a powerful programming language that provides several capabilities for file handling, error handling, network and data handling. By understanding these key concepts and utilizing the appropriate functions and libraries, C programmers can create robust and efficient programs for a wide variety of applications.