473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Use both read and write operation to the same file

I have a file to which I'll need to at first read until it locates the
last line of string.

According to content of the last line of string, I'll need to write
something to this file right after the last line of string.

This requires that I'll need a StreamReader at first (to read one line
at a time), until the last line is read.

Then I'll need a StreamWriter to point to the end of the file, and
write to it.

Questions:

1) Is there a single Stream type of object that I can use for both
reading and writing, because I'll need to write at the point where
read operation is done?

2) Is there a method in the Stream object that reads only the last
line from a file (instead of reading all of the lines of strings)?
Jun 27 '08 #1
4 1291

Hi,

There is no StreamDoSomething

http://msdn.microsoft.com/en-us/library/system.io.aspx

A stream is in fact a stream of bytes. By the textstreams this can be
delimited by the CR and LF to break in so called lines of strings. Thereore
you can not get the last bytes of a string that starts with a CR and LF,
just because you don't know what is the last.

Cor

http://msdn.microsoft.com/en-us/library/system.io.aspx
"Curious" <fi********@yahoo.comschreef in bericht
news:2a**********************************@l42g2000 hsc.googlegroups.com...
>I have a file to which I'll need to at first read until it locates the
last line of string.

According to content of the last line of string, I'll need to write
something to this file right after the last line of string.

This requires that I'll need a StreamReader at first (to read one line
at a time), until the last line is read.

Then I'll need a StreamWriter to point to the end of the file, and
write to it.

Questions:

1) Is there a single Stream type of object that I can use for both
reading and writing, because I'll need to write at the point where
read operation is done?

2) Is there a method in the Stream object that reads only the last
line from a file (instead of reading all of the lines of strings)?
Jun 27 '08 #2
Unless the file size is large, I think the easiest way would be the following:

string [] lines = System.IO.File.ReadAllLines("somefile.txt");
// do some manipulations with lines[lines.Count - 1];
System.IO.File.WriteAllLines("somefile.txt", lines);
"Curious" wrote:
I have a file to which I'll need to at first read until it locates the
last line of string.

According to content of the last line of string, I'll need to write
something to this file right after the last line of string.

This requires that I'll need a StreamReader at first (to read one line
at a time), until the last line is read.

Then I'll need a StreamWriter to point to the end of the file, and
write to it.

Questions:

1) Is there a single Stream type of object that I can use for both
reading and writing, because I'll need to write at the point where
read operation is done?

2) Is there a method in the Stream object that reads only the last
line from a file (instead of reading all of the lines of strings)?
Jun 27 '08 #3
Thanks for sharing!
Jun 27 '08 #4
Good to know that there is a way to get all of the lines in a single
operation, and get the last line!
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dumboo | last post by:
hi there i m using fstream object for reading through and writing to my file, using following code char c; while(!m_aa.eof()) { m_aa.read(&c, sizeof(c)); m_oo.write(&c, sizeof(c));
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
2
by: Lisa Pearlson | last post by:
Hi, I am wanting to write an internet 'server' program. For ease, I want to use PHP-CLI with XINET super server. XINET communicates with the 'server' program via STDIN/STDOUT. I'm not sure...
5
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
4
by: thiago777 | last post by:
I have only 1GB of RAM so I cannot work with files too big with the ReadtoEnd method. Here is the code Im trying so that the file would split in pieces of 256MB: Try Dim BInput...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
10
by: vincex200 | last post by:
My group needs help with this program. We attempted to start it and got no where. Please help us. Write a C++ program that will read data from a file, perform computation on the data, then print...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
1
by: mamul | last post by:
Hi All, I am able to read and write to .txt, .bin files. but the same program is not support for .docx file . can someone help me the procedure to perform read write operation on .docx file. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.