Solved

retrieve int from text file

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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!
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

749 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