c#中如何把txt文件中的指定一行赋值给一个变量,txt中每行只有一个数字

KDS110 2015-10-09 08:44:37
c#中如何把txt文件中的指定一行赋值给一个变量,txt中每行只有一个数字
比如有X,Y
Y<100 ,X=txt文件中的第一行
Y>100 & Y<200, X=txt文件中的第二行
该如何用代码表达?
...全文
389 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
john_QQ:2335298917 2015-10-11
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
double[] arr = System.IO.File.ReadAllLines(@"e:/Test.txt", System.Text.Encoding.Default).Select(p => double.Parse(p.Trim())).ToArray();
return;
}
}
}
正宗熊猫哥 2015-10-10
  • 打赏
  • 举报
回复
神速。。。。
双鱼星星 2015-10-09
  • 打赏
  • 举报
回复
引用 3 楼 KDS110 的回复:
[quote=引用 1 楼 ajianchina 的回复:] 其他的我相信你的智商是可以搞定的,关键是读取文件中每行的数字到数组中,这个我写一下:

int[] arr = System.IO.File.ReadAllLines(@"e:/Test.txt", System.Text.Encoding.Default).Select(p=>int.Parse(p.Trim())).ToArray();
//譬如第三行的数字:arr[2];
满意就结贴
然而我并不能看懂。。。 能详细点吗[/quote] 拆开就容易理解了。 string filePath = @"d:\Test.txt"; string[] lines = File.ReadAllLines(filePath,Encoding.Default); IEnumerable<int> results = lines.Select(p=>int.Parse(p.Trim())); int[] arr = results.ToArray(); int y=50; int x=0; if (y < 100) { x = arr[0]; } else if (y >= 100 && y < 200) { x = arr[2]; } Console.WriteLine("{0}\n", x);
john_QQ:2335298917 2015-10-09
  • 打赏
  • 举报
回复
11 22 33 44.4
john_QQ:2335298917 2015-10-09
  • 打赏
  • 举报
回复
引用 4 楼 KDS110 的回复:
[quote=引用 1 楼 ajianchina 的回复:] 其他的我相信你的智商是可以搞定的,关键是读取文件中每行的数字到数组中,这个我写一下:

int[] arr = System.IO.File.ReadAllLines(@"e:/Test.txt", System.Text.Encoding.Default).Select(p=>int.Parse(p.Trim())).ToArray();
//譬如第三行的数字:arr[2];
满意就结贴
这个运行不成功呀,他说lines是未声明标示符[/quote] VS2010 FrameWork4.0运行成功,你的每行数字是不是不全是整数?如果有小数,就用
double[] arr = System.IO.File.ReadAllLines(@"e:/Test.txt", System.Text.Encoding.Default).Select(p => double.Parse(p.Trim())).ToArray();
john_QQ:2335298917 2015-10-09
  • 打赏
  • 举报
回复
test.txt内容:
KDS110 2015-10-09
  • 打赏
  • 举报
回复
引用 1 楼 ajianchina 的回复:
其他的我相信你的智商是可以搞定的,关键是读取文件中每行的数字到数组中,这个我写一下:

int[] arr = System.IO.File.ReadAllLines(@"e:/Test.txt", System.Text.Encoding.Default).Select(p=>int.Parse(p.Trim())).ToArray();
//譬如第三行的数字:arr[2];
满意就结贴
这个运行不成功呀,他说lines是未声明标示符
KDS110 2015-10-09
  • 打赏
  • 举报
回复
引用 1 楼 ajianchina 的回复:
其他的我相信你的智商是可以搞定的,关键是读取文件中每行的数字到数组中,这个我写一下:

int[] arr = System.IO.File.ReadAllLines(@"e:/Test.txt", System.Text.Encoding.Default).Select(p=>int.Parse(p.Trim())).ToArray();
//譬如第三行的数字:arr[2];
满意就结贴
然而我并不能看懂。。。 能详细点吗
ajianchina 2015-10-09
  • 打赏
  • 举报
回复
其他的我相信你的智商是可以搞定的,关键是读取文件中每行的数字到数组中,这个我写一下:

int[] arr = System.IO.File.ReadAllLines(@"e:/Test.txt", System.Text.Encoding.Default).Select(p=>int.Parse(p.Trim())).ToArray();
//譬如第三行的数字:arr[2];
满意就结贴
  • 打赏
  • 举报
回复
string[] lines = File.ReadAllLines("your path");//如果判断范围少的话,也可以循环一行行读取
int y;
string x;
if (y < 100)
{
    x = lines[0];
}
else if (y >= 100 && y < 200)
{
    x = lines[2];
}
//感觉你这个比喻不就是整除100嘛……

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧