Wrox Programmer Forums
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 12th, 2003, 07:11 AM
Authorized User
Join Date: Aug 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default FILE I/O


I have a text file that I am trying to update. I want to open the file, change a couple of fields and write the rest of the file back as the same. I have tried to use:

FILE *fp;
if (fp = fopen(sFile, "r+") != NULL)

but I do not understand how you can open the file, write the data until you know you are where you want to change the data, change that text, then continue to write the rest of the file. I have a text file like so:

FILE I:\30050N50.P01 RP090L v3.32 - 04 DEC 2001 ENGLISH UNITS

COUNTY 50 ROUTE nc 50 DIR North(+) LANE 1
FILE_NAME 30050n50 USER_REF_2 0 USER_REF_3 30050n50
DATE 05/02/2003 TIME 12:22:20 DCF 14944 E

        MILES IN IN/MI
                     ROUGH RUT RUT AVG
--------- --------- ------- ----- ----- ------ ------ ------
    0.000 0.100 0.100 0.07 0.055 96 82 89
    0.100 0.200 0.100 0.01 0.081 85 81 83
    0.200 0.300 0.100 0.06 0.046 73 71 72
    0.300 0.400 0.100 0.01 0.045 78 89 84
    0.400 0.500 0.100 -0.00 0.048 70 78 74
    0.500 0.600 0.100 0.00 0.038 79 82 81
    0.600 0.700 0.100 -0.04 0.042 71 68 69

Suppose I want to change ROUTE from "nc 50" to "NC 181". How would I do that and keep the rest of the file the same?


Old August 14th, 2003, 07:50 AM
Authorized User
Join Date: Jun 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts

My cheap $0.02:
Assuming you know the format of the file (as in your example), you'd also know how many bytes there are from the beginning of the file to the beginning of the field you want to change, right?
For instance, in your example, it seems that the file uses fixed-width fields.
So, you know that "FILE" is at the beginning of the file, and is 4 chars wide, followed by a space, and then (looks like) 18 chars (the file path and the spaces after it), and then the RP090L... code, followed by a date, and then "ENGLISH UNITS"... and so on.. if you know that "ROUTE" always begins on byte X, then you can use fseek to move the file pointer to the Xth byte in the file. Then, of course, you'd have to move forward by 5 bytes (the width of "ROUTE"), plus one more for the sapce... so you might as well fseek to X+6.
Then, I am assuming that the field where "nc 50" is written is fixed-width, you simply write your new Route (i.e. "NC 181"). This will overwrite "nc 50" and one space following that, but that is a safe operation (since the route, as I said, is written in fixed-wodth... Actually, to be safe, you should padd the new Route with spaces to the right, so that if the old Route was longer than the new one, you'd overwrite the whole old route...
And then you're done...

You need to open the file in read & write mode,
and, as I said, I assumed your file has fixed-width fields, to make things easy.
If you don't have the luxury of fixed-width fields, you must parse through the file until you find the spot where you want to write...

Old August 19th, 2003, 11:12 PM
Authorized User
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts

Read each line of your file into a string variable, and then use the string functions to search out the spots you want to change, change them, then write the string variables back to the file which will overwrite what used to be in there.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 10:50 AM
How to display .tif file, .pdf file and .jpg file phuong171 ASP.NET 1.0 and 1.1 Basics 1 March 13th, 2007 07:29 AM
Save PDF file as text file in VB.Net kvenkatu Classic ASP Basics 0 April 7th, 2006 01:09 PM
SQL 2000 database / text file / binary file pallone Javascript How-To 3 January 28th, 2005 01:26 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.