-
December 8th, 2011, 11:29 AM
#1
[RESOLVED] Reading text file by character
Hi guys,
I am trying to read a text file, filter out the tabs and replace them with commas. For some reason my code will just not do this!! I am new to this site and still doing my degree so still novice programmer. Many thanks
-
December 8th, 2011, 11:30 AM
#2
Re: Reading text file by character
Below is the function in which the text file is being read -
private void Convert()
{
StreamReader sr = new StreamReader(ofd1.FileName);
StreamWriter sw = new StreamWriter(sfd1.FileName);
string line = "";
char c;
while((line = sr.ReadLine()) != null)
{
c = (char)sr.Read();
if (c == '\t')
{
//MessageBox.Show("char found");
c = ',';
sw.Write(c);
sw.Flush();
}
sw.WriteLine(line);
}
sr.Close();
sw.Close();
MessageBox.Show("Convertion complete");
}
-
December 8th, 2011, 12:00 PM
#3
Re: Reading text file by character
You might want to try reading in using a BinaryReader and writing out using BinaryWriter.
BinaryReader r = new BinaryReader(fs);
binWriter = new BinaryWriter(File.Open(outfilename, FileMode.Create));
binWriter.Write(input_char);
Developing using:
.NET3.5 / VS 2010
-
December 8th, 2011, 12:07 PM
#4
Re: Reading text file by character
Code:
int t
while((t = sr.Read()) > -1)
{
c = (char)t;
if (c == '\t')
{
-
December 8th, 2011, 12:47 PM
#5
Re: Reading text file by character
Originally Posted by Jim_Auricman
You might want to try reading in using a BinaryReader and writing out using BinaryWriter.
BinaryReader r = new BinaryReader(fs);
binWriter = new BinaryWriter(File.Open(outfilename, FileMode.Create));
binWriter.Write(input_char);
Why would you read a text file using a BinaryReader? If you're comparing characters you need to be encoding aware which means you need to convert the raw bytes into a character using either a StreamReader with the correct encoding set or by reading into a byte[] and using the System.Text.Encoding classes to do it. The former is by far the easiest.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
December 9th, 2011, 01:14 PM
#6
Re: Reading text file by character
Cheers for you help there was no need for a binary reader, i recieved no emals saying my forum had been responded to so I ended up sorting it myself cheers for your help Silent Sojourner your answer was similar to mine except i used peek i ended up with -
while((sr.Peek() >= 0))
{
char c = (char)sr.Read();
if (c == '\t')
{
c = ',';
}
sw.Write((char)c); //rewrite all characters
}
-
December 9th, 2011, 03:46 PM
#7
Re: Reading text file by character
Reading and writing one character at a time would be the slowest possible way to do this. I would read the entire file at once into a string then use replace to replace all the tabs with commas then write the resulting string to disk all at once. Should be at least 100 times faster.
Always use [code][/code] tags when posting code.
-
December 9th, 2011, 04:24 PM
#8
Re: Reading text file by character
well it depends. what if the file size is huge, say, 2gb?
-
December 9th, 2011, 05:35 PM
#9
Re: Reading text file by character
If it were that big then reading all at once would be a lot faster still. Most systems today have at least 4 gigs of ram and ram is much faster than disk and a single read is much faster than 1000s of reads.
Always use [code][/code] tags when posting code.
-
December 15th, 2011, 03:41 PM
#10
Re: Reading text file by character
I understand how reading and writing it character by character could be a bit naff in some circimstances. But the aim of this program was to demonstrate the use of threads and by reading a large text file by character demonstrated that we needed another thread. cheers
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
|