C++ output to a file

C++ output to a file

Post by Ron Natali » Thu, 20 Jul 2000 04:00:00




> I cannot get a program to write to a file using ios::ate
> as i need to insert spaces into an existing file.
> Does anyone have any suggestions.

> Code:

> void main()
> {
>      ofstream fout( "testfile.txt", ios::ate);
>      int t1=1;
>      int t2=2;
>      fout << t1 << ' ' << t2;
> }

First off, main MUST return int.

Second, the use of the words "insert" seems at odds with ios::ate.
Ate means seek to the end after opening (that is, append to the end of
this existing file).  I believe it "ATE" is dervied from "AT END."
The "app" for "append" mode means seek to the end prior to each write
(similar to the same mode in stdio).

 
 
 

C++ output to a file

Post by John Harriso » Thu, 20 Jul 2000 04:00:00



Quote:> I cannot get a program to write to a file using ios::ate
> as i need to insert spaces into an existing file.
> Does anyone have any suggestions.

> Code:

> void main()
> {
>      ofstream fout( "testfile.txt", ios::ate);
>      int t1=1;
>      int t2=2;
>      fout << t1 << ' ' << t2;
> }

You cannot insert spaces (or anything else) into a file in any mode. If you
want to add something to the middle of a file you read the file into memory,
insert whatever you want while its in memory, then write the the whole thing
out to a file again.

john

 
 
 

C++ output to a file

Post by Legles » Fri, 21 Jul 2000 04:00:00


I cannot get a program to write to a file using ios::ate
as i need to insert spaces into an existing file.
Does anyone have any suggestions.

Code:

void main()
{
     ofstream fout( "testfile.txt", ios::ate);
     int t1=1;
     int t2=2;
     fout << t1 << ' ' << t2;

Quote:}

 
 
 

C++ output to a file

Post by David Harmo » Fri, 21 Jul 2000 04:00:00


On Wed, 19 Jul 2000 12:22:39 -0400 in comp.lang.c++,


>Second, the use of the words "insert" seems at odds with ios::ate.
>Ate means seek to the end after opening (that is, append to the end of
>this existing file).

ios::ate should imply not ios::trunc.  However that it does not do so
seems to be a bug in some popular stream implementation.  I suggest the
workaround:
      ofstream fout( "testfile.txt", ios::in|ios::out|ios::ate);
for those to whom it applies.
 
 
 

1. Creating different output file names in a loop in C++

Hi,

If I use the following for normal output:

 ofstream outputfile("nameofoutputfile.txt");

how do I change the filename condition on a loop counter in C++?

i.e.,
for( count=0;count<5;count++)

how do I create 5 different output files with names such as?:
nameofoutputfile0.txt
nameofoutputfile1.txt
nameofoutputfile2.txt

Thank you for your help.

Thomas

2. SECURITY ENGRS NEEDED 4 REAL - VA Area - Small Company - Immediate Staffing

3. File Output in C++ ?

4. Apple LW IIg HD formatting problems!

5. C++ File Input and Output

6. CD-Rs

7. output and input of a cfg file for C++

8. C++ Input/Output File Streams

9. File comparison tool with parallel file output needed

10. Bypass query output file on print to file?

11. File Input & File Output

12. Program's input from file &output to file