samedi 24 novembre 2018

File Writing Operations within IF statements don't work in Dlang

Writing to a file in D doesn't work within an if-statement. What am I missing or misunderstanding ?

I've got this function :

void log_Event(){
write("Event has been logged...");

// WRITE THE FILE.
// File file_writable = File("Shell_Log.txt","w");
 if ( "Shell_log.txt".exists() ){
    File file_writable = File("Shell_Log.txt","w");
    file_writable.writeln("Data Log: ");
    for(int i=1;i<10;i++)
    file_writable.writeln(i);
    file_writable.close();
 } 

// READ THE FILE.
File file_readable = File("Shell_Log.txt","r");
while (!file_readable.eof()){
    file_readable.readln().writeln();
}file_readable.close();

}

This function doesn't write to the file until I comment out the if-statement like so :

//if ( "Shell_log.txt".exists() ){
    File file_writable = File("Shell_Log.txt","w");
    file_writable.writeln("Data Log: ");
    for(int i=1;i<10;i++)
    file_writable.writeln(i);
    file_writable.close();
 //} 

if you need to know where I'm calling the function, its within this switch :

    API_object* api_obj;
switch(args){
    case "createObject\n":
        api_obj = createObject();
        writeln( "String of Object: ",Object.stringof,
        "\nSize of Object: ", Object.sizeof,
        "\nAuto api_obj value: ", api_obj.date_str);
        api_obj.prinContent(api_obj.date_str);
        log_Event();
        return true;
    break;

Output is supposed to look like so :

Data Log: 
1
2
3
4
5
6
7
8
9

Aucun commentaire:

Enregistrer un commentaire