![]() |
|
#1
|
|||
|
|||
reading and writing to a file in C++I need to write a small program that reads a file and adds somethings to it. The file contains file paths. This is what it needs to look like
1 i:\home8\test_maintenance.drw\02_test.fld\02PC342M .dwg i:\home8\test_maintenance.drw\02_test.fld\02PC342M 1 right now i just have i:\home8\test_maintenance.drw\02_test.fld\02PC342M .dwg in a file The code below is what i have so far. Any help?? thanks CPP / C++ / C Code:
Last edited by dsmith : 01-Mar-2004 at 13:59.
Reason: Added C syntax highlighting
|
|||
|
#2
|
||||
|
||||
|
Do you care how many lines are already in the file before writing a new line? If not, look into the append instead of /in addition to in and out for fstream.
|
|
#3
|
|||
|
|||
|
No it doesn't matter how many lines
Quote:
|
|
#4
|
||||
|
||||
|
I think it may be easier to open a seperate file for reading and a seperate file for writing and then copy the output file to the input file.
Then you should be able to do something like this: CPP / C++ / C Code:
Please note that I haven't tested that. I would make sure that your output file is correct before putting in the code to copy it over your input file. |
|
#5
|
|||
|
|||
|
If you want to append lines to a file, you can open it for reading
and writing, then write to the end of the file (no need to read the lines that already in the file). You could use iofile.seekg() to move around in the file, but that's not necessary if you open the file with the pointer already at the end. Maybe this will help (note that you can eliminate having to write "std::" everywhere by putting the statement "using namespace std;" just before your main() statement). Also, it's always a good idea to see whether the file was opened properly. The following compiles and executes OK on my Windows machine with Borland Version 5.5 and g++ version 3.3.1 CPP / C++ / C Code:
Dave |
|
#6
|
||||
|
||||
|
Here's a tip to help you get rid of all that std wrapping.
For stream I/O, you can simply test for true or false to see if an error occured while opening the file, or if a read was successful. Here's an example of that: CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
|
So, I tried the following:
CPP / C++ / C Code:
Neither Borland Version 5.5 or g++ version 3.3.1 compiled. Error messages indicated that ofstream was undefined (undeclared). When I un-commented the "using namespace std" statement, it compiled and executed properly. I have a lot less experience with C++ than with C, but so far, I have treated "using namespace std;" as an idiom: just do it. Is there any reason not to "just do it"? Dave |
|
#8
|
||||
|
||||
|
You need to use the .h when including fstream.h, not just <fstream>, otherwise it won't compile. I don't see any problem with using namespace std, unless you don't need it. You only need it if you're going to use std functions on objects outside of the std class. If you write a program that doesn't use any std functions, then there's no need for the namespace statement.
|
|
#9
|
|||
|
|||
|
Gnu c (g++ version 3.3.1 will not compile, with or without
the .h. Here are the g++ errors that prevent the compile with fstream.h and with the "using namespace std" commented out: Quote:
Dave |
|
#10
|
||||
|
||||
|
Why is it complaining about std:: if you don't have any in there? There must be something wrong. I've never tried G++. The exact code herein compiles just fine with no warnings under Borland 6 and VC 6.
CPP / C++ / C Code:
|
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linux Kernel Upgrade Mini Howto | dsmith | Computer Software Forum - Linux | 3 | 05-Apr-2004 23:10 |
| [Tutorial] Standard I/O | aaroncohn | C Programming Language | 20 | 27-Feb-2004 22:07 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The