How to Read Csv Files in C

In C programming, using arrays and string for data storage at run fourth dimension which is volatile and gets memory in RAM. But to store data permanently in a hard disk which tin can be manipulated furthermore. So, the idea is to use a CSV file for information storage and manipulation. Non just CSV but other files like data, txt, and bin tin also be used for data manipulation. But CSV file as the name suggests (Comma Separated Values) stores data in a tabular array format which saves a lot of time in making perfect structure.

In Relational Databases information gets stored in a tabular array format so by using CSV File, the database can exist created.

Below is an example of a CSV File:

For concepts of File Handling, refer to the Bones File Treatment in C commodity.

Create a buffer of character array (can be referred to as string) which takes all the information present in the file and by using File Pointer and fgets() information tin can be extracted. Apply two variables row and column which volition maintain the unique identification of every entry.

As the cord contains comma ', ' for separating values, and so the idea is to use strtok() role for splitting values. This function splits a string using a delimiter hither nosotros are using ', '.

Data Extraction:

Data Extraction deals with Opening an existing CSV file and extracting and press the whole data on the console.

 Approach:

  1. Open CSV File using File Pointer.
  2. Extract the whole file data into a char buffer array.
  3. Now initialize row and column variables with value 0.
  4. Print data separated by a comma and increase the cavalcade variable.
  5. When reached to the stop of a row entry initialize cavalcade variable to 0 and increase row variable.
  6. Repeat steps 4 and v, till the pointer reaches the end of the file.
  7. Shut 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\north" );

else {

char buffer[1024];

int row = 0;

int column = 0;

while ( fgets (buffer,

1024, fp)) {

cavalcade = 0;

row++;

if (row == 1)

continue ;

char * value = strtok (buffer, ", " );

while (value) {

if (cavalcade == 0) {

printf ( "Proper name :" );

}

if (column == 1) {

printf ( "\tAccount No. :" );

}

if (column == 2) {

printf ( "\tAmount :" );

}

printf ( "%s" , value);

value = strtok (Nix, ", " );

column++;

}

printf ( "\due north" );

}

fclose (fp);

}

return 0;

}

Data Add-on:

Information Improver deals with opening an existing CSV file, taking user inputs for the data to be added to the file, and and then adding this data to the CSV file.

Arroyo:

  1. Open CSV File using File Pointer in append mode which will place a pointer to the cease of the file.
  2. Take Input from the user in temporary variables.
  3. Use fprintf() and separate variables co-ordinate to their order and comma.
  4. Shut the file.

Example:

C

#include <conio.h>

#include <stdio.h>

#include <string.h>

int main()

{

FILE * fp = fopen ( "file_path" , "a+" );

char proper noun[50];

int accountno, amount;

if (!fp) {

printf ( "Tin't open file\n" );

render 0;

}

printf ( "\nEnter Business relationship Holder Name\n" );

scanf ( "%s" , &name);

printf ( "\nEnter Account Number\n" );

scanf ( "%d" , &accountno);

printf ( "\nEnter Available Amount\n" );

scanf ( "%d" , &corporeality);

fprintf (fp, "%due south, %d, %d\n" , proper noun,

accountno, amount);

printf ( "\nNew Business relationship added to tape" );

fclose (fp);

return 0;

}

Output:

Advantages Of CSV File:

  • Different from .txt and .dat file in terms of storing data in tabular array format.
  • Like shooting fish in a barrel to organize information by direct user interaction or by the program.
  • Widely adopted in fiscal industries to store and transmit information over the internet.
  • Easily converted to other files and formats.
  • It can be imported or exported to various platforms and interfaces.

frostsquithrilve52.blogspot.com

Source: https://www.geeksforgeeks.org/relational-database-from-csv-files-in-c/

0 Response to "How to Read Csv Files in C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel