File Manipulation
-
Not sure why i don't seem to get this but i would like to write some records to a .txt file. At the end of all records, would like to keep a number which signifies the count. So if there are 5 records, the last line in the file will have "5". If i have to add 10 more. I read the last line, add 10 to the number to total 15. etc. I have been using StreamReader and Writer to do this, but i find that there is a limitation I really want to be able to: Open the file Check the last line to see a number count Append more data to file, update the number count. Close the file. if (!FileExists(ReportFile)) { File.Create(ReportFile); } StreamReader stream = new StreamReader(ReportFile); //How would i get to the end of the file and only read a line that says: Count:100 where the statement "Count:" is constant, but the number will vary as we add records? Then i write the records: StreamWriter filewrite = new StreamWriter(ReportFile, true); filewrite.WriteLine(Rec); filewrite.Close; File
-
Not sure why i don't seem to get this but i would like to write some records to a .txt file. At the end of all records, would like to keep a number which signifies the count. So if there are 5 records, the last line in the file will have "5". If i have to add 10 more. I read the last line, add 10 to the number to total 15. etc. I have been using StreamReader and Writer to do this, but i find that there is a limitation I really want to be able to: Open the file Check the last line to see a number count Append more data to file, update the number count. Close the file. if (!FileExists(ReportFile)) { File.Create(ReportFile); } StreamReader stream = new StreamReader(ReportFile); //How would i get to the end of the file and only read a line that says: Count:100 where the statement "Count:" is constant, but the number will vary as we add records? Then i write the records: StreamWriter filewrite = new StreamWriter(ReportFile, true); filewrite.WriteLine(Rec); filewrite.Close; File
Wouldn't it be easier to write the record count in a header row at the start of the file? Then you wouldn't have to read to the end of the file to retrieve this information.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
Wouldn't it be easier to write the record count in a header row at the start of the file? Then you wouldn't have to read to the end of the file to retrieve this information.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
I actually do that: StreamReader stream = new StreamReader(QueryReportFile); buf = stream.ReadLine();//Read in th efirst line which is the count int count = (int)buf; count++; stream.Close My question here is that after i read the count and add to it, i have to update the first line with the new count. Do i have to create another StreamWriter just for that line, then execute below. How can i do this with one StreamWriter?? StreamWriter filewrite = new StreamWriter(ReportFile,true); filewrite.WriteLine(RQ); filewrite.Close();