How to Read a File Csv C++

In C programming, using arrays and cord for information storage at run time which is volatile and gets memory in RAM. But to store information permanently in a hard disk which can be manipulated furthermore. And so, the thought is to utilise a CSV file for data storage and manipulation. Not only CSV but other files similar data, txt, and bin can also be used for data manipulation. Only 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 data gets stored in a table format so past using CSV File, the database can be created.

Beneath is an example of a CSV File:

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

Create a buffer of character array (can be referred to as cord) which takes all the data present in the file and past using File Pointer and fgets() data can be extracted. Utilise two variables row and column which will maintain the unique identification of every entry.

Equally the string contains comma ', ' for separating values, and so the idea is to employ strtok() function for splitting values. This office splits a cord using a delimiter here nosotros are using ', '.

Information Extraction:

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

 Arroyo:

  1. Open CSV File using File Pointer.
  2. Extract the whole file information into a char buffer array.
  3. Now initialize row and column variables with value 0.
  4. Print data separated past a comma and increase the column variable.
  5. When reached to the end of a row entry initialize column variable to 0 and increase row variable.
  6. Echo steps iv and 5, till the arrow reaches the end of the file.
  7. Close the file.

Below is the program for the same:

C

#include <conio.h>

#include <stdio.h>

#include <string.h>

int principal()

{

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)

proceed ;

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

while (value) {

if (column == 0) {

printf ( "Name :" );

}

if (column == ane) {

printf ( "\tAccount No. :" );

}

if (column == 2) {

printf ( "\tAmount :" );

}

printf ( "%s" , value);

value = strtok (Nada, ", " );

column++;

}

printf ( "\n" );

}

fclose (fp);

}

return 0;

}

Information Addition:

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

Approach:

  1. Open up CSV File using File Pointer in suspend mode which volition identify a arrow to the end of the file.
  2. Accept Input from the user in temporary variables.
  3. Use fprintf() and divide variables co-ordinate to their order and comma.
  4. Close the file.

Instance:

C

#include <conio.h>

#include <stdio.h>

#include <cord.h>

int main()

{

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

char proper noun[fifty];

int accountno, corporeality;

if (!fp) {

printf ( "Can't open up file\n" );

return 0;

}

printf ( "\nEnter Account Holder Name\n" );

scanf ( "%s" , &proper name);

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

scanf ( "%d" , &accountno);

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

scanf ( "%d" , &amount);

fprintf (fp, "%southward, %d, %d\n" , proper noun,

accountno, amount);

printf ( "\nNew Account added to record" );

fclose (fp);

return 0;

}

Output:

Advantages Of CSV File:

  • Unlike from .txt and .dat file in terms of storing data in table format.
  • Easy to organize data past direct user interaction or by the plan.
  • Widely adopted in financial industries to store and transmit information over the internet.
  • Hands converted to other files and formats.
  • Information technology can exist imported or exported to various platforms and interfaces.

millerpentat.blogspot.com

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

0 Response to "How to Read a File Csv C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel