ofstream problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Eggler

    ofstream problem

    Hi,

    I'm trying to write to a text file with following code:
    std::ofstream out("/usr/share/NovaxTSP/INITreport.txt" , std::ios_base:: in |
    std::ios_base:: out | std::ios_base:: app);
    if(!out) { // if out couldn't be opened, print error and exit
    std::cout << "Cannot open reportfile:
    \"/usr/share/NovaxTSP/INITreport.txt\ "." << std::endl;
    exit(0);
    }
    double num = 100.45;
    char str[100];
    strcpy(str, newLine.c_str() );
    strcat(str,"\n\ r");
    out.write((char *) &num, sizeof(double)) ;
    out.write(str, strlen(str));
    out.close();

    the folder /usr/share/NovaxTSP/ has permissions set to 775 and the
    application is run as root but i still always get: Cannot open
    reportfile: "/usr/share/NovaxTSP/INITreport.txt" .
    Why??? I don't understand, i made a df -h and there's enough space available
    as well.
    Does anyone have any other ideas on what to check?
    I also touched the file before appending but that didn't change it either.

    I appreciate every input!

    Thank you to help me further!

    Ron
  • Gianni Mariani

    #2
    Re: ofstream problem

    Ron Eggler wrote:
    Hi,
    >
    I'm trying to write to a text file with following code:
    std::ofstream out("/usr/share/NovaxTSP/INITreport.txt" , std::ios_base:: in |
    std::ios_base:: out | std::ios_base:: app);
    if(!out) { // if out couldn't be opened, print error and exit
    std::cout << "Cannot open reportfile:
    \"/usr/share/NovaxTSP/INITreport.txt\ "." << std::endl;
    exit(0);
    }
    double num = 100.45;
    char str[100];
    NEVER EVER EVER DECLARE A CHAR ARRAY ON STACK
    strcpy(str, newLine.c_str() );
    AND THEN FILL IT WITH AN UNBOUND STRING.
    strcat(str,"\n\ r");
    "\n\r" is operating system specific. If you write that to some
    platforms (MSVC) for example, you'll get more than one \r.

    What's wrong with:
    std::string str = newLine + "\n";
    ?

    That code above will work correctly for any "newLine".
    out.write((char *) &num, sizeof(double)) ;
    This is also endian dependant - not good depending on your use for the
    output file. Are you really trying to write the bitwise representation
    of the machine specific "double" type ?
    out.write(str, strlen(str));
    out.close();
    >
    the folder /usr/share/NovaxTSP/ has permissions set to 775 and the
    application is run as root but i still always get: Cannot open
    reportfile: "/usr/share/NovaxTSP/INITreport.txt" .
    If you're running under linux, try

    touch /usr/share/NovaxTSP/INITreport.txt

    Or run your program under strace and see what error you're getting back
    from the OS.

    Comment

    Working...