p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Visual C++ (http://p2p.wrox.com/forumdisplay.php?f=88)
-   -   FILE I/O (http://p2p.wrox.com/showthread.php?t=2705)

awhittaker August 12th, 2003 07:11 AM


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?


Olorin August 14th, 2003 07:50 AM

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...


7stud August 19th, 2003 11:12 PM

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.

All times are GMT -4. The time now is 07:03 AM.

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