Log File Wrap Over Failing

Hi All,

I'm having an issue with my app at midnight when it's meant to start writing to a new log file. I've created this demo app (spooling a new file every min) to highlight / work the issue. If anyone has any ideas, any help is greatly appreciated.

It looks as if once reopened, the fileOResults is only accessible from within the IF block.

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
using namespace std;

const std::string fctGetFileName() {
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%Y-%m-%d-%I-%M-ADSB-Data-RollBug.csv", &tstruct);
    return buf;
}

int main(int argc, char *argv[])
{
    string strCurrentFileName=fctGetFileName();
    ofstream fileOResults(strCurrentFileName);    
    fileOResults << "\n INIT";

    while (1 < 2) {
        if (strCurrentFileName != fctGetFileName()){
            fileOResults.close();
            strCurrentFileName = fctGetFileName();
            ofstream fileOResults(strCurrentFileName);  
            fileOResults << "\n NEW FILE";
        }
        fileOResults << ".";
        sleep(10);
    }

    fileOResults.close();
    return 0;
}


Output Sample;
(I was hoping for 6 x fullstops after each NEW FILE)
pi@raspberrypi:~/cpp-app $ head *RollBug.csv
==> 2021-04-24-01-37-ADSB-Data-RollBug.csv <==

 INIT.....
==> 2021-04-24-01-38-ADSB-Data-RollBug.csv <==

 NEW FILE
==> 2021-04-24-01-39-ADSB-Data-RollBug.csv <==

 NEW FILE
==> 2021-04-24-01-40-ADSB-Data-RollBug.csv <==

 NEW FILE
Last edited on
Line 22 above creates a new ofstream variable with the same name as fileOResults but that is scoped to the enclosing if statement. Just open the new file without creating a new ofstream variable.

 
fileOResults.open(strCurrentFileName);

Thanks you dutch, that works a treat. Makes perfect sense once you said it.

I didn't think the compiler would allow two variables of the name name to be defined, that that's a lesson learned.

I also didn't know that variables could be confined to within an IF statement. I was on the false impression when starting out the smallest scope would be to within a procedure (i.e. Main) or function. I later realised it must be constrained smaller, but had the syntax issue to overcome as well.

Everyday's a school day. Thank you - Very much appreciated :-)
the compiler would allow two variables of the same name to be defined


It will if there are at different scope levels. The one defined at an 'inner' scope is used. At the end of a scope, variables defined in that scope are no longer available.
I'm surprised your compiler didn't warn you about this. Have you turned your warning level down?
GCC, for instance, will only warn on shadowing if you explicitly enable -Wshadow (-Wall and -Wextra won't cover it)

warning: declaration of 'fileOResults' shadows a previous local [-Wshadow]

Visual Studio is quicker to warn here, I think it does it at warning level 3 or 4.
Last edited on
GCC, for instance, will only warn on shadowing if you explicitly enable -Wshadow (-Wall and -Wextra won't cover it)

Huh. That seems... counter-intuitive. Oh well.
Topic archived. No new replies allowed.