| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1430 人关注过本帖
标题:新手求助如果从文件里读入数据
只看楼主 加入收藏
xinqing928
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2014-1-11
收藏
得分:0 
数据是虽然不是自己生成的,但是可以用FORTRAN改为想要的格式,要是怎么样方便读的话,指教我一下如何修改吧
2014-01-13 11:40
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
如果你不能改變文件佈局,照這樣做也是可以的。我中午要外出一下,先提示你一點關鍵的思路,若還搞不定,我晚上回來再改一下現在做好的。

關鍵點:你的數據都是定寬的!頭2行的整數,全部是7位寬,讀數據時按這個寬度讀,然後轉換就是了。浮點數都是10位寬的。

本來一批過讀333個(這數字從NP來)浮點數,是最方便的,但由於多了回車換行符的干擾,要先把它們過濾掉。

授人以渔,不授人以鱼。
2014-01-13 11:42
xinqing928
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2014-1-11
收藏
得分:0 
版主还是贴一下吧,总能学习一下
2014-01-13 14:29
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:20 
我不給你全部做完,你自己參照這個路子改吧:

程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using namespace ConsoleApplication4
{
    class Program
    {
        static void Main(String[] args)
        {
            if (args.Count() == 0)
            {
                Console.WriteLine("請以數據文件名路徑作爲參數啓動程序!");
                return;
            }

            String fileName = args[0].Trim();
            if (!File.Exists(fileName))
            {
                Console.WriteLine("文件{0}不存在,請檢查并重新輸入。", fileName);
                return;
            }

            Int32 intWidth = 7;        // 整型數據的寬度
            Int32 floatWidth = 10;     // 浮點數據的寬度
            Int32 itemsInLine = 10;    // 每行的項數

            Int32 index;

            using (StreamReader file = File.OpenText(fileName))
            {
                Int32 NT = GetInteger(file, intWidth);
                Int32 NP = GetInteger(file, intWidth);
                Int32 DT = GetInteger(file, intWidth);
                file.ReadLine();
                Console.WriteLine("NT={0}, NP={1}, DT={2}", NT, NP, DT);
                for (index = 0; index < 6; ++index)
                {
                    Console.Write("{0},", GetInteger(file, intWidth));
                }
                Console.WriteLine("{0:F3}", GetFloat(file, floatWidth));
                file.ReadLine();
                Int32 counter = 0;
                List<Double> Z = new List<Double>();
                index = 0;
                while (index < NP)
                {
                    if (counter < itemsInLine)
                    {
                        Z.Add(GetFloat(file, floatWidth));
                        ++counter;
                        ++index;
                    }
                    else
                    {
                        file.ReadLine();
                        counter = 0;
                    }
                }
                foreach (Double data in Z)
                {
                    Console.Write("{0} ", data);
                }
                Console.WriteLine("\nZ[]的總項數={0}", Z.Count());
            }
        }

        static Int32 GetInteger(StreamReader file, Int32 width)
        {
            Char[] array = new Char[width];
            if (file.Read(array, 0, width) > 0)
            {
                return Int32.Parse(new String(array));
            }
            else
            {
                return 0;
            }
        }

        static Double GetFloat(StreamReader file, Int32 width)
        {
            Char[] array = new Char[width];
            if (file.Read(array, 0, width) > 0)
            {
                return Double.Parse(new String(array));
            }
            else
            {
                return 0.0;
            }
        }
    }
}


關鍵是那兩個函數,以及在適當時候讀走換行的手法。

授人以渔,不授人以鱼。
2014-01-13 21:40
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
来帮顶 学习下~~

梅尚程荀
马谭杨奚







                                                       
2014-01-13 22:33
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
以下是引用有容就大在2014-1-13 22:33:39的发言:

来帮顶 学习下~~

流竄犯,趕你回水區!

授人以渔,不授人以鱼。
2014-01-13 22:37
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
哈哈  冒个泡而已 不要这么大火气吧

梅尚程荀
马谭杨奚







                                                       
2014-01-13 23:16
xinqing928
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2014-1-11
收藏
得分:0 
万分感谢版主的热心帮忙,已经散分结帖
2014-01-14 09:09
快速回复:新手求助如果从文件里读入数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027906 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved