Problem writing to a writable file 
Author Message
 Problem writing to a writable file

[I'm not sure if these are the most appropriate newsgroups for this
question.  Suggestions for a more appropriate newgroup are welcome.]

Can someone help with a problem I'm having trying to write to a file using
Visual C++ on Windows NT?  The relevant code sections are:

FileTable::update() {
...
        // Close the file, then open it, truncated, for writing.
        close();
        open(file_name, ios::trunc|ios::out);
        // Write all rows back to the file, substituting the updated row
        // for the one it is to replace.
        for (int i = 1; i <= total_rows; ++i)
        {
                if (i == update_row_number)
                {
                        write_row(*update_row);
                }
                else
                {
                        write_row(*rows[i - 1]);
                }
        }
        flush();
        close();
...

Quote:
}

void FileTable::write_row(vector<string>& r)
{
        for (int i = 0; i < r.size(); ++i)
        {
                *this << r[i];
                *this << "\n";
        }

Quote:
}

Class FileTable inherits from fstream, so the close, open, etc. are defined
in fstream.  The file is first closed, then opened with state
ios::trunc|ios::out, which truncates - zeros out - the file, and which
should also allow writing to the file; but any writes to the file fail.
The first time write_row is called, the write fails.  The FileTable
(fstream) object - this - ends up in an error state.  (bad() == true)
(r[i] does contain a valid string.)

This has me stumped because the file is writable and the program does
truncate the file to 0 size; but it cannot write to it.  When run on Linux,
the program writes to the file with no problems.

Any idea what's going on?  Could it be some subtle Windows NT configuration
issue?

Thanks -
--
Jim Cochrane



Mon, 24 Jun 2002 03:00:00 GMT  
 Problem writing to a writable file
Jim, NT is very {*filter*}y picky about permission flags.  you might need the Create
flag on or something.


Mon, 24 Jun 2002 03:00:00 GMT  
 Problem writing to a writable file
I found the problem, I think.  It appears to have to do with the fact that
FileTable inherits from fstream.  I don't know the exact details of why,
but adding two lines before the first write:

        *this << "";
        clear();

fix the problem.  Don't worry - it's not production code.


Quote:

>[I'm not sure if these are the most appropriate newsgroups for this
>question.  Suggestions for a more appropriate newgroup are welcome.]

>Can someone help with a problem I'm having trying to write to a file using
>Visual C++ on Windows NT?  The relevant code sections are:

>FileTable::update() {
>...
>    // Close the file, then open it, truncated, for writing.
>    close();
>    open(file_name, ios::trunc|ios::out);
>    // Write all rows back to the file, substituting the updated row
>    // for the one it is to replace.
>    for (int i = 1; i <= total_rows; ++i)
>    {
>            if (i == update_row_number)
>            {
>                    write_row(*update_row);
>            }
>            else
>            {
>                    write_row(*rows[i - 1]);
>            }
>    }
>    flush();
>    close();
>...
>}

>void FileTable::write_row(vector<string>& r)
>{
>    for (int i = 0; i < r.size(); ++i)
>    {
>            *this << r[i];
>            *this << "\n";
>    }
>}

>Class FileTable inherits from fstream, so the close, open, etc. are defined
>in fstream.  The file is first closed, then opened with state
>ios::trunc|ios::out, which truncates - zeros out - the file, and which
>should also allow writing to the file; but any writes to the file fail.
>The first time write_row is called, the write fails.  The FileTable
>(fstream) object - this - ends up in an error state.  (bad() == true)
>(r[i] does contain a valid string.)

>This has me stumped because the file is writable and the program does
>truncate the file to 0 size; but it cannot write to it.  When run on Linux,
>the program writes to the file with no problems.

>Any idea what's going on?  Could it be some subtle Windows NT configuration
>issue?

>Thanks -
>--
>Jim Cochrane


--
Jim Cochrane



Tue, 25 Jun 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. My .rc file must be writable when compiling

2. My .rc file must be writable when compiling

3. How to create a writable .text section?

4. Preserving writable server cursor after rollback (7.0, vc++, CDatabase/CRecordset)

5. Problem opening a file for read while it is open for write

6. File Read/Write Problem

7. problems writing to binary files

8. Problems with writing to a file

9. help - write to file in interrupt problems

10. Q: problem: CGI-programming with C (writing files and sending picyures)

11. problem writing to file

12. PROBLEM READING/WRITING FILE __SIMULTANEOUSLY__!

 

 
Powered by phpBB® Forum Software