[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

retrieve int from text file

Posted on 2011-02-22
5
Medium Priority
?
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 280 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 200 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 1520 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.

656 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