473,803 Members | 3,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read and write to ascii file at the same time?

VM
Is it possible to open an ascii file, read the first 499 lines, overwrite
line 500 with the new string, and close the file? All the lines before and
after line 500 will be untouched.

Thanks.


Nov 16 '05 #1
2 6800
Yes and no. Your question is a little more complicated then just reading
and writing lines.

You can certainly open a FileStream with read/write permissions. However,
streams work on bytes, not lines. Now, you can create a StreamReader or
StreamWriter from a file stream, but these are mutually exclusive meaning
that you cannot read and write at the same time. You can normally chain the
readers and writers, but I do not know how that would behave if you pointed
them to the same stream (normally it is used to copy the contents).

What you might try doing is to read the lines in and copy them to a
temporary file, changing the lines that you want. Then writing that file
onto the original.

Or, if you are not shy of using bytes, you could work with the FileStream
directly and determine where line 500 begins and end and overwrite it, but
you can run into problems if the byte count of what you are writing is not
the same as what you are replacing.

"VM" <vo******@yahoo .com> wrote in message
news:O4******** ******@TK2MSFTN GP11.phx.gbl...
Is it possible to open an ascii file, read the first 499 lines, overwrite
line 500 with the new string, and close the file? All the lines before and
after line 500 will be untouched.

Thanks.

Nov 16 '05 #2
VM
That's what I'm doing currently doing (using temporary file). But I assumed
there could be an easier way to modify an existing file, rather than copy
the lines to a temp. file, modifying, and copying it back to original.

Thanks anyway,
Vaughn
"Peter Rilling" <pe***@nospam.r illing.net> wrote in message
news:#w******** ******@TK2MSFTN GP12.phx.gbl...
Yes and no. Your question is a little more complicated then just reading
and writing lines.

You can certainly open a FileStream with read/write permissions. However,
streams work on bytes, not lines. Now, you can create a StreamReader or
StreamWriter from a file stream, but these are mutually exclusive meaning
that you cannot read and write at the same time. You can normally chain the readers and writers, but I do not know how that would behave if you pointed them to the same stream (normally it is used to copy the contents).

What you might try doing is to read the lines in and copy them to a
temporary file, changing the lines that you want. Then writing that file
onto the original.

Or, if you are not shy of using bytes, you could work with the FileStream
directly and determine where line 500 begins and end and overwrite it, but
you can run into problems if the byte count of what you are writing is not
the same as what you are replacing.

"VM" <vo******@yahoo .com> wrote in message
news:O4******** ******@TK2MSFTN GP11.phx.gbl...
Is it possible to open an ascii file, read the first 499 lines, overwrite line 500 with the new string, and close the file? All the lines before and after line 500 will be untouched.

Thanks.


Nov 16 '05 #3

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

Similar topics

3
3725
by: Albert Tu | last post by:
Dear there, We have an x-ray CT system. The acquisition computer acquires x-ray projections and outputs multiple data files in binary format (2-byte unsigned integer) such as projection0.raw, projection1.raw, projection2.raw ... up to projection500.raw. Each file is 2*1024*768-byte big. I would like to read those files and convert to ascii files in %5.0f/n format as projection0.data ... projection500.data so that our
1
4312
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
4
5815
by: Martin Hvidberg | last post by:
Dear group I need to make a very simple piece of code in C, that can be command line executed and will compile on Linux, i.e. gcc. It should read a ascii Comma Separated Values (CSV) file and write the data (numbers) to another ascii file in a specific fixed width format (fwf). Let me show an example. >>> Infile.csv
10
2548
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.538 / Virus Database: 333 - Release Date: 11/10/2003
9
7919
by: John Howard | last post by:
How can I read a text file that is on a UNIX server in VB.Net? Please keep it simple. Thanks, John
4
12150
by: Josema | last post by:
Hi to all, Im searching a hand to solve an Exception that i get when i try to give the user to a gmail account... Any help would be appreciated.. Thanks in advance This is a piece of my code: Server=new TcpClient("pop.gmail.com",995); //stablish a connection
1
1333
by: Stephen | last post by:
Hi All, is it possible to read a file and write to the same file but the condition is: 1. suppose there is a line "Today is Wednesday. A beautiful day" and I want to change it to "Today is Thursday. A beautiful day" Thanks, Stephen
8
18657
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in adv.
5
11281
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a couple areas that have ASCII text that I need to extract. At the end of the 2580 bytes, I can read the report like a standard text file. It should have CR/LF at the end of each line. What is the best way for me to read this report using C#. It is...
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.