Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Open ??????????????

Status
Not open for further replies.

SteveBrett

Programmer
Apr 24, 2000
107
MT
I start as a trainee developer next week and have been working through the &quot;C++ in 21 days&quot; book and have the following problem.<br><br>In Visual C++ I can't seem to open a text file (lmhosts) and write data to it without creating a new file each time.<br><br>I also can't seem to write the end of line character and so get a new file with a single new line every time I run the prog.<br><br>I've tried using the archive object but am getting control characters in there which I don't want.<br><br>Any pointers or help would be greatly appreciated.<br><br>Cheers,<br><br>Steve<br><br>p.s I learned C++ on Unix and it was soooo much easier ...
 
May I ask which commands you are using to write to the file, from fstream.h? or from the older C File I/O (fread, fwrite, etc) <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
I've been using the following and worked it out at 2am this morning ...<br><br>stream = fopen( &quot;c:\\windows\\lmhosts&quot;, &quot;a+&quot; );<br>fprintf( stream, &quot;%s%c&quot;, Newline, c );<br><br>I doubt it is the best way but it works which will do for now. This has raised another problem though, I need to reference the installation directory for NT within the path for the stream.<br>I've tried using %windir% (I guess it will be %winnt% for NT) with no luck (caused a exception violation!!). I need to do this as some of the chaps at work install NT in D and E drives etc.<br>[ the example above was on my machine at home]<br><br>Cheers,<br><br>Steve
 
I see , so you are using the old C sytle Method for handling files (which is the way it seems to be with most of the Microsoft-made stuff)<br><br>just to see if this helps any the c++ method is shown as so.<br>#include &lt;fstream.h&gt; (omit .h for standard if it works for you)<br><br>ofstream outfile; //use ofstream to creat output pointer<br>ifstream infile;&nbsp;&nbsp;//use ifstream to create file in<br><br>for an output to file<br>can be easy as<br><br>outfile.open(&quot;filename&quot;,ios::eek:ut);<br><br>Other ios:: you can use to define the type of opening<br><br>ios::app&nbsp;&nbsp;&nbsp;The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream::seekp function.<br><br><br>ios::ate&nbsp;&nbsp;&nbsp;The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.<br><br><br>ios::in&nbsp;&nbsp;&nbsp;The file is opened for input. The original file (if it exists) will not be truncated.<br><br><br>ios::eek:ut&nbsp;&nbsp;&nbsp;The file is opened for output.<br><br><br>ios::trunc&nbsp;&nbsp;&nbsp;If the file already exists, its contents are discarded. This mode is implied if ios::eek:ut is specified, and ios::ate, ios::app, and ios:in are not specified.<br><br><br>ios::nocreate&nbsp;&nbsp;&nbsp;If the file does not already exist, the function fails.<br><br><br>ios::noreplace&nbsp;&nbsp;&nbsp;If the file already exists, the function fails.<br><br><br>ios::binary&nbsp;&nbsp;&nbsp;Opens the file in binary mode (the default is text mode).<br>Note that there is no ios::in or ios::eek:ut default mode for fstream objects. You must specify both modes if your fstream object must both read and write files.<br><br>you can combine these types using ¦<br>like ios::binary ¦ ios::eek:ut<br><br>from there if it was sucessful to output is easy as<br>outfile&lt;&lt;&quot;something&quot;;<br>of course you may want to put binary data out, in which you use <br>.put for single byte output<br>and .write for a block of bytes output<br><br>same with file input, only .get, or .read, and will use &lt;&lt; operator as well<br><br>to close just use .close, check your MSDN for further information on fstream. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
many thanks ... still can't reference install direstory though ....<br><br>Cheers,<br><br>Steve
 
Unlike VB you cant use constants to find directories, or none that I know of, but there is a way in the windows API, which I will find later tonight. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top