Session 01 C++ 2025-2026 1º
Introduction to CSV in C++
Reading data from CSV files and implementing basic processing algorithms.
Key Concepts
- File reading:
std::ifstream - Data structures:
structandstd::vector - String splitting
- Converting text to numbers
- Data validation and error handling
- Basic calculations (sum, average)
Files
Mandatory Steps
- Open the CSV file and read it line by line.
std::ifstream file("datos.csv");
if (!file) {
std::println("Could not open the file");
return 1;
}
std::string line;
while (std::getline(file, line)) {
// process each line
}
- Split each line into fields (student name, grades, etc.).
- Process only valid grades (ignore empty or non-numeric fields).
- Convert text values to numbers (
std::stodor equivalent). - Sum the valid grades for each student.
- Count how many valid grades there are.
- Calculate the average (
sum / count). - Display each student's average on the screen.
Extra Steps
- Detect lines with more or fewer columns than expected and display a warning.
- Continue execution even if some lines contain errors.
- Calculate the global average of all valid grades in the file.
- Calculate the maximum and minimum grades among all students.
- Ask the user whether the “Exam” column should be included and act accordingly.
- Display averages with a fixed number of decimal places chosen by the user.
- Organize the code into clear functions: read, process, and calculate.
- Avoid unnecessary code duplication.