Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
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...
Posted

I would recommend for File.AppendText(FileName, NewText)
 
Share this answer
 
Comments
Un_NaMeD 27-May-11 4:22am    
I tried but couldn't succeed. Can you show a little pseudo code?
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).
 
Share this answer
 
Comments
Un_NaMeD 27-May-11 3:53am    
Hi.
I don't want to override any entry.

Here's the problem.

First entry: 1 242 252 135 50

When user enters these, it writes to file

Second entry: 2 345 234 512 50

When user enters these, it writes to file but first entry disappears.
I want it to be still there.
Legor 27-May-11 5:46am    
Okay, given that you know the ID of the entry that should be written in this block.

First of all you need to find the Block in your file. This is done sucessfully as it seems right now.

Then from that point you'll need to Read the next lines one by one,

Store the actual line number / stream position.

Look at the first value in the actual lines (of course expected that the ID is allways at the first position of a line).

If the value ist the ID you want to add

(maybe overwrite it) or give error message .. depends on your requirements

Else if the value is CELLEND
Add the new entry to the remembered line and finish

I think this can be realized even simpler by some given stream functions.
Dalek Dave 27-May-11 4:02am    
Good Answer
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
VB
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
 
Share this answer
 
Comments
Un_NaMeD 27-May-11 3:50am    
Could you write you code with C# please?
cuteband 27-May-11 4:24am    
Please run this script in VB to c# ocnverter. and this will reallly help. use this link
http://www.developerfusion.com/tools/convert/vb-to-csharp/
The problem is the point from where are you writing wont shift rest of the text ahead it will overwrite it so the idea to write in between something is
1- get the index where you want to write
2- get the string after the index and store in some temp variable
3 - write your new text at the index
4- write value of your temp variable.

Replace
MIDL
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);



With
MIDL
string lines = "CELLEND";
int index = file.IndexOf(lines);
string restOfTheString = file.Substring(index);
fs2.Seek(index, SeekOrigin.Begin);

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


Hope this will help

Thanks,
Hemant
 
Share this answer
 
Comments
Un_NaMeD 31-May-11 7:50am    
Man, really great idea, Thank you it works great :)
My 5...
Hemant__Sharma 31-May-11 22:50pm    
You are welcome :). and thanks for the vote.
you should consider writing a peace of code like this (pseudocode)
open sourcefile
open targetfile
while not eof(sourcefile)
   if time to write new data -> do it
   write line from sourcefile
end

close sourcefile
close targetfile

and please do not ask me to code this in c# - its pseudocode
 
Share this answer
 
You are explicitly seeking to the position of the first cell. If you don't want to write to that position in the file (which won't work in all cases anyway as your cell record is not a fixed length), seek to the end.

If you want to be able to overwrite a single record in place, you need to make sure that the format is a fixed length, which at the moment it is not.
 
Share this answer
 
As far as i understood,,,overwriting problem is there.
change ur streamwriter with this

C++
StreamWriter SW = new StreamWriter(fs2,true);
 
Share this answer
 
Comments
Un_NaMeD 27-May-11 3:49am    
Thx for the answer but it still overrides the first cell, and code gives error..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900