标签: streamreader

C# StreamReader 可以检查当前行号吗?

我尝试制作一个脚本来逐行读取 TXT 文件,并根据里面的内容更改标签。有没有办法检查正在读取哪一行?

c# streamreader

3
推荐指数
1
解决办法
481
查看次数

用文本文件填充两个一维数组

对于我的 Visual Basic 期末考试,我的程序需要将文本文件中的数据读取到两个不同的数组中,每个数组都是一维的。以下是我这样做的代码:

Option Explicit On
Option Infer Off
Option Strict On

Public Class frmMain

    'Constant for filename and a dirty flag variable
    Const INVENTORY_FILENAME As String = "inventory.txt"
    Dim noFile As Boolean = False

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Populates DVD listbox with text file data upon load

        'Variable for reading the file
        Dim myFile As IO.StreamReader

        'Declaring arrays for DVD names and prices
        Dim arrayDVD() As String
        Dim arrayPrice() As Double

        'Variables …
Run Code Online (Sandbox Code Playgroud)

vb.net arrays streamreader

3
推荐指数
1
解决办法
53
查看次数

如何读取仅由LF分隔的文件中的每一行?

我必须逐行读取日志文件.它的大小约为6MB,总线数为40000.但在测试我的程序后,我发现该日志文件仅由LF字符分隔.所以我不能使用类的Readline方法StreamReader

我该如何解决这个问题?

编辑:我尝试使用文本阅读器,但我的程序仍然无法正常工作:

using (TextReader sr = new StreamReader(strPath, Encoding.Unicode))
            {


                sr.ReadLine(); //ignore three first lines of log file
                sr.ReadLine(); 
                sr.ReadLine();

                int count = 0; //number of read line
                string strLine;
                while (sr.Peek()!=0)
                {
                    strLine = sr.ReadLine();
                    if (strLine.Trim() != "")
                    {
                        InsertData(strLine);
                        count++;
                    }
                }

                return count;
            }
Run Code Online (Sandbox Code Playgroud)

c# file streamreader

2
推荐指数
1
解决办法
5950
查看次数

有没有办法从StreamReader中删除第一行

我有一个加载csv文件的streamreader,问题是我想忽略的数据顶部有一个额外的行.

我正在使用CSVReader项目,这很棒但是

 csv.GetFieldHeaders(); 
Run Code Online (Sandbox Code Playgroud)

总是看第一排.而不是进入CSVReader源代码并更改它,我认为如果我可以在将它注入csvReader之前从流​​读取器本身"剥离"第一行可能更容易.这可能吗?

c# csv streamreader

2
推荐指数
1
解决办法
2425
查看次数

XNA StreamReader/Writer如何在不同的行中读/写?

我正在尝试通过StreamWriter编写一个简单的.txt文件.我希望它看起来像这样:

  • 12
  • 26
  • 100

如此简单的数字.但是如何告诉读/写器写入或读取哪一行.

http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

这里说它ReadLine()读取当前流的一行.但我怎么知道它是哪条线.或者它总是第一个?

我想读取数字,修改它们然后再写回来.

有什么建议?

提前致谢!

c# xna newline streamwriter streamreader

2
推荐指数
1
解决办法
875
查看次数

如何将StreamReader转换为XDocument?

我在IsolatedStorage中存储XML数据,同时从IsolatedStorage读取数据.我需要将StreamReader转换为XDocument.以下代码我已经习惯将StreamReader转换为XDocument.我收到一个错误:"root element is missing"

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {                   
                IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("AllHeadLine.xml", FileMode.Open);
                using (StreamReader reader = new StreamReader(isoFileStream))
                {                        
                    displayXmlData.Text = reader.ReadToEnd();                       
                    XDocument offlineHeadline = XDocument.Load(reader);

                }
            }
Run Code Online (Sandbox Code Playgroud)

编辑:XML内容

<category><catname>?????</catname><img>http://www.udayavani.com/udayavani_cms/gall_content/2013/3/2013_3$thumbimg113_Mar_2013_235853890.jpg</img><heading>???? ??? ????? ?????? ???</heading><navigateurl>some Url </navigateurl></category>
Run Code Online (Sandbox Code Playgroud)

怎么解决这个?

xml isolatedstorage linq-to-xml streamreader windows-phone-7

2
推荐指数
1
解决办法
5014
查看次数

如何将.csv文件的每一行分隔为字符串列表>

我是c#的新手,我正在尝试读取.csv文件并将每行文本放入一个单独的列表项中,以便稍后对其进行排序.

.csv文件的组织方式如下:

1;"final60";"英国";"2013-12-06 15:48:16";
2;"donnyr8";"荷兰";"2013-12-06 15:54:32"; 等等

这是我的第一次尝试不起作用.它在Visual Studio 2010中没有显示错误,但是当我运行控制台程序时,它显示以下异常而不是列表. Exception of type 'System.OutOFMemoryException' was thrown.这很奇怪,因为.csv文件只包含一个小列表.

try
{
// load csv file
using (StreamReader file = new StreamReader("file.csv"))
      {
       string line = file.ReadLine();
       List<string> fileList = new List<string>();
       // Do something with the lines from the file until the end of  
       // the file is reached. 
       while (line != null)
       {

          fileList.Add(line);

        }
        foreach (string fileListLine in fileList)
         {
            Console.WriteLine(fileListLine);
         }
       }
}
catch (Exception e)
{
  // Let …
Run Code Online (Sandbox Code Playgroud)

c# file list streamreader

2
推荐指数
2
解决办法
860
查看次数

如何正确处理C#中的空行,空行或空行

我有一些正在处理文本文件的C#代码,但似乎无法使它与空白或空(空格)行一起正常工作。

我的代码:

        while (!file.EndOfStream)
        {
            line = file.ReadLine();

            bool isComment = (line[0] == '/') && (line[1] == '/');
            bool isPoint = (line[0] == '(') && (line[line.Length - 1] == ')');
            bool isWhiteSpace = string.IsNullOrEmpty(line);

            Debug.Log("Comment: " + isComment + ", Point: " + isPoint + ", WhiteSpace: " + isWhiteSpace + "Value: '" + line + "'");

            if (!isComment && !isPoint && !isWhiteSpace) { Application.Quit(); }
            else if (isPoint)
            {
                //Strip parenthesis
                line = line.Remove(line.Length - 1, 1).Remove(0, 1);

                //break …
Run Code Online (Sandbox Code Playgroud)

c# streamreader unexpectendoffile

2
推荐指数
1
解决办法
1万
查看次数

StreamReader输出

在C#中,为什么这两个代码的输出彼此不同?

StreamReader test = new StreamReader(@"C:\a.txt");

while (test.ReadLine() != null)
{
    Console.WriteLine(test.ReadLine());
}
Run Code Online (Sandbox Code Playgroud)

这段代码:

StreamReader test = new StreamReader(@"C:\a.txt");

string line = "";

while ((line = test.ReadLine()) != null)
{
    Console.WriteLine(line);
} 
Run Code Online (Sandbox Code Playgroud)

c# streamreader output

2
推荐指数
1
解决办法
350
查看次数

从大文件中以非升序读取多行

我有一个非常大的文本文件,超过1GB,并且有一个代表行号的整数列表,需要生成另一个文件,其中包含新文件中原始文件行号的文本。

原始大文件示例:

ogfile line 1
some text here
another line
blah blah
Run Code Online (Sandbox Code Playgroud)

因此,当我得到“ 2,4,4,1”的列表时,输出文件应为:

some text here
blah blah
blah blah
ogfile line 1
Run Code Online (Sandbox Code Playgroud)

我试过了 string lineString = File.ReadLines(filename).Skip(lineNumList[i]-1).Take(1).First();

但这要花很长时间,因为必须读取文件,跳到有问题的行,然后在下一次重新读取...而且我们正在谈论1GB文件中的数百万行,而我List<int>是成千上万的行号。

是否有更好/更快的方法来读取单行,或者让读者跳到特定的行号而不逐行“跳过”?

c# file streamreader filereader

2
推荐指数
1
解决办法
275
查看次数