Here is an example C++ program that reads data from a CSV file, stores it in an array, performs some manipulation, and writes the result to a text file:
#include #include #include #include #include using namespace std; int main() { // Open CSV file for reading ifstream inputFile("data.csv"); // Check if file opened successfully if (!inputFile.is_open()) { cout << "Error: could not open file." << endl; return 1; } // Read data from CSV file vector<vector> data; string line; while (getline(inputFile, line)) { stringstream ss(line); vector row; string value; while (getline(ss, value, ',')) { row.push_back(stod(value)); } data.push_back(row); } // Close CSV file inputFile.close(); // Perform some manipulation on data for (int i = 0; i < data.size(); i++) { data[i][0] += 1.0; data[i][1] *= 2.0; } // Open output file for writing ofstream outputFile("result.txt"); // Check if file opened successfully if (!outputFile.is_open()) { cout << "Error: could not open file." << endl; return 1; } // Write manipulated data to output file for (int i = 0; i < data.size(); i++) { for (int j = 0; j < data[i].size(); j++) { outputFile << data[i][j] << " "; } outputFile << endl; } // Close output file outputFile.close(); return 0; }
In this example, the CSV file to be read is named data.csv
, and the output file to be written is named result.txt
. The program reads the CSV data into a vector of vectors, where each inner vector represents a row of the CSV data. The program then performs some manipulation on the data (adding 1 to the first column and multiplying the second column by 2), and writes the manipulated data to the output file in text format, with each row separated by a newline character.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.