Solved

retrieve int from text file

Posted on 2011-02-22
5
282 Views
Last Modified: 2013-12-17
Hi Experts,

I have an int like this:
int[] input = {1,2};

And I would like to retrieve the int value from a txt file which locate in the first line. The file I have looks like this:
test.txt:
1,2

Open in new window


As you can see that the value of the first line is 1,2 and I wanna import it to:

int[] input = (the first line of test.txt)

As you have notice that the first line is divided by a comma so I wanna save the whole line with the comma to the int[] input.

Note: I am using c# 2008

Thanks in advance
Regards
0
Comment
Question by:Sat80
5 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 70 total points
ID: 34950454
I don't see the problem
int[] input = {1,2};

will give you:
input[0] = 1
input[1] = 2

When you have a string, you can do the following:
string inputString = "1,2";
input = inputString.Split(',');

will give you:
input[0] = 1
input[1] = 2
0
 
LVL 2

Assisted Solution

by:Twitchlet
Twitchlet earned 50 total points
ID: 34950460
read in the text file and then use string s = txt.split(",") then convert s to an arary
0
 
LVL 8

Accepted Solution

by:
crysallus earned 380 total points
ID: 34950485
string[] lines = File.ReadAllLines(@"C:\test.txt");
List<int> inputs = new List<int>();
foreach (string i in lines[0].Split(','))
	inputs.Add(Convert.ToInt32(i));
int[] input = inputs.ToArray();

Open in new window

Change the filename to suit, and you may wish to surround it with try catch in case file can't be opened or it doesn't contain what you expect.
0
 

Author Comment

by:Sat80
ID: 34950490
Hi, but how can I read the first line only and then assign the line value to int[] input? as I might have more than one line.
0
 

Author Closing Comment

by:Sat80
ID: 34950509
Thank you ALL
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question