写文件问题[覆盖] [英] Write to File Problem[Overriding]

查看:107
本文介绍了写文件问题[覆盖]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我有一个小问题.让我解释一下工作场所和问题.

设计:

有5个文本框,分别称为 txtCellId txtCellCenterX txtCellCenterY txtCellCenterZ txtCellRadius 和名为 btnSave 的按钮将其写入txt文件.

我的文本文件结构:

X_OFFSET 0
Y_OFFSET 0
电池
1222222222222
2250500250
3 1700 500 500 250
4900600500250
5 1350 250 500 250
电池

我必须做的是找到CELL-CELLEND块并在它们之间写入.

示例:用户设置1 123 124 125 126 => 1表示ID,123表示中心X,124表示中心Y,125表示中心Z,126表示半径.

我编写的代码:

Hey Everyone.

I have a little problem. Let me explain the workplace and the problem.

The Design:

There are 5 TextBoxes which are called txtCellId, txtCellCenterX, txtCellCenterY, txtCellCenterZ, txtCellRadius and a Button named btnSave to write these to a txt File.

My Text File Structure:

X_OFFSET 0
Y_OFFSET 0
CELL
1 222 222 222 222
2 250 500 250
3 1700 500 500 250
4 900 600 500 250
5 1350 250 500 250
CELLEND

What I must do is to find the CELL-CELLEND block and write between them.

Example: User set 1 123 124 125 126 = > 1 means ID, 123 means Center X, 124 means Center Y, 125 means Center Z and 126 means Radius.

Code that I write:

public void AddCellProperty()
{
FileStream fs2 = new FileStream("C:\\Documents and Settings\\Administrator\\cell.txt", FileMode.Open);
StreamReader SR2 = new StreamReader(fs2);
string file = SR2.ReadToEnd();

string hucreKimlik = txtCellId.Text;
string hucreMerkezX = txtCellCenterX .Text;
string hucreMerkezY = txtCellCenterY.Text;
string hucreMerkezZ = txtCellCenterZText;
string hucreYaricapi = txtCellRadius.Text;

StreamWriter SW = new StreamWriter(fs2);

string lines = "CELL";
int index = file.IndexOf(lines);
int length = Convert.ToInt32(lines.Length.ToString());
fs2.Seek(length + index + 1, SeekOrigin.Begin);

SW.WriteLine();
SW.WriteLine(hucreKimlik  + '' '' + hucreMerkezX  + '' '' + hucreMerkezY + '' '' + hucreMerkezZ + '' '' + hucreYaricapi);

SW.WriteLine();
SW.Flush();
SW.Close();
SR2.Close();
}



当按下按钮时,一切都做好了,并保存到文件中.
但是,当我要添加另一个单元格时,它会覆盖第一个单元格属性.

我该如何克服呢?需要紧急帮助...

我最好的问候...



When press button, everything is nicely done and saved to file.
But, when I want to add another cell, it overrides the first cell properties.

How can I overcome this? Need urgent help...

My best Regards...

推荐答案

我建​​议使用File.AppendText(FileName, NewText)


据我了解,您有一种情况您要覆盖文件中的CELL/CELLEND块,而另一种情况是要向文件中添加新块.

因此,您将不得不考虑这两种情况.您现在显示的代码始终可以找到第一个出现的CELL块并覆盖这些值.

一种方法是实现两个功能(一个用于添加一个用于覆盖的功能).当您要添加新块时,只需将其附加到现有文件中即可.如果要覆盖文件中的特定块(而不是现在的第一个块),则必须指定一个参数,指出应该覆盖哪个块(例如,ID或仅块号)在文件中).
As i understand it you have one case where you want to override a CELL/CELLEND block in your file and another case were you want to add a new block to the file.

So you''ll have to consider these two cases. The code you''ve showed right now allways finds the first occuring CELL block and overwrites the values.

One way is to make two functions (one for adding one for overriding). When you want to add a new block just append it to the exiting file. If you want to override a specific block (not allways the first one as it is right now) in the file you''ll have to specify a parameter that states which block should be overriden (e.g. an ID or just the number of the block in the file).


您可以使用它.

昏暗的堆栈作为新的System.Collections.Stack
Dim SR作为System.IO.StreamReader
SR = File.OpenText("C:\\ Documents and Settings \\ Administrator \\ cell.txt")

昏暗的LogLine作为字符串
You can use this.

Dim Stack As New System.Collections.Stack
Dim SR As System.IO.StreamReader
SR = File.OpenText("C:\\Documents and Settings\\Administrator\\cell.txt")

Dim LogLine As String
Do
                LogLine = SR.ReadLine
                If LogLine Is Nothing Then
                    Exit Do
                Else
                    Stack.Push(LogLine)
                End If
            Loop Until LogLine Is Nothing


            LogLine = Stack.Pop

If Trim(LogLine) = "Enter your last character here" Then
  LogLine = Stack.Pop
  LogRead = LogLine

  'You can start coding here

else
'Put your message that its still on the process
End if


这篇关于写文件问题[覆盖]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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