?
Solved

retrieve int from text file

Posted on 2011-02-22
5
Medium Priority
?
288 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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

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!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

771 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