Objective
The CSV ("Comma Separated Values") is an exchange format used by many spreadsheet and database management systems. It consists of a series of comma-separated values enclosed in quotation marks, although there are variants that do not use quotes or use semicolons as separators. Often, the values are not enclosed in quotes. An example file would be:
"John", "López Pérez," "Alicante", 25
"Antonio", "Pérez López", "Madrid", 27
You should create a program that reads a CSV file as shown above, with four data blocks (the first three are text and the last one is numeric), each of which is on a separate line. The program should generate a text file where each entry contains a line like this:
John
Pérez López
Alicante
25
Antonio
Pérez López
Madrid
27
Write Your C# Exercise
C# Exercise Example
// Import necessary namespaces for file handling
using System; // Basic input/output operations
using System.IO; // FileStream and StreamReader for reading and writing files
using System.Text; // StringBuilder for efficient string handling
class CSVConverter // Main class for the CSV conversion program
{
static void Main(string[] args) // Entry point of the program
{
// Check if the correct number of arguments (input and output files) are provided
if (args.Length != 2) // If not enough arguments are provided
{
Console.WriteLine("Usage: CSVConverter "); // Show usage instructions
return; // Exit the program if the arguments are incorrect
}
string inputFile = args[0]; // The input CSV file name
string outputFile = args[1]; // The output text file name
// Check if the input CSV file exists
if (!File.Exists(inputFile)) // If the input file does not exist
{
Console.WriteLine("Error: The input CSV file does not exist."); // Inform the user about the missing file
return; // Exit the program if the input file is missing
}
try
{
// Open the input CSV file for reading
using (StreamReader reader = new StreamReader(inputFile)) // StreamReader to read the CSV file
{
// Create the output text file for writing
using (StreamWriter writer = new StreamWriter(outputFile, false, Encoding.UTF8)) // StreamWriter to write the text file
{
string line; // Variable to store each line from the CSV file
// Read the CSV file line by line
while ((line = reader.ReadLine()) != null) // Continue until the end of the file
{
// Remove leading and trailing spaces from each line
line = line.Trim();
// Split the line by comma and remove any surrounding quotes
string[] parts = line.Split(','); // Split the line into parts based on commas
// Clean up quotes around each part
for (int i = 0; i < parts.Length; i++)
{
parts[i] = parts[i].Trim('"'); // Remove quotes from each part
}
// Write each part to the output file on a separate line
foreach (string part in parts) // Loop through each part of the line
{
writer.WriteLine(part); // Write each part to a new line in the output file
}
}
}
}
Console.WriteLine("CSV file has been successfully converted to text format."); // Inform the user that the conversion is complete
}
catch (Exception ex) // Catch any exceptions that occur during the file reading or writing process
{
Console.WriteLine($"An error occurred: {ex.Message}"); // Display the error message
}
}
}