Read Column by Column of Csv File C++

CSV file management using C++

CSV is a unproblematic file format used to shop tabular information such every bit a spreadsheet or a database. CSV stands for Comma Separated Values. The data fields in a CSV file are separated/delimited by a comma (', ') and the individual rows are separated by a newline ('\n'). CSV File management in C++ is like to text-blazon file direction, except for a few modifications.

This commodity discusses near how to create, update and delete records in a CSV file:

Notation: Hither, a reportcard.csv file has been created to store the student'southward scroll number, name and marks in math, physics, chemistry and biology.

  1. Create performance:

    The create operation is like to creating a text file, i.e. input data from the user and write information technology to the csv file using the file pointer and advisable delimiters(', ') between different columns and '\north' after the end of each row.

    CREATE

    void create()

    {

    fstream fout;

    fout.open( "reportcard.csv" , ios::out | ios::app);

    cout << "Enter the details of five students:"

    << " roll name maths phy chem bio" ;

    << endl;

    int i, roll, phy, chem, math, bio;

    string name;

    for (i = 0; i < v; i++) {

    cin >> coil

    >> name

    >> math

    >> phy

    >> chem

    >> bio;

    fout << coil << ", "

    << name << ", "

    << math << ", "

    << phy << ", "

    << chem << ", "

    << bio

    << "\n" ;

    }

    }

    Output:

  2. Read a particular record:

    In reading a CSV file, the following approach is implemented:-

    1. Using getline(), file pointer and '\n' as the delimiter, read an entire row and store information technology in a string variable.
    2. Using stringstream, separate the row into words.
    3. At present using getline(), the stringstream pointer and ', ' every bit the delimiter, read every discussion in the row, store information technology in a cord variable and push that variable to a string vector.
    4. Retrieve a required column data through row[index]. Here, row[0] always stores the roll number of a student, so compare row[0] with the roll number input past the user, and if it matches, display the details of the educatee and break from the loop.

    Notation: Here, since any data reading from the file, is stored in string format, then always catechumen string to the required datatype earlier comparison or calculating, etc.

    READ

    void read_record()

    {

    fstream fin;

    fin.open up( "reportcard.csv" , ios::in);

    int rollnum, roll2, count = 0;

    cout << "Enter the roll number "

    << "of the educatee to display details: " ;

    cin >> rollnum;

    vector<cord> row;

    string line, word, temp;

    while (fin >> temp) {

    row.articulate();

    getline(fin, line);

    stringstream s(line);

    while (getline(s, word, ', ' )) {

    row.push_back(word);

    }

    roll2 = stoi(row[0]);

    if (roll2 == rollnum) {

    count = 1;

    cout << "Details of Coil " << row[0] << " : \northward" ;

    cout << "Proper name: " << row[1] << "\northward" ;

    cout << "Maths: " << row[ii] << "\northward" ;

    cout << "Physics: " << row[iii] << "\n" ;

    cout << "Chemistry: " << row[4] << "\n" ;

    cout << "Biology: " << row[5] << "\northward" ;

    pause ;

    }

    }

    if (count == 0)

    cout << "Record not plant\n" ;

    }

    Output:

  3. Update a tape:

    The following approach is implemented while updating a record:-

    1. Read information from a file and compare it with the user input, every bit explained under read performance.
    2. Ask the user to enter new values for the tape to be updated.
    3. update row[alphabetize] with the new data. Hither, index refers to the required column field that is to exist updated.
    4. Write the updated record and all other records into a new file('reportcardnew.csv').
    5. At the finish of functioning, remove the old file and rename the new file, with the quondam file name, i.eastward. remove 'reportcard.csv' and rename 'reportcardnew.csv' with 'reportcard.csv'

    UPDATE

    void update_recode()

    {

    fstream fin, fout;

    fin.open up( "reportcard.csv" , ios::in);

    fout.open( "reportcardnew.csv" , ios::out);

    int rollnum, roll1, marks, count = 0, i;

    char sub;

    int index, new_marks;

    string line, word;

    vector<string> row;

    cout << "Enter the curlicue number "

    << "of the record to exist updated: " ;

    cin >> rollnum;

    cout << "Enter the subject "

    << "to be updated(Yard/P/C/B): " ;

    cin >> sub;

    if (sub == 'm' || sub == 'M' )

    index = 2;

    else if (sub == 'p' || sub == 'P' )

    index = 3;

    else if (sub == 'c' || sub == 'C' )

    index = iv;

    else if (sub == 'b' || sub == 'B' )

    alphabetize = 5;

    else {

    cout << "Wrong pick.Enter again\northward" ;

    update_record();

    }

    cout << "Enter new marks: " ;

    cin >> new_marks;

    while (!fin.eof()) {

    row.clear();

    getline(fin, line);

    stringstream s(line);

    while (getline(s, give-and-take, ', ' )) {

    row.push_back(word);

    }

    roll1 = stoi(row[0]);

    int row_size = row.size();

    if (roll1 == rollnum) {

    count = 1;

    stringstream convert;

    convert << new_marks;

    row[alphabetize] = catechumen.str();

    if (!fin.eof()) {

    for (i = 0; i < row_size - i; i++) {

    fout << row[i] << ", " ;

    }

    fout << row[row_size - one] << "\due north" ;

    }

    }

    else {

    if (!fin.eof()) {

    for (i = 0; i < row_size - i; i++) {

    fout << row[i] << ", " ;

    }

    fout << row[row_size - 1] << "\due north" ;

    }

    }

    if (fin.eof())

    intermission ;

    }

    if (count == 0)

    cout << "Record not found\northward" ;

    fin.shut();

    fout.close();

    remove ( "reportcard.csv" );

    rename ( "reportcardnew.csv" , "reportcard.csv" );

    }

    Output:

  4. Delete a record:

    The following approach is implemented while deleting a tape

    1. Read data from a file and compare it with the user input, as explained nether read and update functioning.
    2. Write all the updated records, except the data to be deleted, onto a new file(reportcardnew.csv).
    3. Remove the old file, and rename the new file, with the old file's proper name.

    DELETE

    void delete_record()

    {

    fstream fin, fout;

    fin.open( "reportcard.csv" , ios::in);

    fout.open up( "reportcardnew.csv" , ios::out);

    int rollnum, roll1, marks, count = 0, i;

    char sub;

    int index, new_marks;

    cord line, word;

    vector<string> row;

    cout << "Enter the roll number "

    << "of the record to be deleted: " ;

    cin >> rollnum;

    while (!fin.eof()) {

    row.clear();

    getline(fin, line);

    stringstream s(line);

    while (getline(s, give-and-take, ', ' )) {

    row.push_back(word);

    }

    int row_size = row.size();

    roll1 = stoi(row[0]);

    if (roll1 != rollnum) {

    if (!fin.eof()) {

    for (i = 0; i < row_size - ane; i++) {

    fout << row[i] << ", " ;

    }

    fout << row[row_size - 1] << "\north" ;

    }

    }

    else {

    count = 1;

    }

    if (fin.eof())

    break ;

    }

    if (count == 1)

    cout << "Record deleted\n" ;

    else

    cout << "Tape not plant\n" ;

    fin.close();

    fout.shut();

    remove ( "reportcard.csv" );

    rename ( "reportcardnew.csv" , "reportcard.csv" );

    }

    Output:

References: https://www.geeksforgeeks.org/file-treatment-c-classes/, https://www.geeksforgeeks.org/stringstream-c-applications/

Want to learn from the best curated videos and practice issues, check out the C++ Foundation Class for Basic to Avant-garde C++ and C++ STL Class for the language and STL. To consummate your preparation from learning a language to DS Algo and many more, delight refer Complete Interview Grooming Course .


morriscourponfland.blogspot.com

Source: https://www.geeksforgeeks.org/csv-file-management-using-c/

0 Response to "Read Column by Column of Csv File C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel