如何计算行数并将其写入同一文件? [英] How do I count the number of lines and write it in the same file?

查看:92
本文介绍了如何计算行数并将其写入同一文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenFileDialog openFileDialog = new OpenFileDialog();
          openFileDialog.Multiselect = true;
          openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
          openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
          if (openFileDialog.ShowDialog(this) == DialogResult.OK)
          {
                string text;
           using (StreamReader r = new StreamReader(openFileDialog.FileName))
           {
               text = r.ReadToEnd();
           }
           Regex RE = new Regex("\\n", RegexOptions.Multiline);
           MatchCollection theMatches = RE.Matches(text);
              using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Append, FileAccess.Write))
              using (StreamWriter sw = new StreamWriter(fs))
              {

                  sw.Write("Count: {0}", theMatches.Count.ToString());
                  sw.Flush();

              }
             }





我的尝试: < br $>


i只想计算文本文件中的行数,并将计数值写入同一文件中,但我写的代码也计算了这一行(sw .Write(Count:{0},theMatches.Count.ToString());)例如。如果文件中没有行为39,则显示计数:40,

,另一件事是我想写入计数:40在顶行,但在此代码中它位于底部。请帮帮我



What I have tried:

i just want to count the no of lines in a text file,and write the count value in the same file,but the code i wrote also counts the this line (sw.Write("Count: {0}", theMatches.Count.ToString());) for eg. if no of line in a file is 39, it shows count: 40,
and another thing is that i want to write the count: 40 in top line, but in this code it is in the bottom. Please help me out

推荐答案

一个相当简单的方法取决于我猜的文件大小



a )使用File.ReadAllLines()将文件中的所有行读入数组 - 这将为您提供一个字符串数组,例如string [] AllFileLines = File.ReadAllLines(某些文件路径);

b )使用int CountOfLines = AllFileLines.Length;

c)然后我会创建一个临时文件

d)写入(Count:{0},CountOfLines)到临时文件

e)然后将数组中的所有行写回到附加它们的文件,如File.AppendAllLines(临时文件路径,AllFileLines)

f)如果你成功到达这里,删除原始文件

g)将临时文件重命名为原始文件
A fairly easy way depends on the size of the file I guess

a) use File.ReadAllLines() to read all the lines from the file into an array - that would give you an array of strings eg string[] AllFileLines = File.ReadAllLines(some file path);
b) use int CountOfLines = AllFileLines.Length;
c) I would then create a temporary file
d) write ("Count: {0}", CountOfLines) to the temporary file
e) Then write all the lines from the array back to the file appending them, as in File.AppendAllLines(temp file path, AllFileLines)
f) if you get here successfully, delete the original file
g) rename the temporary file to the original file


在o / p中必须显示count:40在​​顶行中跟随通过我附加的文件。例如:

o / p:

数:5

123

234

456

789

789
that is in o/p it has to show count: 40 in top line followed by the file i append. for eg:
o/p:
count: 5
123
234
456
789
789


这篇关于如何计算行数并将其写入同一文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆