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);
Related
I'm reading a binary file and writing to another file in CP 437 format by skipping few lines. But the output file size is increased than the original file and also data is corrupted. Any help to identify the issue.
StreamReader sStreamReader = new StreamReader(#"D:\Denesh\Input.txt");
string AllData = sStreamReader.ReadToEnd();
string[] rows = AllData.Split(",".ToCharArray());
FileStream fileStream = new FileStream(TransLog, FileMode.Open);
StreamReader streamReader = new StreamReader((Stream)fileStream, Encoding.GetEncoding(437));
StreamWriter streamWriter = new StreamWriter(outFile, false);
int num = 0;
int count = 0;
while (!streamReader.EndOfStream)
{
string tlogline = streamReader.ReadLine();
if (rows[count] == Convert.ToString(num))
{
++count;
}
else
{
++num;
streamWriter.WriteLine(tlogline, streamReader.CurrentEncoding);
}
}
fileStream.Close();
streamWriter.Close();
Adding filestream for streamwriter solves the issue. Thanks.
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();
}
}
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);
}
}
}
}
}
When I add a txt file as a resource to a project, how can I then consume the contents of that resource as a string?
The closest I've been able to get is by using the Resource Manager, to pull an unmanaged stream. However, this throws a null error:
using (StreamReader sr = new StreamReader(
Properties.Resources.ResourceManager.GetStream(
"TestFile.txt", CultureInfo.CurrentCulture)))
{
Console.WriteLine(sr.ReadToEnd());
}
You could do this too:
var myAss = Assembly.GetExecutingAssembly();
var mytxtFileResource = "Namespace.Project.MyTxtFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(mytxtFileResource))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}
You dont need to do it like that for text files
Just write it like this
https://msdn.microsoft.com/en-us/library/aa287548(v=vs.71).aspx
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(lines);
file.Close();
and read it like this:
https://msdn.microsoft.com/en-us/library/aa287535(v=vs.71).aspx
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}
file.Close();
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);
}