Truncate ofstream failing

I'm trying to truncate
"The Big Picture.txt"
so that it will be data free from the beginning, thus allowing the program to rewrite the same data onto an empty, but identical, file. Data is being copied from
"The Adopted.txt"
and
"The Originals.txt"


The truncation wipes the the file completely, and nothing replaces it by the end of the program.
Can someone please help me understand what I've done wrong (presumably in line 26 with trunc declaration). I've been nose deep in references for hours. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <string> 
using namespace std;
    
int main()                                                                  
{        
    ifstream theAdopted("The Adopted.txt");
    ifstream theOriginals("The Originals.txt");
    ofstream theBigPic("The Big Picture.txt");

    string lineA;
    string lineB;
    
    //Retreive his family's grades.      
    if(theAdopted.fail())                                    
    {   cerr << "ERROR: Cannot open " << theAdopted << ". \n";
        return EXIT_FAILURE;  } 

    //Retreive her family's grades.                    
    if(theOriginals.fail())                                    
    {   cerr << "ERROR: Cannot open " << theOriginals << ". \n";
        return EXIT_FAILURE;  }
        
    //Retrieve and clear our (combined) family's grades.
    theBigPic.open("The Big Picture.txt",ios_base::in|ios_base::out|ios_base::trunc);                
    
    //Merge sort.
    getline(theAdopted, lineA);
    getline(theOriginals, lineB);
    while((!theAdopted.eof()) && (!theOriginals.eof()))
    {     
        if (lineA < lineB)
        { 
            theBigPic << lineA << endl;
            getline(theAdopted, lineA);
        } 
        else 
        {
            theBigPic << lineB << endl;
            getline(theOriginals, lineB);
        }
    } 
    cout << "Input data mergered to file 'The Big Picture.exe'." << endl;
    //Close files.
    theAdopted.close();
    theOriginals.close();
    system("pause");
    return 0;
}
Last edited on
Change line 10 from
 
    ofstream theBigPic();
to
 
    ofstream theBigPic;

The first declares a function which returns a value of type ofstream; the second defines an ofstream object.
Last edited on
That worked for printing the data. But the file still doesn't clear at the beginning, and display at the end. That's what I'm attempting to do. Is it possible? Removing the () basically made it as if the trunc were never there.
Should I consider a buffer?
The current version attempts to open theBigPic twice, first at line 10, then again at line 26. The second will fail as the file is already open.
I thought of that, but when I remove line 10, I get error
37 `theBigPic' undeclared (first use this function)
You could in fact remove line 26 since line 10 has already opened the output file for you.

Also your file merge is incomplete. When the end of one file is reached, no further lines are processed from the other file - that's a logic error. You could try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    getline(theAdopted, lineA);
    getline(theOriginals, lineB);
    
    while ( theAdopted &&  theOriginals )
    {     
        if (lineA < lineB)
        { 
            theBigPic << lineA << endl;
            getline(theAdopted, lineA);
        } 
        else 
        {
            theBigPic << lineB << endl;
            getline(theOriginals, lineB);
        }
    } 
    
    while ( theAdopted )
    {     
        theBigPic << lineA << endl;
        getline(theAdopted, lineA);
    } 
    
    while ( theOriginals )
    {     
        theBigPic << lineB << endl;
        getline(theOriginals, lineB);
    } 
Well you might just be a lifesaver because after testing your code I found that I had been missing a couple units from one of the files!! Goodness. Thanks you so much!

I just have one more quiestion.
When I call the .txt files to open or close, am I supposed to SEE either of these things happening, or are they in background? I would think that open would pop the tab open on the IDE for said file?
When I call the .txt files to open or close, am I supposed to SEE either of these things happening, or are they in background?

You won't usually see anything happen. (but it might be system-dependent).
When the output is closed, the file buffer is flushed and any remaining text is physically written to the file, as well as updating the file attributes such as date modified. When I tested this, I had the output file open in a text editor, which does pop up its own message when that happens, but i don't think all editors behave that way.

As for opening the file, the changes won't necessarily be reflected (I think) until either some data is physically written to the file or when it is closed.
Last edited on
Awesome, thank you.
Topic archived. No new replies allowed.