Solved

retrieve int from text file

Posted on 2011-02-22
5
280 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now