Share via


Removing the last character of text file using C#

Question

Thursday, March 20, 2008 12:04 PM

Hi All,

 

I am a newbie in C#. I am trying to remove the last character of the text file and then re saving it so that it overrites the original file.

 

After searching on the internet I found that only way a character can be replaced is if you exactly know what the character is. E.g. "#" or "~". In my case it is a text file that is generated on a AS400 Operating System using a old program written in COBOL. I know it is ancient but I must admit it works perfect and does the job so it is still in use.

 

I cannot find that character in the Unicode Character Set on windows Operating System. And hence cannot replace it.

 

Please can someone cast their eyes on the code that I have managed to put together and let me know what can be done inorder to remove the last character of the file. I am also displaying the spanshot of the file below:

 

20/03/2008;73;0720092;NOVISIA LOGISTIC              ;FR;7696841;AD1;112013                        ;9854700                       ;001;247;100;75;1;198;530;   ;   ;   ;-0900;+075;155;175;          ;26/03/2008;28/03/2008;00002;S.SF  ;20/03/2008;08:53:00;                              ;1D
20/03/2008;73;0720092;NOVISIA LOGISTIC              ;FR;7696841;AD1;                              ;                              ;002;247;100;75;2;198;530;   ;   ;   ;-0850;+200;165;175;          ;26/03/2008;28/03/2008;00002;S.SF  ;20/03/2008;08:53:00;                              ;1D

 

As mentioned earlier above is the last character in the file. And the C# code is below:

 

FileUpload1.SaveAs(strFilePath + strFileName);

StreamReader tr = File.OpenText(strFilePath + strFileName);

String filedata = tr.ReadToEnd();

int intLength = filedata.Length;

filedata.Remove(intLength - 4, 4);

tr.Close();

FileInfo f = new FileInfo(strFilePath + strFileName);

FileStream fs = f.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);

StreamWriter Tex = f.CreateText();

Tex.Write(filedata);

Tex.Close();

fs.Close();

File.Copy(filedata, strFilePath + strFileName, true);

Delimited(strFilePath + strFileName);

Label2.Text = "Data import has been successfully completed!";

Please if you can help me, that would be great.
Thanks for looking.

Regards,
Shriroop.

All replies (8)

Thursday, March 20, 2008 1:02 PM ✅Answered

The character is actually an old ASCII control character (26) which is SUB (substitute) designed to used when character had an error (you know when bytes would arrive through a serial port with parity (now thats really showing my age)).

 

This is likely to work:-

 

Code Snippet

byte[] contents = File.ReadAllBytes(strPath);

FileStream fsOut = File.OpenWrite(strPath);

fsOut.Write(contents, 0, contents.length -1);

fsOut.Close();

 

 

 

 

 

 

 

 

 


Thursday, March 20, 2008 1:20 PM ✅Answered

if you wanted to remove the last char of the file you can set like this in your code.

 

FileInfo table = new FileInfo("C:\Filename.something");

string strTable = table.OpenText().ReadToEnd();

string erase = strTable.Substring(strTable.Length - 1);

 

if you want to replace chars do the following

 

FileInfo table = new FileInfo("C:\Filename.something");

string strTable = table.OpenText().ReadToEnd();

string rep = strTable.Replace("old char", "new char");

 

 


Thursday, March 20, 2008 12:37 PM

Hi!

 

If you look at the file in a Hex editor then you can determmine the hex value, and then you are able to replace it just by using the hexcode in your replace statement.

 

/Lars.

 


Wednesday, March 26, 2008 2:48 PM

Hi Anthony,

 

Sorry for the delay in reply, was away on business.

 

Just tried what you suggested, but it does not update the text file.

It does not give me any errors.

 

Thanks for your help so far.

 

Regards,

 


Wednesday, March 26, 2008 4:01 PM

If you specify an output path thats different to the input path does it create the new file?

Its not 1 byte shorter than the original?

 

 


Wednesday, March 26, 2008 4:57 PM

Hi Anthony,

 

It does create a new file if I change the path. The file is created successfully, but the size is exactly the same.

Not a byte more, not a byte less.

 

Regards,


Monday, March 31, 2008 11:01 AM

Hi Bermil,

 

Tried what you have suggested. I can get hold of the last character, but I am struggling to write it back rest of the content to the file.

 

Please can you help, how do I write it back once I have removed the character.

 

Thanks in advance.


Monday, March 31, 2008 3:59 PM

Try this:-

 

Code Snippet

FileStream fsOut = File.OpenWrite(strPath);

fsOut.SetLength(fsOut.Length - 1);

fsOut.Close();

 

 

tested it this time