Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言从文本文件中提取数据#_C#_File_Filestream_Streamreader - Fatal编程技术网

C# 用C语言从文本文件中提取数据#

C# 用C语言从文本文件中提取数据#,c#,file,filestream,streamreader,C#,File,Filestream,Streamreader,我正在尝试使用c从文本文件中提取数据# 文件的内容如下所示- 2002/01/10 00:44:51.53 40.4415 -126.0167 25.37 3.92 Md 56 269 147 0.29 NCSN 21208454 现在我希望文本文件中的数据存储在如下变量中- 2002/01/10 00:44:51.53 40.4415 -126.0167 25.37 3.92 Md 56 269 147 0.29 NCSN 21208454 日期

我正在尝试使用c从文本文件中提取数据# 文件的内容如下所示-

2002/01/10 00:44:51.53  40.4415 -126.0167  25.37  3.92   Md   56 269  147 0.29 NCSN   21208454 
现在我希望文本文件中的数据存储在如下变量中-

2002/01/10 00:44:51.53  40.4415 -126.0167  25.37  3.92   Md   56 269  147 0.29 NCSN   21208454 
日期=2002/01/10

时间=00:44:51.53

lat=40.4415,依此类推

下面是我使用的代码片段

     public class data
  {
  public string date, time;
  public double lat, lon,depth,mag;
  }

 class Program
 {

static void Main(string[] args)
{
    string dt;
    List<data> gd = new List<data>();
    using (StreamReader sr = new StreamReader("E:\\op.html"))
    {
        while (sr.Peek() > 0)
        {
            string str;
            string[] arr;
            str = sr.ReadLine();
            arr = str.Split(' ');
            data d = new data();
            d.date = arr[0];
            //d.time = arr[1];
            //d.lat = Convert.ToDouble( arr[2]);
            //d.lon = Convert.ToDouble(arr[3]);
            //d.depth = Convert.ToDouble(arr[4]);
            //d.mag = Convert.ToDouble(arr[5]);


            Console.WriteLine(d.date);
            //Console.WriteLine(d.time);
            //Console.WriteLine(d.lat);
            //Console.WriteLine(d.lon);
            //Console.WriteLine(d.depth);
            //Console.WriteLine(d.mag);
            Console.ReadKey();

        }
公共类数据
{
公共字符串日期、时间;
公共双lat、lon、深度、mag;
}
班级计划
{
静态void Main(字符串[]参数)
{
字符串dt;
List gd=新列表();
使用(StreamReader sr=newstreamreader(“E:\\op.html”))
{
而(sr.Peek()>0)
{
字符串str;
字符串[]arr;
str=sr.ReadLine();
arr=str.Split(“”);
数据d=新数据();
d、 日期=arr[0];
//d、 时间=arr[1];
//d、 lat=转换为双(arr[2]);
//d、 lon=转换为双(arr[3]);
//d、 深度=Convert.ToDouble(arr[4]);
//d、 mag=转换为双(arr[5]);
控制台写入线(d.日期);
//控制台写入线(d时间);
//控制台写入线(d.lat);
//控制台写入线(d.lon);
//控制台写入线(d深度);
//控制台写入线(d.mag);
Console.ReadKey();
}

但是我只得到d.date的值,不能得到其余的值。标有“/”的行抛出一个错误“IndexOutOfBound”错误。如何解决这个问题?

您需要将
httpWReq.Proxy.Credentials
设置为带有代理身份验证信息的
NetworkCredentials

公共类数据
public class data
{
    public string date, time,mag;
    public double lat, lon, depth;
}

class Program
{

    static void Main(string[] args)
    {
        string str;

        string dt;
        List<data> gd = new List<data>();
        using (StreamReader sr = new StreamReader("E:\\op.html"))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                string [] arr = line.Split(new char[] {' '}, System.StringSplitOptions.RemoveEmptyEntries);
                while (sr.Peek() > 0)
                {

                    string[] s = new string[] { " " };
                    char[] c = new char[] { ' ' };
                    str = sr.ReadLine();
                    arr = str.Split(c);

                    data d = new data();
                    d.date = arr[0];
                    d.time = arr[1];

                    d.lon =Convert.ToDouble(arr[3]);
                    d.depth = Convert.ToDouble (arr[4]);
                    d.mag = arr[8];
                    //                    File.WriteAllText("E:\\abc.txt",d.date);

                    Console.WriteLine(d.date);
                    Console.WriteLine(d.time);
                    //Console.WriteLine(d.lat);
                    Console.WriteLine(d.lon);
                    Console.WriteLine(d.depth);
                    Console.WriteLine(d.mag);
                    int x = arr[0].Length;
                    Console.WriteLine(x);
                    Console.ReadKey();

                }
{ 公共字符串日期、时间、杂志; 公共双lat、lon、深度; } 班级计划 { 静态void Main(字符串[]参数) { 字符串str; 字符串dt; List gd=新列表(); 使用(StreamReader sr=newstreamreader(“E:\\op.html”)) { 弦线; 而((line=sr.ReadLine())!=null) { string[]arr=line.Split(新字符[]{''},System.StringSplitOptions.RemoveEmptyEntries); 而(sr.Peek()>0) { 字符串[]s=新字符串[]{”“}; char[]c=新的char[]{''}; str=sr.ReadLine(); arr=str.Split(c); 数据d=新数据(); d、 日期=arr[0]; d、 时间=arr[1]; d、 lon=转换为双(arr[3]); d、 深度=Convert.ToDouble(arr[4]); d、 mag=arr[8]; //File.writealText(“E:\\abc.txt”,d.date); 控制台写入线(d.日期); 控制台写入线(d时间); //控制台写入线(d.lat); 控制台写入线(d.lon); 控制台写入线(d深度); 控制台写入线(d.mag); int x=arr[0]。长度; 控制台写入线(x); Console.ReadKey(); }
那不是本地文件吗?在调用ReadLine后检查str的值。它是您期望的吗?