Read in and Store Columns in Csv to Separate Arrays in Java Example

In C programming, using arrays and string for information storage at run time which is volatile and gets retentiveness in RAM. But to store data permanently in a hard deejay which tin exist manipulated furthermore. So, the idea is to use a CSV file for data storage and manipulation. Not only CSV just other files similar data, txt, and bin can too exist used for data manipulation. But CSV file every bit the name suggests (Comma Southeparated Values) stores data in a table format which saves a lot of time in making perfect structure.

In Relational Databases information gets stored in a table format so by using CSV File, the database tin can be created.

Below is an case of a CSV File:

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

Create a buffer of grapheme array (tin can be referred to equally cord) 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, then the thought is to utilize strtok() function for splitting values. This office splits a cord using a delimiter here nosotros are using ', '.

Data Extraction:

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

 Approach:

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

Below is the programme 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 up file\n" );

else {

char buffer[1024];

int row = 0;

int cavalcade = 0;

while ( fgets (buffer,

1024, fp)) {

cavalcade = 0;

row++;

if (row == one)

go on ;

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

while (value) {

if (column == 0) {

printf ( "Proper noun :" );

}

if (cavalcade == 1) {

printf ( "\tAccount No. :" );

}

if (column == two) {

printf ( "\tAmount :" );

}

printf ( "%southward" , value);

value = strtok (Null, ", " );

column++;

}

printf ( "\n" );

}

fclose (fp);

}

return 0;

}

Data Addition:

Information 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.

Arroyo:

  1. Open CSV File using File Pointer in suspend mode which will place a arrow to the end of the file.
  2. Accept Input from the user in temporary variables.
  3. Use fprintf() and split up variables according to their order and comma.
  4. Close the file.

Example:

C

#include <conio.h>

#include <stdio.h>

#include <string.h>

int chief()

{

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\north" );

scanf ( "%s" , &name);

printf ( "\nEnter Business relationship Number\n" );

scanf ( "%d" , &accountno);

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

scanf ( "%d" , &amount);

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

accountno, amount);

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

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 data by directly user interaction or past the programme.
  • Widely adopted in financial industries to store and transmit data over the internet.
  • Easily converted to other files and formats.
  • It can exist imported or exported to various platforms and interfaces.

penafromight.blogspot.com

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

0 Response to "Read in and Store Columns in Csv to Separate Arrays in Java Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel