ReadLine, Perform Action, DeleteLine, ReadNextLine...
-
Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.
j.t.
-
Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.
j.t.
I'm afraid there is no direct way to remove a line from the beginning of a file. The only way you can do this is to overwrite the entire file with all the lines in the original file but the first one. In case you don't know, you can use
File.ReadAllLines
andFile.WriteAllLines
to read and write lines on a file. -
Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.
j.t.
To do this, you have to do something like this:
while(true)
{
StringBuilder wholeText = "";
String fileName = "file.txt";
// create reader & open file
Textreader tr = new StreamReader(fileName);// read a line of text
String oneLine = tr.ReadLine();
if(oneLine == null)
{
break;
}
else
{
doSomethingWith(oneLine);
}while(String s = tr.ReadLine != null)
{
wholeText.add(String.Format("{0}\n",s);
}
// close the stream
tr.Close();//open the same file and write the text in there
Textwriter tw = new StreamWriter(fileName);
tw.write(wholeText.ToString());
tw.Close();
}This isn't tested, but the methodoligy should work.
-
Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.
j.t.
Why would you delete the line? in your scenario, when you are done, the file is empty. You could as well use File.ReadAllLines as has been said before, but leave the lines where they are and delete the entire file (or just all its content with WriteAllText("") when done. Much easier. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.
j.t.
Thank ya'll for your great advice. :-D It's much appreciated. I figured out how to do it Yay!!!!! Here's the code incase anybody's interested.
private void TookMeLongEnough_Click(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(openFileDialog1.Tag.ToString());
foreach (string line in lines)
{
SaveSessionTextBox.Text = fbd.SelectedPath +
"\\" +
i++ +
"A" +
i++ +
DateTime.Now.Hour +
DateTime.Now.Minute +
DateTime.Now.Second +
DateTime.Now.Millisecond +
".spr";
}
}foreach( inch on Jason ) { Girlfriend.IsHappier(); }
-
Thank ya'll for your great advice. :-D It's much appreciated. I figured out how to do it Yay!!!!! Here's the code incase anybody's interested.
private void TookMeLongEnough_Click(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(openFileDialog1.Tag.ToString());
foreach (string line in lines)
{
SaveSessionTextBox.Text = fbd.SelectedPath +
"\\" +
i++ +
"A" +
i++ +
DateTime.Now.Hour +
DateTime.Now.Minute +
DateTime.Now.Second +
DateTime.Now.Millisecond +
".spr";
}
}foreach( inch on Jason ) { Girlfriend.IsHappier(); }
some remarks anyway: 1.
jas0n23 wrote:
openFileDialog1.Tag.ToString()
very strange, you use the Tag to hold a filespec? 2. the hour/minute/sec/msec parts would be nicer with a fixed width, i.e. leading zeroes. Try DateTime.Now.ToString("HHmmssfff") 3. not sure why you would want two autoincrements in the filename; I would suggest
s=fbd.SelectedPath + "\\"+ i.ToString("N5")+ "_" + DateTime.Now.ToString("HHmmssfff")+".spr";
i++;:)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
some remarks anyway: 1.
jas0n23 wrote:
openFileDialog1.Tag.ToString()
very strange, you use the Tag to hold a filespec? 2. the hour/minute/sec/msec parts would be nicer with a fixed width, i.e. leading zeroes. Try DateTime.Now.ToString("HHmmssfff") 3. not sure why you would want two autoincrements in the filename; I would suggest
s=fbd.SelectedPath + "\\"+ i.ToString("N5")+ "_" + DateTime.Now.ToString("HHmmssfff")+".spr";
i++;:)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Haha, yeah now that you mention it Luc Pattyn, it is very strange that I use .Tag to hold File Specs. But, it's just a weird preference is all. Is there any reason as to why this may be a bad thing to do? Regards, Jay.
foreach( inch on Jason ) { Girlfriend.IsHappier(); }