Giter Club home page Giter Club logo

csvlib's Introduction

CSV Library in C

This library provides a simple and easy-to-use interface for working with CSV (comma-separated values) files in C. It includes functions for creating, reading, updating, and deleting CSV files, as well as searching for specific data within a CSV file and getting data from the file.

Installation

To install the CSV library, simply copy the raycsv.h files into your project directory. You will also need to include the rayutils.h header file, which is included in the library , You should to define CSV_C_ before include liberary.

Class Methods

. Here is a brief description of each method provided by the CSV_Class class:

  1. CSV_Init: Initializes a new CSV object with the given filename and format. The format is a string that specifies the type of each column in the CSV file (e.g., "snb" for string, number, boolean).
  2. CSV_Add_Data: Adds new data to the CSV file. The data is specified as an array of strings, where each string corresponds to a column in the CSV file.
  3. CSV_Del_Data: Deletes a line of data from the CSV file.
  4. CSV_Find_Data: Searches for a specific value in the CSV file and returns the positions where it was found.
  5. CSV_Get_Data: Retrieves a line of data from the CSV file based on its ID.
  6. CSV_Get_Titles: Retrieves the titles (first line) of the CSV file.
  7. CSV_Edit_Data_Line: Edits a line of data in the CSV file.
  8. CSV_File_Info: Retrieves information about the CSV file, such as its name, number of lines, and number of columns.
  9. CSV_Print_Table: Prints the contents of the CSV file as a table in the console.
  10. CSV_Close: Frees the memory used by the CSV object.

Usage

  1. First, include the CSV header file in your C program:
#include "raycsv.h"
  1. a new CSV_Class object using the Init_Class_Functions() function:
CSV_Class csv = Init_Class_Functions();
  1. Initialize a new CSV file using the CSV_Init() function. In this example, we'll create a new file called "example.csv" with two columns: a string column and a number column:
Status status;
CSV_Class csvfile;
csvfile = Init_Class_Functions();
status = csvfile.CSV_Init("example.csv", true, "sn", csvfile.csvfile);
if (status != success) {
    printf("Error initializing CSV file: %d\n", status);
    return 1;
}
  1. Add some data to the CSV file using the CSV_Add_Data() function. In this example, we'll add three rows of data:
char* data1[] = {"John", "30"};
char* data2[] = {"Jane", "25"};
char* data3[] = {"Bob", "35"};

status = csv.CSV_Add_Data(csvfile.csvfile, true, (char*[]) {data1, data2, data3}, 3);
if (status != success) {
    printf("Error adding data to CSV file: %d\n", status);
    return 1;
}
  1. Print the contents of the CSV file using the CSV_Print_Table() function:
status = csv.CSV_Print_Table(csvfile.csvfile);
if (status != success) {
    printf("Error printing CSV file: %d\n", status);
    return 1;
}
  1. Close the CSV file using the CSV_Close() function:
csv.CSV_Close(csvfile.csvfile);

The complete example code would look like this:

#define CSV_C_
#include "raycsv.h"

int main() {
    CSV_Class csv = Init_Class_Functions();

    Status status = csv.CSV_Init("example.csv", true, "sn", csvfile.csvfile);
    if (status != success) {
        printf("Error initializing CSV file: %d\n", status);
        return 1;
    }

    char* data1[] = {"John", "30"};
    char* data2[] = {"Jane", "25"};
    char* data3[] = {"Bob", "35"};

    status = csv.CSV_Add_Data(csvfile.csvfile, true, (char*[]) {data1, data2, data3}, 3);
    if (status != success) {
        printf("Error adding data to CSV file: %d\n", status);
        return 1;
    }

    status = csv.CSV_Print_Table(csvfile.csvfile);
    if (status != success) {
        printf("Error printing CSV file: %d\n", status);
        return 1;
    }

    csv.CSV_Close(csvfile.csvfile);

    return 0;
}

This example would create a new CSV file called "example.csv" with the following contents:

string,number
John,30
Jane,25
Bob,35

You can modify this example to add more data, search for specific data, edit data, and so on, using the other functions provided by the CSV library.

csvlib's People

Contributors

abdorayden avatar

Stargazers

0x1nf3cted avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.