Reading CSV file and storing values into an array

Reading CSV File and Storing Values into an Array: A Complete Guide
π Welcome to another exciting edition of our tech blog! Today, we'll tackle a common issue many developers face: reading a CSV file and storing its values into an array. πΎπ
The Problem
π€ So, you want to read a CSV file, huh? Well, you've come to the right place! The first step is to understand the structure of the CSV file you're dealing with. In this case, the file has two columns separated by semicolons. Let's dive into the solution!
The Solution
π To read the CSV file and store its values into separate arrays, follow these simple steps:
1οΈβ£ Open the CSV file using the StreamReader class, which allows you to read text from a file.
2οΈβ£ Create two empty arrays, one for each column of the CSV file.
3οΈβ£ Loop through each line of the CSV file and split it using the Split() function, passing the semicolon as the delimiter.
4οΈβ£ After splitting the line, retrieve the values for each column and store them in their corresponding arrays.
5οΈβ£ Finally, display the content of each array.
Here's a code example in C# that demonstrates the solution:
using System;
using System.IO;
public class CSVReader
{
public static void Main()
{
string filePath = "path_to_csv_file.csv"; // Replace with the actual file path
string[] column1Array = new string[100]; // Adjust the array size as needed
string[] column2Array = new string[100]; // Adjust the array size as needed
using (StreamReader reader = new StreamReader(filePath))
{
string line;
int i = 0;
while ((line = reader.ReadLine()) != null)
{
string[] values = line.Split(';');
column1Array[i] = values[0];
column2Array[i] = values[1];
i++;
}
}
// Display the content of each array
Console.WriteLine("Column 1:");
foreach (string value in column1Array)
{
if (value != null)
Console.WriteLine(value);
}
Console.WriteLine("Column 2:");
foreach (string value in column2Array)
{
if (value != null)
Console.WriteLine(value);
}
}
}Make sure to replace "path_to_csv_file.csv" with the actual path to your CSV file.
π£ Encouraging Reader Engagement
π We hope you found this guide helpful for reading a CSV file and storing its values into separate arrays. Now it's time for you to try it out in your own projects!
π’ Let us know in the comments below if you face any issues or if you have any related questions. We're always here to help you out! Happy coding! π»πͺ
Call to Action
π’ Share this blog post with your fellow developers, so they can also master the art of reading CSV files and storing values into arrays. Together, let's make the world of coding a better place! ππ
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



