c# edit txt file without creating another file (without StreamWriter) - c#

Because I'm using non-latin alphabet, if I use StreamWriter, the characters aren't correct.
String line;
StreamReader sr = new StreamReader(#"C:\Users\John\Desktop\result.html");
line = sr.ReadLine();
while (line != null)
{
line = sr.ReadLine();
if (line.Contains("</head>"))
{
line = "<img src=\"result_files\\image003.png\"/>" + line;
}
}
sr.Close();
Here I'm editing the string I want to edit in the file, but I'm not saving it in the same file. How to do that?

If you use one of the StreamWriter constructors that accepts an encoding you shouldn't have any problems with incorrect characters. You are also skipping the first line in your reading method.
Encoding encoding;
StringBuilder output = new StringBuilder();
using (StreamReader sr = new StreamReader(filename))
{
string line;
encoding = sr.CurrentEncoding;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("</head>"))
{
line = "<img src=\"result_files\\image003.png\"/>" + line;
}
output.AppendLine(line);
}
}
using (StreamWriter writer = new StreamWriter(filename, false, encoding))
{
writer.Write(output.ToString());
}

I think the easiest approach would be
open the file in read/write mode
read everything from the file
make the modifications inmemory
rewrite it back to the file rather than appending..

you use a StreamReader. And the name say what it's function. To read!
Dirty-Code
if (File.Exists(fileName))
{
int counter = 1;
StringBuilder sb = new StringBuilder();
foreach (string s in File.ReadAllLines(fileName, Encoding.Default))
{
if (s.Contains("</head>"))
{
s= "<img src=\"result_files\\image003.png\"/>" + line;
}
sb.AppendLine(s);
counter++;
}
File.WriteAllText(fileName, sb.ToString(), Encoding.Default);
}

Related

Stream reader to analysis computation times

I'm very new to C#, I'm trying to develop a program to analyze computational time. The simulations program's output gives me 4 files to 4 parts of the process.
The form should present only the time in seconds (that's why i got regex.replace)
Here is some of my code , so far.
I got a error " Not possible to read form closed textreader"
private void btn_read_Click(object sender, EventArgs e)
{
string line;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Log operation A files (*.lga)|*.lgf|Log operation B files (*.lgb)|*.lgb|Log operation C files (*.lgc)|*.lgc|Log operation D files (*.lgd)|*.lgd|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path;
path = openFileDialog1.FileName;
StringBuilder str = new StringBuilder();
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(path);
using (StreamReader sr = new StreamReader(path))
{
// Read the stream to a string, and write the string to the console.
line = sr.ReadToEnd();
while ((line = file.ReadLine()) != null)
{
if (line.Contains("Computation Time for part A Analysis ="))
{
txt_t_a.Text = Regex.Replace(line, #"[^0-9.]+", "");
}
file.Close();
}
}
}
}
}
}
my code
an example of the log file folders
You're closing your reader while you're trying to read it, resulting in an error. The relevant code is below. Move the close and it should stop giving you that error.
using (StreamReader sr = new StreamReader(path))
{
// Read the stream to a string, and write the string to the console.
line = sr.ReadToEnd();
while ((line = file.ReadLine()) != null)
{
if (line.Contains("Computation Time for part A Analysis ="))
{
txt_t_a.Text = Regex.Replace(line, #"[^0-9.]+", "");
}
// Remove this line.
// file.Close();
}
// Put it here
file.Close()
}
// Or here
Delete this line:
System.IO.StreamReader file = new System.IO.StreamReader(path);
And Update:
using (StreamReader sr = new StreamReader(path))
{
// Read the stream to a string, and write the string to the console.
line = sr.ReadToEnd();
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("Computation Time for part A Analysis ="))
{
txt_t_a.Text = Regex.Replace(line, #"[^0-9.]+", "");
}
sr.Close();
}
}

StreamWriter only writes one line

I am trying to write from a .csv file to a new file.
Every time StreamWriter writes, it writes to the first line of the new file. It then overwrites that line with the next string, and continues to do so until StreamReader reaches EndOfStream.
Has anybody ever experienced this? How did you overcome it?
This is my first solution outside of those required in by my school work. There is an unknown number of rows in the original file. Each row of the .csv file has only 17 columns. I need to write only three of them and in the order found in the code snippet below.
Before coding the StreamWriter I used Console.WriteLine() to make sure that each line was in the correct order.
Here is the code snippet:
{
string path = # "c:\directory\file.csv";
string newPath = # "c:\directory\newFile.csv"
using(FileStream fs = new FileStream(path, FileMode.Open))
{
using(StreamReader sr = new StreamReader(fs))
{
string line;
string[] columns;
while ((line = sr.ReadLine()) != null)
{
columns = line.Split(',');
using(FileStream aFStream = new FileStream(
newPath,
FileMode.OpenOrCreate,
FileAccess.ReadWrite))
using(StreamWriter sw = new StreamWriter(aFStream))
{
sw.WriteLine(columns[13] + ',' + columns[10] + ',' + columns[16]);
sw.Flush();
sw.WriteLine(sw.NewLine);
}
}
}
}
}
You should open the target in the same scope as you are opening the source instead of doing so in the loop which will cause you to overwrite the file every time with the FileMode option OpenOrCreate.
var path = #"c:\directory\file.csv";
var newPath = #"c:\directory\newFile.csv"
using(var sr = new StreamReader(new FileStream(path, FileMode.Open)))
using(var sw = new StreamWriter(new FileStream(newPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
while(!sr.EndOfStream)
{
string line = sr.ReadLine();
var columns = line.Split(',');
sw.WriteLine(columns[13] + ',' + columns[10] + ',' + columns[16]);
sw.WriteLine(sw.NewLine);
}
sw.Flush();
}
I also hope you are sure about your CSV spacing as you are hard coding the positions in your code.
To correctly fix your code, you'll want to structure more:
public void CopyFileContentToLog()
{
var document = ReadByLine();
WriteToFile(document);
}
public IEnumerable<string> ReadByLine()
{
string line;
using(StreamReader reader = File.OpenText(...))
while ((line = reader.ReadLine()) != null)
yield return line;
}
public void WriteToFile(IEnumerable<string> contents)
{
using(StreamWriter writer = new StreamWriter(...))
{
foreach(var line in contents)
writer.WriteLine(line);
writer.Flush();
}
}
You could obviously tailor and make it a bit more flexible. But this should demonstrate and resolve some of the issues you have with your loop and streams.
First off, you are creating and closing a write stream to the same file for every single line. This means the file gets overwritten every line. You want to take your using block outside of the while loop; however, if you insist on opening and closing the write stream for every single line, then you need to use FileMode.Append
{
string path=#"c:\directory\file.csv";
string newPath=#"c:\directory\newFile.csv"
using(StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open))) // no need for 2 usings
using (FileStream aFStream = new FileStream (newPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
string line;
string[] columns;
{
while((line = sr.ReadLine()) != null)
{
columns = line.Split(',');
using (StreamWriter sw = new StreamWriter(aFStream))
{
sw.WriteLine(columns[13] + ',' + columns[10] + ',' + columns[16]);
sw.Flush();
sw.WriteLine(sw.NewLine);
}
}
}
}
}

Why no line is writen in output using StreamWriter

I'm having an issue with StreamWriter and I just can't find what I'm doing wrong.
String line, new_line;
using (StreamReader sr = new StreamReader(txtFilePath.Text))
{
using (StreamWriter sw = new StreamWriter(txtResultFolder.Text.ToString() + "\\" + "NEW_trimmed_file" + ".csv", true))
{
while ((line = sr.ReadLine()) != null)
{
new_line = line.TrimEnd();
MessageBox.Show(new_line);
sw.WriteLine(new_line);
}
}
}
I used the MessageBox.Show(new_line), just to be sure I have a value for the StreamWriter to write, but in the resulted file I cannot find anything.
As an additional information I have a text which has empty spaces for each line (a lot of spaces) and I'm making another file with same lines as the first, but with no spaces.
Any hints why the StreamWriter does not actually writes in the destination file?
Many thanks,
but in the resulted file I cannot find anything
If you want your StreamWriter to immediately write it's buffer before disposal, you need to call Flush():
string line, new_line;
using (StreamReader sr = new StreamReader(txtFilePath.Text))
using (StreamWriter sw = new StreamWriter(txtResultFolder.Text.ToString() +
"\\" + "NEW_trimmed_file" + ".csv", true))
{
while ((line = sr.ReadLine()) != null)
{
new_line = line.TrimEnd();
MessageBox.Show(new_line);
sw.WriteLine(new_line);
}
sw.Flush();
}
There is a much shorter way to read and write from and to files. Just use File.ReadAllLines() and File.WriteAllLines()
var content = File.ReadAllLines(txtFilePath.Text)
File.WriteAllLines(txtResultFolder.Text.ToString(), content);
I am a little confused about of the naming of txtResultFolder. Is this path a folder? What is the exact content of this TextBox?

Reading resource txt line by line

Had a txt file on my desktop with code:
string source = #"C:\Users\Myname\Desktop\file.txt"
string searchfor = *criteria person enters*
foreach (string content in File.ReadLines(source))
{
if (content.StartsWith(searchfor)
{
*do stuff*
}
}
I recently just learned I can add the txt as a resource file (as it will never be changed). However, I cannot get the program to read that file.txt as a resource line by line like above. I have tried
Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApplication.file.txt")
with a stream reader but it says invalid types.
Basic concept: user enters data, turned into a string, compared to the starting line of the file.txt as it reads down the list.
Any help?
edit
Jon, I tried as a test to see if it is even reading the file:
var assm = Assembly.GetExecutingAssembly();
using (var stream = assm.GetManifestResourceStream("WindowsFormsApplication.file.txt")) ;
{
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
label1.Text = line;
}
}
}
It says "The name stream does not exist in the current context" and "Possible Mistaken Empty Statement" for the stream = assm.Get line
You can use a TextReader to read a line at a time - and StreamReader is a TextReader which reads from a stream. So:
var assm = Assembly.GetExecutingAssembly();
using (var stream = assm.GetManifestResourceStream("WindowsFormsApplication.file.txt"))
{
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
...
}
}
}
You could write an extension method on TextReader to read all the lines, but the above is simpler if you only need this once.
Found the issue:
The file, while loaded as a resource, despite all the tutorials saying it is NameSpace.File, the truth is the system puts the location as NameSpace.Resources.File, so I had to update that as well.
Then I used the following code:
string searchfor = textBox1.Text
Assembly assm = Assembly.GetExecutingAssembly();
using (Stream datastream = assm.GetManifestResourceStream("WindowsFormsApplication2.Resources.file1.txt"))
using (StreamReader reader = new StreamReader(datastream))
{
string lines;
while ((lines = reader.ReadLine()) != null)
{
if (lines.StartsWith(searchfor))
{
label1.Text = "Found";
break;
}
else
{
label1.Text = "Not found";
}
}
}

Override line in file while writing

I am reading a file using streamreader opened in ReadWrite mode. The requirement I have is to check for a file for specific text, and if it is found, replace that line with a new line.
Currently I have initialized a StreamWriter for writing.
It is writing text to a file but it's appending that to a new line.
So what should I do to replace the particular line text?
System.IO.FileStream oStream = new System.IO.FileStream(sFilePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read);
System.IO.FileStream iStream = new System.IO.FileStream(sFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
System.IO.StreamWriter sw = new System.IO.StreamWriter(oStream);
System.IO.StreamReader sr = new System.IO.StreamReader(iStream);
string line;
int counter = 0;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("line_found"))
{
sw.WriteLine("line_found false");
break;
}
counter++;
}
sw.Close();
sr.Close();
Hi Try the below Code.... it will help you....
//Replace all the HI in the Text file...
var fileContents = System.IO.File.ReadAllText(#"C:\Sample.txt");
fileContents = fileContents.Replace("Hi","BYE");
System.IO.File.WriteAllText(#"C:\Sample.txt", fileContents);
//Replace the HI in a particular line....
string[] lines = System.IO.File.ReadAllLines("Sample.txt");
for (int i = 0; i < lines.Length; i++)
{
if(lines[i].Contains("hi"))
{
MessageBox.Show("Found");
lines[i] = lines[i].Replace("hi", "BYE");
break;
}
}
System.IO.File.WriteAllLines("Sample.txt", lines);

Categories

Resources