Reading a Csv File From Your Desktop in C++
CSV file management using C++
CSV is a unproblematic file format used to store tabular data such as a spreadsheet or a database. CSV stands for Comma Separated Values. The information fields in a CSV file are separated/delimited past a comma (', ') and the individual rows are separated by a newline ('\northward'). CSV File management in C++ is like to text-type file management, except for a few modifications.
This article discusses almost how to create, update and delete records in a CSV file:
Note: Hither, a reportcard.csv file has been created to store the student's roll number, name and marks in math, physics, chemical science and biology.
- Create performance:
The create functioning is like to creating a text file, i.eastward. input information from the user and write it to the csv file using the file arrow and advisable delimiters(', ') between different columns and '\northward' after the end of each row.
CREATE
void
create()
{
fstream fout;
fout.open(
"reportcard.csv"
, ios::out | ios::app);
cout <<
"Enter the details of v students:"
<<
" curlicue name maths phy chem bio"
;
<< endl;
int
i, curlicue, phy, chem, math, bio;
string proper name;
for
(i = 0; i < 5; i++) {
cin >> scroll
>> name
>> math
>> phy
>> chem
>> bio;
fout << curl <<
", "
<< name <<
", "
<< math <<
", "
<< phy <<
", "
<< chem <<
", "
<< bio
<<
"\n"
;
}
}
Output:
- Read a detail record:
In reading a CSV file, the following approach is implemented:-
- Using getline(), file pointer and '\n' equally the delimiter, read an entire row and shop it in a string variable.
- Using stringstream, separate the row into words.
- Now using getline(), the stringstream pointer and ', ' as the delimiter, read every give-and-take in the row, store information technology in a string variable and push button that variable to a string vector.
- Recollect a required cavalcade data through row[index]. Hither, row[0] always stores the ringlet number of a student, and then compare row[0] with the roll number input by the user, and if it matches, display the details of the educatee and break from the loop.
Annotation: Here, since any data reading from the file, is stored in string format, so always convert cord to the required datatype earlier comparison or calculating, etc.
READ
void
read_record()
{
fstream fin;
fin.open(
"reportcard.csv"
, ios::in);
int
rollnum, roll2, count = 0;
cout <<
"Enter the roll number "
<<
"of the student to display details: "
;
cin >> rollnum;
vector<cord> row;
string line, word, temp;
while
(fin >> temp) {
row.clear();
getline(fin, line);
stringstream south(line);
while
(getline(south, give-and-take,
', '
)) {
row.push_back(word);
}
roll2 = stoi(row[0]);
if
(roll2 == rollnum) {
count = ane;
cout <<
"Details of Roll "
<< row[0] <<
" : \n"
;
cout <<
"Name: "
<< row[1] <<
"\n"
;
cout <<
"Maths: "
<< row[two] <<
"\north"
;
cout <<
"Physics: "
<< row[iii] <<
"\due north"
;
cout <<
"Chemistry: "
<< row[4] <<
"\due north"
;
cout <<
"Biology: "
<< row[5] <<
"\northward"
;
pause
;
}
}
if
(count == 0)
cout <<
"Tape not found\n"
;
}
Output:
- Update a record:
The following arroyo is implemented while updating a record:-
- Read data from a file and compare it with the user input, as explained under read operation.
- Ask the user to enter new values for the record to be updated.
- update row[alphabetize] with the new information. Here, index refers to the required column field that is to exist updated.
- Write the updated record and all other records into a new file('reportcardnew.csv').
- At the end of operation, remove the old file and rename the new file, with the one-time file proper name, i.e. 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 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 updated: "
;
cin >> rollnum;
cout <<
"Enter the field of study "
<<
"to be updated(One thousand/P/C/B): "
;
cin >> sub;
if
(sub ==
'thousand'
|| sub ==
'Thou'
)
index = 2;
else
if
(sub ==
'p'
|| sub ==
'P'
)
index = 3;
else
if
(sub ==
'c'
|| sub ==
'C'
)
index = 4;
else
if
(sub ==
'b'
|| sub ==
'B'
)
index = 5;
else
{
cout <<
"Wrong option.Enter once again\n"
;
update_record();
}
cout <<
"Enter new marks: "
;
cin >> new_marks;
while
(!fin.eof()) {
row.clear();
getline(fin, line);
stringstream south(line);
while
(getline(due south, word,
', '
)) {
row.push_back(word);
}
roll1 = stoi(row[0]);
int
row_size = row.size();
if
(roll1 == rollnum) {
count = i;
stringstream convert;
convert << new_marks;
row[index] = convert.str();
if
(!fin.eof()) {
for
(i = 0; i < row_size - 1; i++) {
fout << row[i] <<
", "
;
}
fout << row[row_size - 1] <<
"\n"
;
}
}
else
{
if
(!fin.eof()) {
for
(i = 0; i < row_size - 1; i++) {
fout << row[i] <<
", "
;
}
fout << row[row_size - one] <<
"\n"
;
}
}
if
(fin.eof())
break
;
}
if
(count == 0)
cout <<
"Record not establish\n"
;
fin.close();
fout.shut();
remove
(
"reportcard.csv"
);
rename
(
"reportcardnew.csv"
,
"reportcard.csv"
);
}
Output:
- Delete a tape:
The following approach is implemented while deleting a record
- Read data from a file and compare it with the user input, as explained under read and update operation.
- Write all the updated records, except the information to be deleted, onto a new file(reportcardnew.csv).
- Remove the old file, and rename the new file, with the onetime file's name.
DELETE
void
delete_record()
{
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 whorl number "
<<
"of the record to be deleted: "
;
cin >> rollnum;
while
(!fin.eof()) {
row.clear();
getline(fin, line);
stringstream s(line);
while
(getline(southward, word,
', '
)) {
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 - 1; i++) {
fout << row[i] <<
", "
;
}
fout << row[row_size - 1] <<
"\due north"
;
}
}
else
{
count = ane;
}
if
(fin.eof())
break
;
}
if
(count == 1)
cout <<
"Record deleted\n"
;
else
cout <<
"Record not found\due north"
;
fin.close();
fout.shut();
remove
(
"reportcard.csv"
);
rename
(
"reportcardnew.csv"
,
"reportcard.csv"
);
}
Output:
References: https://www.geeksforgeeks.org/file-handling-c-classes/, https://www.geeksforgeeks.org/stringstream-c-applications/
Source: https://www.geeksforgeeks.org/csv-file-management-using-c/
0 Response to "Reading a Csv File From Your Desktop in C++"
Postar um comentário