Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 12th, 2003, 07:11 AM
Authorized User
 
Join Date: Aug 2003
Location: Cary, NC, USA.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default FILE I/O

Hi,

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
OPERATOR jimmy DRIVER jimmy VEHICLE 1151 EQUIPMENT 062-1151
FILE_NAME 30050n50 USER_REF_2 0 USER_REF_3 30050n50
DATE 05/02/2003 TIME 12:22:20 DCF 14944 E
WAVELENGTH_LONG 300 ft
WAVELENGTH_SHORT none

        MILES IN IN/MI
                     ROUGH RUT RUT AVG
  FROM TO DIST AVG STD IRI 1 IRI 2 IRI
--------- --------- ------- ----- ----- ------ ------ ------
    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?

Thanks!
Andy

Reply With Quote
  #2 (permalink)  
Old August 14th, 2003, 07:50 AM
Authorized User
 
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

HTH,
Olorin
Reply With Quote
  #3 (permalink)  
Old August 19th, 2003, 11:12 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:26 AM.


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