I'm using the code below to break apart a large text file into smaller files based on the logic you can see here. I'm getting an error on the File.WriteAllText line saying that tempfile doesn't exist. The flow is one header record, followed by multiple report data rows, followed by one end of report line, then it starts over again. Does anyone know why my temp file wouldn't be created here, what am I missing? Thanks.
private static void SplitFile()
{
StreamReader sr = new StreamReader($"{_processDir}{_processFile}");
StreamWriter sw = null;
string fileName = string.Empty;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (line.Split('\t')[0] == "FILEIDENTIFIER")
{
//line is a header record
sw = new StreamWriter("{_processDir}tempfile.txt", false);
sw.WriteLine(line);
}
else if (line.Contains("END OF\tREPORT"))
{
//line is end of report
sw.Close();
File.WriteAllText($"{_processDir}{fileName}.txt", File.ReadAllText($"{_processDir}tempfile.txt"));
}
else
{
//line is a report datarow
fileName = line.Split('\t')[0];
sw.WriteLine(line);
}
}
}
This code is getting you problem :
sw = new StreamWriter("{_processDir}tempfile.txt", false);
Use string interpolation with above code :
sw = new StreamWriter($"{_processDir}tempfile.txt", false);
You can check that where the streamwriter has written the data.
Related
I've been given a few different sets of procedures for various different things in C# under file manipulation.
I have forgotten basically how to call or use the procedures and so they are pretty much useless to me until I figure out how. Apologies for sounding stupid but I've done as much searching as I can and I can't relate other sources to my problem.
Here's a procedure I've been given:
void readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
}
Now I understand completely what this and all the other procedures do, yet I forget how to use them in main.
Here's what I currently have in main:
string path = "C:\\Users\\Joe\\Documents\\General\\College\\Computer Science\\Coding\\TextFileWork\\textFile.txt";
string readFile;
readFile = readFromTextFile(path);
Now the problem I'm having is understanding how to use the procedure back into main to return the read file. The string readFile is what I am trying to append the read text into, however I don't know how I should call the function in order to append it. Some basic help should suffice, thanks!
EDIT:
Here's the entire code I currently have (C# Console Application)
namespace TextFileWork_03._03._18
{
class Program
{
static void Main(string[] args)
{
string path = "C:\\Users\\Joe\\Documents\\General\\College\\Computer
Science\\Coding\\TextFileWork\\textFile.txt";
string readFile;
readFromTextFile(readFile);
if (File.Exists(path) == true)
{
//Create a file to write to.
Console.WriteLine(path + " Exists");
}
else
{
Console.WriteLine(path + " File not found");
}
FileInfo fi = new FileInfo(path);
FileStream fs = fi.Create();
fs.Close();
if (File.Exists(path) == true)
{
//Create a file to write to.
Console.WriteLine(path + " Now exists");
}
else
{
Console.WriteLine(path + " File still not found");
}
}
static void readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
}
}
}
To make your code work you'll need a return value from your method, otherwise you cannot say:
mysomthing = readFromTextFile
So, lets return a string value:
There is one problem: do you want to return a single line or just the whole file?
Here's the whole file version:
static string readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
StringBuilder sb = new StringBuilder();
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
sb.AppendLine(line);
}
//close the file
sr.Close();
return sb.ToString();
}
update this answer is not valid, I'll just keep it for a few minutes for reference.
You should have given more of your code to actually make this a valid question. Nevertheless, I'll try to help you out.
Your procedure (we call it methods in C#, (strongly related to functions)), lives in a class, lets call it Foo, but you can look it up in your code. Just scroll up: its the first blue class you'll see.
public class Foo //this is your class
{
void readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
}
}
To call it, from your Main method, you'll need an object
void Main()
{
string path = "C:\\Users\\Joe\\Documents\\General\\College\\ComputerScience\\Coding\\TextFileWork\\textFile.txt";
string readFile;
Foo fooObject = new Foo(); //create a new Foo
readFile = fooObject.readFromTextFile(path);
}
I have two methods: saveFile and saveAsToFile. The first one is supposed to overwrite the contents of the current, existing file. While the second is supposed to save as a new file (if the current is non-existing or the user just wants to make a copy.)
When I use the saveAsToFile method it works every time. When I use the saveFile method it doesn't write anything. (I DO see the "Saved!" MessageBox, though.)
Here are my methods:
public void saveFile(string[] inData, string inDataTitle)
{
//This method saves the file
SaveFileDialog savefile;
string trueFileName;
if (isStrArrayNotEmpty(inData)) {
//Only attempt to save the file if there is anything written in the textArea
if (f.getDocumentSavedStatus()) {
if (f.getDocumentChangedStatus()) {
savefile = new SaveFileDialog();
if (inDataTitle.EndsWith("*")) {
//Remove the asterisk from the document name
savefile.FileName = inDataTitle.Substring(0, inDataTitle.Length - 1);
}
else {
savefile.FileName = inDataTitle;
}
StreamWriter sw = new StreamWriter(savefile.FileName, false);
foreach (string line in inData) {
sw.WriteLine(line);
}
sw.Flush();
if (sw.BaseStream != null)
sw.BaseStream.Flush();
sw.Close();
/*
using (StreamWriter sw = new StreamWriter(savefile.FileName, false))
foreach (string line in inData) {
sw.WriteLine(line);
}
*/
f.setDocumentName(string.Empty);
trueFileName = Path.GetFileName(savefile.FileName);
f.setDocumentName(trueFileName);
f.setDocumentSavedStatus(true);
f.setDocumentChangedStatus(false); //Changes saved, status updated
MessageBox.Show("Saved!");
}
}
else {
//If the document hasn't been saved before, send the values to the 'Save As' method
saveAsToFile(inData, inDataTitle);
}
}
}
public void saveAsToFile(string[] inData, string inDataTitle)
{
//This method checks if there is anything written in the texArea,
//if so it prompts the user to save the file to disk (Save As)
SaveFileDialog savefile;
string trueFileName;
if (isStrArrayNotEmpty(inData)) {
//Only attempt to save the file if there is anything written in the textArea
savefile = new SaveFileDialog();
if (inDataTitle.EndsWith("*")) {
//Remove the asterisk from the document name
savefile.FileName = inDataTitle.Substring(0, inDataTitle.Length - 1);
}
else {
savefile.FileName = inDataTitle;
}
savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (savefile.ShowDialog() == DialogResult.OK) {
StreamWriter sw = new StreamWriter(savefile.FileName, false);
foreach (string line in inData) {
sw.WriteLine(line);
}
sw.Flush();
if (sw.BaseStream != null)
sw.BaseStream.Flush();
sw.Close();
/*
using (StreamWriter sw = new StreamWriter(savefile.FileName))
foreach (string line in inData) {
sw.WriteLine(line);
}
*/
f.setDocumentName(string.Empty);
trueFileName = Path.GetFileName(savefile.FileName);
f.setDocumentName(trueFileName);
f.setDocumentSavedStatus(true);
f.setDocumentChangedStatus(false); //Changes saved, status updated
}
}
}
As you can see by the comments in the code; I tried using using, and then I tried to "manually flush" the streamwriter, but nothing works.
Using saveAsToFile works every time. It overwrites the text file as expected, no problems. While saveFile doesn't write anything to the file. (Leaving it unchanged.)
I tried looking for errors in saveFile by using MessageBox.Show to print the values of savefile.Filename and line in the appropriate places - they all worked as expected, yet nothing is written to the actual text file.
isStrArrayNotEmpty returns true if the string array does not contain any white spaces.
getDocumentSavedStatus returns the value of a boolean which tells if the file has been saved before or not (existent / non-existent)
getDocumentChangedStatus returns the value of a boolean which tells if the file has been modified or not (asterisk by the end of the file name, indicating that work will be lost if the user shuts down the application.)
Does the inDataTitle parameter include the path of the filename you're trying to save? If not, it's likely saving to a file of the same name but in a different folder.
After your line:-
StreamWriter sw = new StreamWriter(savefile.FileName, false);
add the line:-
MessageBox.Show(((FileStream)(sw.BaseStream)).Name);
and it'll tell you where the file is being saved.
Try replacing
StreamWriter sw = new StreamWriter(savefile.FileName, false);
foreach (string line in inData) {
sw.WriteLine(line);
}
sw.Flush();
if (sw.BaseStream != null)
sw.BaseStream.Flush();
sw.Close();
with
File.WriteAllLines(saveFile.Filename, inData);
I am trying to Write to a text file after this code block checks for the last time the PC was restarted. The code below reads from a text file, the last time the PC was resarted, and from there it determines whether to show a splash-screen. However, After this method runs, i need to write to the text file what the current "System Up-Time" is. But i keep getting an error that says the text file is in use. This has driven me insane. I have made sure all StreamWriters and StreamReaders are closed. I have tried Using Statements. I have tried GC.Collect. I feel like i have tried everything.
Any help would be appreciated.
private void checkLastResart()
{
StreamReader sr = new StreamReader(Path.GetDirectoryName(Application.ExecutablePath) + #"\Settings.txt");
if (sr.ReadLine() == null)
{
sr.Close();
MessageBox.Show("There was an error loading 'System UpTime'. All settings have been restored to default.");
StreamWriter sw = new StreamWriter(Path.GetDirectoryName(Application.ExecutablePath) + #"\Settings.txt", false);
sw.WriteLine("Conversion Complete Checkbox: 0");
sw.WriteLine("Default Tool: 0");
sw.WriteLine("TimeSinceResart: 0");
sw.Flush();
sw.Close();
}
else
{
try
{
StreamReader sr2 = new StreamReader(Path.GetDirectoryName(Application.ExecutablePath) + #"\Settings.txt");
while (!sr2.EndOfStream)
{
string strSetting = sr2.ReadLine();
if (strSetting.Contains("TimeSinceResart:"))
{
double lastTimeRecorded = double.Parse(strSetting.Substring(17));
//If the lastTimeRecorded is greater than timeSinceResart (computer has been resarted) OR 2 hours have passed since LVT was last run
if (lastTimeRecorded > timeSinceRestart || lastTimeRecorded + 7200 < timeSinceRestart)
{
runSplashScreen = true;
}
else
{
runSplashScreen = false;
}
}
}
sr2.Close();
sr2.Dispose();
}
catch (Exception e) { MessageBox.Show("An error has occured loading 'System UpTime'.\r\n\r\n" + e); }
}
}
Below is a sample of writing to the Text file, after the above code has been run. It doesnt matter if i open a StreamWriter, or use File.WriteAllLines, an error is thrown immediately.
StreamWriter sw = new StreamWriter(Path.GetDirectoryName(Application.ExecutablePath) + #"\Settings.txt");
string[] lines = File.ReadAllLines(Path.GetDirectoryName(Application.ExecutablePath) + #"\Settings.txt");
lines[2] = "TimeSinceResart: " + timeSinceRestart;
foreach (string s in lines)
sw.WriteLine(s);
Your writing code should be changed in this way
string file = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"Settings.txt");
// First read the two lines in memory
string[] lines = File.ReadAllLines(file);
// then use the StreamWriter that locks the file
using(StreamWriter sw = new StreamWriter(file))
{
lines[2] = "TimeSinceResart: " + timeSinceRestart;
foreach (string s in lines)
sw.WriteLine(s);
}
In this way the lock on the StreamWriter doesn't block the reading with FileReadAllLines.
Said that, please note a couple of things. Do not create path strings with string concatenation, use the static methods of the Path class. But most important, when you create a disposable object like a stream be sure to use the using statement to close correctly the file
To complete the answer in response to your comment. Using statement also for the first part of your code
private void checkLastResart()
{
string file = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"Settings.txt");
using(StreamReader sr = new StreamReader(file))
{
if (sr.ReadLine() == null)
{
sr.Close();
MessageBox.Show(...)
using(StreamWriter sw = new StreamWriter(file, false))
{
sw.WriteLine("Conversion Complete Checkbox: 0");
sw.WriteLine("Default Tool: 0");
sw.WriteLine("TimeSinceResart: 0");
sw.Flush();
}
}
else
{
....
}
} // exit using block closes and disposes the stream
}
Where you create sr2, sr still has settings.txt open.
I'm trying to remove any "new line" characters from each line of text in my log file.
Below is an example entry that I am reading in with a Stream Reader :-
<![LOG[Raising event:
[SMS_CodePage(850), SMS_LocaleID(2057)]
instance of SoftDistProgramStartedEvent
{
AdvertisementId = "000216F6";
ClientID = "GUID:B55C2757-CBAE-468E-B54F-46CAF2ECF68F";
CommandLine = "\"C:\\WINNT\\system32\\cscript.exe\" /nologo Shutdown_Desktops_Overnight.vbs";
DateTime = "20130211080211.784000+000";
MachineName = "DWD*****";
PackageName = "0000073C";
ProcessID = 2516;
ProgramName = "Shutdown Desktops Overnight";
SiteCode = "S00";
ThreadID = 3640;
UserContext = "NT AUTHORITY\\SYSTEM";
WorkingDirectory = "C:\\WINNT\\system32\\CCM\\Cache\\0000073C.1.System\\";
};
]LOG]!><time="08:02:11.800+000" date="02-11-2013" component="execmgr" context="" type="1" thread="3640" file="event.cpp:522">
In the actual Logfile this is displayed as one line in the file, with the "New Line Characters" replaced with a square.
I'm using the following code to read in the log entries :-
using (StreamReader sr = new StreamReader(#"C:\Documents and Settings\riversd\Desktop\Logfile2.log"))
{
string Line;
while ((Line = sr.ReadLine()) != null)
{
}
}
The issue is that when the StreamReader reads this entry from the txt file it breaks at :-
"<![LOG[Raising event:"
I need to remove all new line characters in this entry, on the fly. I don't want to read the entire file into memory and then remove them, I'd rather deal with each log as I read it.
Is it possible?
The call to Replace isn't working because of this detail from the MSDN doc of StreamReader.ReadLine:
A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed.
So if you're going to use StreamReader.ReadLine to read in the input, you could build up a new string with StringBuilder.Append (not AppendLine) as StreamReader.ReadLine implicitly removes the new line characters.
var filePath = "text.txt";
StringBuilder sb = new StringBuilder();
using (StreamReader reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
sb.Append(reader.ReadLine());
}
}
File.WriteAllText(filePath, sb.ToString());
sr.ReadLine().Replace(Environment.NewLine, String.Empty);
EDIT:
In case the end of line is not \r\n but \n you can use regex:
Line = Regex.Replace(sr.ReadLine(), #"(\r\n|\n)", String.Empty);
i dont know if anyone else was having exactly this issue.
here is the code i used to fix this issue.
using (System.IO.FileStream File = new System.IO.FileStream(e.FullPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
{
using (System.IO.StreamReader Reader = new System.IO.StreamReader(File, Encoding.Default))
{
String CompleteData = Reader.ReadToEnd();
foreach (String Line in CompleteData.Split(new char[] { (char)13 }, StringSplitOptions.RemoveEmptyEntries))
{
if (Line.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)[0].Contains("Raising event"))
{
//Do Stuff
}
}
Reader.Close();
}
File.Close();
}
For some reason i had to do this because just using streamreader would throw an exception saying that the file is in use from another process.
It might help someone else at a later date..
I have two text files, Source.txt and Target.txt. The source will never be modified and contain N lines of text. So, I want to delete a specific line of text in Target.txt, and replace by an specific line of text from Source.txt, I know what number of line I need, actually is the line number 2, both files.
I haven something like this:
string line = string.Empty;
int line_number = 1;
int line_to_edit = 2;
using StreamReader reader = new StreamReader(#"C:\target.xml");
using StreamWriter writer = new StreamWriter(#"C:\target.xml");
while ((line = reader.ReadLine()) != null)
{
if (line_number == line_to_edit)
writer.WriteLine(line);
line_number++;
}
But when I open the Writer, the target file get erased, it writes the lines, but, when opened, the target file only contains the copied lines, the rest get lost.
What can I do?
the easiest way is :
static void lineChanger(string newText, string fileName, int line_to_edit)
{
string[] arrLine = File.ReadAllLines(fileName);
arrLine[line_to_edit - 1] = newText;
File.WriteAllLines(fileName, arrLine);
}
usage :
lineChanger("new content for this line" , "sample.text" , 34);
You can't rewrite a line without rewriting the entire file (unless the lines happen to be the same length). If your files are small then reading the entire target file into memory and then writing it out again might make sense. You can do that like this:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
int line_to_edit = 2; // Warning: 1-based indexing!
string sourceFile = "source.txt";
string destinationFile = "target.txt";
// Read the appropriate line from the file.
string lineToWrite = null;
using (StreamReader reader = new StreamReader(sourceFile))
{
for (int i = 1; i <= line_to_edit; ++i)
lineToWrite = reader.ReadLine();
}
if (lineToWrite == null)
throw new InvalidDataException("Line does not exist in " + sourceFile);
// Read the old file.
string[] lines = File.ReadAllLines(destinationFile);
// Write the new file over the old file.
using (StreamWriter writer = new StreamWriter(destinationFile))
{
for (int currentLine = 1; currentLine <= lines.Length; ++currentLine)
{
if (currentLine == line_to_edit)
{
writer.WriteLine(lineToWrite);
}
else
{
writer.WriteLine(lines[currentLine - 1]);
}
}
}
}
}
If your files are large it would be better to create a new file so that you can read streaming from one file while you write to the other. This means that you don't need to have the whole file in memory at once. You can do that like this:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
int line_to_edit = 2;
string sourceFile = "source.txt";
string destinationFile = "target.txt";
string tempFile = "target2.txt";
// Read the appropriate line from the file.
string lineToWrite = null;
using (StreamReader reader = new StreamReader(sourceFile))
{
for (int i = 1; i <= line_to_edit; ++i)
lineToWrite = reader.ReadLine();
}
if (lineToWrite == null)
throw new InvalidDataException("Line does not exist in " + sourceFile);
// Read from the target file and write to a new file.
int line_number = 1;
string line = null;
using (StreamReader reader = new StreamReader(destinationFile))
using (StreamWriter writer = new StreamWriter(tempFile))
{
while ((line = reader.ReadLine()) != null)
{
if (line_number == line_to_edit)
{
writer.WriteLine(lineToWrite);
}
else
{
writer.WriteLine(line);
}
line_number++;
}
}
// TODO: Delete the old file and replace it with the new file here.
}
}
You can afterwards move the file once you are sure that the write operation has succeeded (no excecption was thrown and the writer is closed).
Note that in both cases it is a bit confusing that you are using 1-based indexing for your line numbers. It might make more sense in your code to use 0-based indexing. You can have 1-based index in your user interface to your program if you wish, but convert it to a 0-indexed before sending it further.
Also, a disadvantage of directly overwriting the old file with the new file is that if it fails halfway through then you might permanently lose whatever data wasn't written. By writing to a third file first you only delete the original data after you are sure that you have another (corrected) copy of it, so you can recover the data if the computer crashes halfway through.
A final remark: I noticed that your files had an xml extension. You might want to consider if it makes more sense for you to use an XML parser to modify the contents of the files instead of replacing specific lines.
When you create a StreamWriter it always create a file from scratch, you will have to create a third file and copy from target and replace what you need, and then replace the old one.
But as I can see what you need is XML manipulation, you might want to use XmlDocument and modify your file using Xpath.
You need to Open the output file for write access rather than using a new StreamReader, which always overwrites the output file.
StreamWriter stm = null;
fi = new FileInfo(#"C:\target.xml");
if (fi.Exists)
stm = fi.OpenWrite();
Of course, you will still have to seek to the correct line in the output file, which will be hard since you can't read from it, so unless you already KNOW the byte offset to seek to, you probably really want read/write access.
FileStream stm = fi.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
with this stream, you can read until you get to the point where you want to make changes, then write. Keep in mind that you are writing bytes, not lines, so to overwrite a line you will need to write the same number of characters as the line you want to change.
I guess the below should work (instead of the writer part from your example). I'm unfortunately with no build environment so It's from memory but I hope it helps
using (var fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite)))
{
var destinationReader = StreamReader(fs);
var writer = StreamWriter(fs);
while ((line = reader.ReadLine()) != null)
{
if (line_number == line_to_edit)
{
writer.WriteLine(lineToWrite);
}
else
{
destinationReader .ReadLine();
}
line_number++;
}
}
The solution works fine. But I need to change single-line text when the same text is in multiple places. For this, need to define a trackText to start finding after that text and finally change oldText with newText.
private int FindLineNumber(string fileName, string trackText, string oldText, string newText)
{
int lineNumber = 0;
string[] textLine = System.IO.File.ReadAllLines(fileName);
for (int i = 0; i< textLine.Length;i++)
{
if (textLine[i].Contains(trackText)) //start finding matching text after.
traced = true;
if (traced)
if (textLine[i].Contains(oldText)) // Match text
{
textLine[i] = newText; // replace text with new one.
traced = false;
System.IO.File.WriteAllLines(fileName, textLine);
lineNumber = i;
break; //go out from loop
}
}
return lineNumber
}