0

我有一个 .CSV 文件,其中包含如下所示的数据:

Sana Paden,1098,64228,46285,2/15/2011
Ardelle Mahr,1242,85663,33218,3/25/2011
Joel Fountain,1335,10951,50866,5/2/2011
Ashely Vierra,1349,5379,87475,6/9/2011
Amado Loiacono,1406,62789,38490,7/17/2011
Joycelyn Dolezal,1653,14720,13638,8/24/2011
Alyse Braunstein,1657,69455,52871,10/1/2011
Cheri Ravenscroft,1734,55431,58460,11/8/2011
Russ Leth,9720,77542,72705,12/16/2011

我试图在使用 Filestream 读取数据时设置指向行的指针。

  1. 我可以在 C# 中设置指向文件每一行的 HDD 位置的指针吗?
  2. 我可以看到每行有多少字节,但我不知道每行从哪里开始......
  3. 您可以在流本身中确定每行终止的位置吗?

此代码确实读取了当前没有问题的文件:

public static void seeks()
    {
        using (FileStream fs = new FileStream(@"C:\Users\bussard\Documents\James T work\SourceDatatoedit.csv", FileMode.Open, FileAccess.Read))
        {
            fs.Seek(0, SeekOrigin.Begin);
            while ((nextByte = fs.ReadByte()) >= 0)
            {
                Console.Write(Convert.ToChar(nextByte));
            }
            Console.WriteLine();
        }

    }
4

3 回答 3

0

我希望我能正确地标记你的答案达伦和布兰德,因为你给我的建议帮助我找到了我在这里寻找的答案。我能够根据新行出现的位置建立一个基于指针的系统。让我发布我的代码,向您展示我做了什么。

        public static void seeks()
    {

        long offset = 0;
        string streamsample = "";
        int numoflines = 0;
        using (FileStream fs = new FileStream(@"C:\Users\bussard\Documents\JamesTwork\SourceDatatoedit.csv", FileMode.Open, FileAccess.Read))
        {
            fs.Seek(offset, SeekOrigin.Begin);
            StreamReader sr = new StreamReader(fs);
            { //determines how many lines are in my file.
                while (!sr.EndOfStream)
                {
                    streamsample = sr.ReadLine();
                    numoflines++;

                }// end while block
                Console.WriteLine("Lines: " + numoflines);
            }//end stream sr block

            long[] dataArray = new long[numoflines]; //create a long array based on amount of lines

            fs.Seek(offset, SeekOrigin.Begin);
            StreamReader dr = new StreamReader(fs);
            {create an array of long values based on where new lines occur as pointers.

                numoflines = 0;
                streamsample = "";

                while (!dr.EndOfStream)
                {
                    streamsample = dr.ReadLine();
                    dataArray[numoflines] = offset;
                    offset += streamsample.Length - 1;
                    numoflines++;
                }// end while

                int j = 0;

                while (j < numoflines)
                { // displays the pointer and what data is at that point.
                    fs.Seek(dataArray[j], SeekOrigin.Begin);
                    Console.WriteLine("Pointer: " + dataArray[j].ToString() + " String: " + dr.ReadLine().ToString());
                    j++;
                }//end while
            }//end streamReader dr block

        }//end filestream fs block

    }// end method seeks ()
于 2013-05-19T00:34:50.740 回答
0

我会假设你的意思是\r\n?对于您想要做的事情,我会假设您会想要这样的东西

using (FileStream fs = new FileStream(path))
using (StreamReader sr = new StreamReader(fs))
{
    while (!sr.EndOfStream)
        Console.WriteLine(sr.ReadLine());
}

StreamReader 类非常适合处理文本文件。

于 2013-05-16T00:48:39.953 回答
0

读取时存储 FileStream 的Position。然后使用Seek导航到位置,以便您可以在存储/所需位置进行阅读。我认为您会将字节数与此位置一起存储,这样您就可以在此时执行Read而不是 ReadByte 了。

于 2013-05-16T02:36:49.193 回答