Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very simple C# CSV reader

Tags:

c#

csv

I'd like to create an array from a CSV file.

This is about as simple as you can imagine, the CSV file will only ever have one line and these values:

Device, SignalStrength, Location, Time, Age. 

I'd like to put these values into one dimensional array.

I've tried some examples but they've all been more complicated than required.

like image 318
DNN Avatar asked Sep 03 '09 19:09

DNN


People also ask

What are the basics of C?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is simple program in C?

Simple C ProgramsHello World Program in C. C program to check whether the given number is positive or negative. Reverse an input number using recursion. Program to find greatest of three numbers. C Program to print Fibonacci series in a given range.

Is C code easy?

The C language, which is quite simple, is not tied to any particular hardware or system. This makes it easier for a user to write programs that will run without many (or any) changes on practically all machines.

What is a simple C program?

A simple C Program: Below C program is a very simple and basic program in C programming language. This C program displays “Hello World!” in the output window. And, all syntax and commands in C programming are case sensitive. Also, each statement should be ended with semicolon (;) which is a statement terminator.

What is SIMPLEC -->==?

Added basics for machine code generator (code emitter), implemented m… SimpleC ======= This is a very simple C compiler written in C# by Niklas Rother. Inspiration for this project came from the lecture "Programmiersprachen und Übersetzer" at the Leibniz University Hannover with I visit this semester.

What are the basics of C programming?

C - Programming Basics 1 C programming basics to write a C Program: 2 A simple C Program: 3 Creation, Compilation and Execution of a C program: 4 Basic structure of a C program: 5 Example C programs with definition, example program and output:

What are the best C programs to learn?

C program for Factorial C program for Fibonacci series C program for Palindrome C program for Swapping 2 numbers with and without temp variable Sample calculator program and bank application program etc. Key points to remember in C programming basics:


2 Answers

If there is only ever one line then do something like this:

using System; using System.IO;  class Program {     static void Main()     {         String[] values = File.ReadAllText(@"d:\test.csv").Split(',');     } } 
like image 37
Andrew Hare Avatar answered Sep 22 '22 12:09

Andrew Hare


You can try the some thing like the below LINQ snippet.

string[] allLines = File.ReadAllLines(@"E:\Temp\data.csv");      var query = from line in allLines                 let data = line.Split(',')                 select new                 {                     Device = data[0],                     SignalStrength = data[1],                     Location = data[2],                      Time = data[3],                     Age = Convert.ToInt16(data[4])                 }; 

UPDATE: Over a period of time, things evolved. As of now, I would prefer to use this library http://www.aspnetperformance.com/post/LINQ-to-CSV-library.aspx

like image 80
Ramesh Avatar answered Sep 23 '22 12:09

Ramesh