|
my t1.txt is:
1
5
18
125
.
.
.
I want to read a specific value from txt file( sample 3.value that is 18)
using System;<br />
using System.Collections.Generic;<br />
using System.IO;<br />
<br />
class Program<br />
{<br />
static void Main()<br />
{<br />
const string f =(@"C:\t1.txt");<br />
<br />
List<string> lines = new List<string>();<br />
<br />
using (StreamReader r = new StreamReader(f))<br />
{<br />
string line;<br />
while ((line = r.ReadLine()) != null)<br />
{<br />
lines.Add(line);<br />
}<br />
}<br />
<br />
foreach (string s in lines)<br />
{ <br />
Console.WriteLine(s);<br />
}<br />
<br />
}<br />
}</string></string>
|
|
|
|
|
You could try using File.ReadAllLines[^] which returns an array of data from your file.
If you want to refer to specific line (for example 3 as in your post) you would simply use ...yourvariable[2]...
|
|
|
|
|
this means you want to read the value from line X?
you may implement a helper method wich takes the StreamReader and returns the value contained in a file at line X. use a loop to skip the lines and count them.
Calin
|
|
|
|
|
If you want a value from the specific line, you can simply count the lines and use the value from the correct one. Just like this:
string line;
int current_line = 0;
int desired_line = 3;
while ((line = r.ReadLine()) != null)
{
current_line++;
if(current_line == desired_line)
{
lines.Add(line);
break;
}
}
This is just a sample logic. I hope you can use something like that for your case.
|
|
|
|
|
|
To comment on this post, I'd write this code a little different and smaller, like this.
It maybe is just my style, but I think it's better:
int desired_line = 3;
for(int current_line = 0, string line; (line = r.ReadLine()) != null; current_line++)
{
if(current_line == desired_line)
{
lines.Add(line);
break;
}
}
|
|
|
|
|
thanks for replies
t1.txt is:
11
22
33
44
55
.
.
how can I get x value? x=a(line's 1 value)+b(line's 3 value)+c(line 5)that is c must 99
using System;<br />
using System.IO;<br />
<br />
class MainClass<br />
{<br />
public static void Main(string[] args)<br />
{<br />
try<br />
{<br />
FileStream fs = new FileStream("c:\\t1.txt", FileMode.Open);<br />
StreamReader sr = new StreamReader(fs);<br />
<br />
string line = "";<br />
<br />
int lineNo = 0;<br />
do<br />
{<br />
line = sr.ReadLine();<br />
if (line != null)<br />
{<br />
Console.WriteLine("{0}: {1}", lineNo, line);<br />
lineNo++;<br />
x=a+b+c;-------->values=?<br />
}<br />
} while (line != null);<br />
}<br />
catch (Exception e)<br />
{<br />
Console.WriteLine("Exception in ShowFile: {0}", e);<br />
}<br />
}<br />
}
|
|
|
|
|
First of all: use the pre tag, it's better than the code tag.
Secondly it is not as tough as you think. Just use common sense for this question:
You are using the do while construction, but if I where you, I should use the while function or for function.
List<string> lines = new List<string>();
String line;
while((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
try
{
int numberOne = Int32.Parse(lines[0]);
int numberThree = Int32.Parse(lines[2]);
int numberFive = Int32.Parse(lines[4]);
int x = numberOne + numberThree + numberFive;
}
catch {}
</string></string>
You could use the TryParse method, but you can also use the try catch block.
Good luck!
|
|
|
|
|
wonderful
thank you very much
|
|
|
|
|
Hi!
I have a world map in my GUI and I want the countries to highlight when mouse hovers over a country. Something like this http://www.netzgesta.de/mapper/
Is this possible to do this with c# at all?
I have read about the Imagemap controller with which it is possible to do polygon hotspots, but I'm not sure this is exactly what I need. It can't change anything within the coordinates of the polygon when mouse hovers over the hotspots.
Can someone give me a clue what to do?
|
|
|
|
|
|
LMAO! Just posted the exact same link
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Yes, but it doesn't have that hover effect.
|
|
|
|
|
So use it as a base and program the hover effect yourself.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
There's an article here[^] that might help
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
hello,
I would stock a list of string, i make this
<br />
string[] TableauString;<br />
<br />
TableauString[tt]= (nom.Trim() + ";" + prenom.Trim() + ";" + adresse1.Trim() + ";" + cp.Trim() + ";" + ville.Trim() + resultat3.Trim() + resultat2.Trim());<br />
tt++;<br />
<br />
there are exception : the referce is not instance of obkect, help me please to resolve this problem, thank you verry mutch
|
|
|
|
|
The exception occurs because you don't allocate any memory for the string array. Try something like this:
string[] TableauString = new string[10];
Here i allocate a memory for 10 elements. This is only an example. You have to specify correct size for the array depending on your needs.
|
|
|
|
|
If i don't know the size of my elements?? thank you verry mutch
|
|
|
|
|
You can use the code as shown bellow just replace
"The Size" with an int number
TestArray= Array.CreateInstance(typeof(String),"The Size");
this will create a array based on the size that you want
hopes this helps
|
|
|
|
|
Then you can use ArrayList. Here is a sample code:
using System.Collections;
........
ArrayList myList = new ArrayList();
myList.Add(new string(" Here is your string "));
for(int i=0; i<5; i++)
myList.Add(new string(" Here is your string "));
|
|
|
|
|
When i declare array list, this error appear : Erreur 1 The type or namespace name 'ArrayList' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Administrateur\Mes documents\Visual Studio 2005\Projects\Form@Home1.0\WindowsApplication2\Form1.cs 20 83 WindowsApplication2
Thank you verry mutch
|
|
|
|
|
You have to insert the next "using" directive at the top of your source file:
using System.Collections;
|
|
|
|
|
Thank you verry mutch, i would use this to read from mylist :
foreach (Array ee in myList) {MessageBox.Show(ee)} , bu there are an exception, thank you verry mutch
|
|
|
|
|
The type of "ee" must be string, not an Array. Use this:
foreach (string ee in myList) {MessageBox.Show(ee)}
|
|
|
|
|
i try for foreach (Object obj in myList), and it work, what is the difference between Object and String ?? thank you verry mutch.
|
|
|
|