|
-
June 17th, 2009, 09:54 AM
#1
C# File Operations
Hi,
I am working on a method which adds a new line in a text file.
The line should be of this format.
Line name (length 15, start position 0)
Project name (length 20, start position 0 + 15)
date (length 8, start position 0+15+20 )
For example i just took 3 fields, in real i have to deal with more than 10 fields
Length is the maximum length. If the length is less then it adds spaces. if the length is greater than than required length then it should erase that entire line.
Ex:
TestLineName TestProjectName 06172009 --- This is correct line
TestLineName TestProjectNameAgain --- this is incorrect as TestProjectNameAgain exceeds max limit. Now how can i remove only the second line?
I am able to create a new line when all conditions are fine but when there is any exception then I am not sure how to delete that line. Can any one help me please?
code:
int startPosition = 0
using (FileStream fs = new FileStream(saveToPath, FileMode.Append, FileAccess.Write))
{
if (Line_Name.Length > 15)
throw new Exception();
//and delete the current line... how to implement?
else
bytesToWrite = System.Text.ASCIIEncoding().GetBytes(Line_Name);
fs.Seek(startPosition , SeekOrigin.Begin);
fs.Write(bytesToWrite, 0, bytesToWrite.Length);
if (Project_name.Length > 20)
throw new Exception();
//and delete the current line... how to implement?
else
bytesToWrite = System.Text.ASCIIEncoding().GetBytes(Project_name);
fs.Seek(startPosition + 15, SeekOrigin.Begin);
fs.Write(bytesToWrite, 0, bytesToWrite.Length);
if (createDate.Length > 8)
throw new Exception();
//and delete the current line... how to implement?
else
bytesToWrite = System.Text.ASCIIEncoding().GetBytes(createDate);
fs.Seek(startPosition + 35, SeekOrigin.Begin);
fs.Write(bytesToWrite, 0, bytesToWrite.Length);
}
I am stuck at two things.
1. Not able to delete the current line
2. Is there any better way to do entire funtionality?
Please help me.
TIA
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|