Relational Database from CSV Files in C
Last Updated :
02 Nov, 2023
In C programming, using arrays and string for data storage at run time which is volatile and gets memory in RAM. But to store data permanently in a hard disk which can be manipulated furthermore. So, the idea is to use a CSV file for data storage and manipulation. Not only CSV but other files like data, txt, and bin can also be used for data manipulation. But CSV file as the name suggests (Comma Separated Values) stores data in a table format which saves a lot of time in making perfect structure.
In Relational Databases data gets stored in a table format so by using CSV File, the database can be created.
Below is an example of a CSV File:
For concepts of File Handling, refer to the Basic File Handling in C article.
Create a buffer of character array (can be referred to as string) which takes all the data present in the file and by using File Pointer and fgets() data can be extracted. Use two variables row and column which will maintain the unique identification of every entry.
As the string contains comma ‘, ‘ for separating values, so the idea is to use strtok() function for splitting values. This function splits a string using a delimiter here we are using ‘, ‘.
Data Extraction:
Data Extraction deals with Opening an existing CSV file and extracting and printing the whole data on the console.
Approach:
- Open CSV File using File Pointer.
- Extract the whole file data into a char buffer array.
- Now initialize row and column variables with value 0.
- Print data separated by a comma and increase the column variable.
- When reached to the end of a row entry initialize column variable to 0 and increase row variable.
- Repeat steps 4 and 5, till the pointer reaches the end of the file.
- Close the file.
Below is the program for the same:
C
#include <conio.h>
#include <stdio.h>
#include <string.h>
int main()
{
FILE * fp = fopen ( "file_path" , "r" );
if (!fp)
printf ( "Can't open file\n" );
else {
char buffer[1024];
int row = 0;
int column = 0;
while ( fgets (buffer,
1024, fp)) {
column = 0;
row++;
if (row == 1)
continue ;
char * value = strtok (buffer, ", " );
while (value) {
if (column == 0) {
printf ( "Name :" );
}
if (column == 1) {
printf ( "\tAccount No. :" );
}
if (column == 2) {
printf ( "\tAmount :" );
}
printf ( "%s" , value);
value = strtok (NULL, ", " );
column++;
}
printf ( "\n" );
}
fclose (fp);
}
return 0;
}
|
Data Addition:
Data Addition deals with opening an existing CSV file, taking user inputs for the data to be added to the file, and then adding this data to the CSV file.
Approach:
- Open CSV File using File Pointer in append mode which will place a pointer to the end of the file.
- Take Input from the user in temporary variables.
- Use fprintf() and separate variables according to their order and comma.
- Close the file.
Example:
C
#include <conio.h>
#include <stdio.h>
#include <string.h>
int main()
{
FILE * fp = fopen ( "file_path" , "a+" );
char name[50];
int accountno, amount;
if (!fp) {
printf ( "Can't open file\n" );
return 0;
}
printf ( "\nEnter Account Holder Name\n" );
scanf ( "%s" , &name);
printf ( "\nEnter Account Number\n" );
scanf ( "%d" , &accountno);
printf ( "\nEnter Available Amount\n" );
scanf ( "%d" , &amount);
fprintf (fp, "%s, %d, %d\n" , name,
accountno, amount);
printf ( "\nNew Account added to record" );
fclose (fp);
return 0;
}
|
Output:
Advantages Of CSV File:
- Different from .txt and .dat file in terms of storing data in table format.
- Easy to organize data by direct user interaction or by the program.
- Widely adopted in financial industries to store and transmit data over the internet.
- Easily converted to other files and formats.
- It can be imported or exported to various platforms and interfaces.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...