Page 1 of 1

File Stream Problem

Posted: Sun Dec 22, 2002 11:04 pm
by Guest
Hi,

I have this piece of code beloew and it is crashing my program. I cannot see a problem with it.

Code: Select all

void WriteLogFile(const char *pMsg) {
   fstream fs(LogFile, ios::in | ios::out | ios::binary);
   if (fs.is_open()) {
      fs.seekp(0, ios::end);
      fs.write(pMsg, strlen(pMsg));
      fs.close();
   }
}
I have code almost identical to this in another program and it works fine. LogFile is definately OK, the file does get created, however as soon as it tries to write to the file it crashes.

any ideas??

thanks.

Re:File Stream Problem

Posted: Mon Dec 23, 2002 5:50 pm
by ark
I don't see any problems with it at first glance. This probably isn't the problem, but it's good practice anyway, try adding an assert in there:

Code: Select all

#include <cassert>

void WriteLogFile(const char* pMsg)
{
    assert(pMsg != NULL);

    fstream fs(LogFile, ios::in | ios::out | ios::binary);

    if (fs.is_open())
    {
        fs.seekp(0, ios::end);
        fs.write(pMsg, strlen(pMsg));
        fs.close();
    }
}
Also, unless there's a reason why you have the file open for both input and output at the same time, it may be easier just to use an ifstream object instead of an fstream object.

Re:File Stream Problem

Posted: Mon Dec 23, 2002 5:56 pm
by ark
I just tested the code out, and there's no problem with the following program, which means the bug is probably external to the WriteLogFile function:

Code: Select all

#include <cassert>
#include <fstream.h>

void WriteLogFile(const char* pszMsg)
{
    assert(pszMsg != NULL);

    fstream fs("logfile", ios::in | ios::out | ios::binary);

    if (fs.is_open())
    {
        fs.seekp(0, ios::end);
        fs.write(pszMsg, strlen(pszMsg));
        fs.close();
    }
}

int main()
{
    WriteLogFile("Test write");

    return 0;
}

Re:File Stream Problem

Posted: Mon Dec 23, 2002 6:03 pm
by ark
To clarify, there's probably a problem with your pMsg pointer. I assume you're trying to write a string to the logfile. If so, make sure it is null-terminated and that its length is within the bounds of the character array. The assert will also make sure that the pointer is not a null pointer. Also, if the string was dynamically allocated, make sure that it wasn't deleted or freed before being passed to WriteLogFile. A good way to avoid this kind of thing is to always set pointers to NULL after you delete (of free) them. That way, the assert in WriteLogFile will catch the problem (of course, make sure you add it or it obviously won't catch the problem).

Re:File Stream Problem

Posted: Mon Dec 23, 2002 7:31 pm
by Guest
Thanks for your help, but I am still having trouble.

Code: Select all

void WriteLogFile() {
   fstream fs(LogFile, ios::in | ios::out | ios::binary);
   if (fs.is_open()) {
       char msg[] = "This is some text!\0";
      fs.write(msg, strlen(msg));
      fs.close();
   }
}
This code above also crashes?? So it is something to do with the write(), I am linking with Multi-Thread C Library, could this make a difference? Although streams are not part of C are they?

And in case you are wondering, this code is protected by a critical section as more than 1 thread needs access to it, but I know for sure that the critical section code is ok.

Re:File Stream Problem

Posted: Mon Dec 23, 2002 9:57 pm
by ark

Code: Select all

char msg[] = "This is some text!\0";
The \0 is not really necessary...the compiler will automatically put it onto the end of string literals, but I guess it doesn't really hurt.

The fact that streams are not a part of C shouldn't make a difference (although I won't say it doesn't). Do you have access to a debugger, such as Visual C++? It would be helpful to be able to look at the call stack when the crash occurs, if that feature is available.

Also, what is "LogFile"...is it a constant? Try changing it to a string literal, just as a test:

Code: Select all

fstream fs("testlogfile", ios::in | ios::out | ios::binary);
Also, out of curiosity, is there some reason you're writing the logfile in binary mode? If you're just writing text, you probably don't want binary mode, although that shouldn't make the program crash.

If you know for certain that the problem is occurring at the call to fs.write, then the problem is probably either with the fs object or your pointer. Given that your function with no pointer still crashed, there's a good chance it's a problem with the fs object, but again there are no guarantees.

If it's a problem with the multi-threading library, I don't really have enough experience with it to diagnose the problem.

Re:File Stream Problem

Posted: Tue Dec 24, 2002 5:21 am
by Guest
Thanks for your help.

The reason I am opening in binary is that there may be some times where I need to write binary data into that log file.

I did find a solution, however I am still not sure what the problem was with the other code as I have used code like it many times before.

LogFile is a char array char LogFile[MAX_PATH]. It is filled with an appropriate file path before it is used.

This is the solution:

Code: Select all

void WriteLogFile(const char *pData, int nDataSize) {
   ofstream fs(LogFile, ios::in | ios::binary);
   if (fs.is_open() && pData) {
      fs.seekp(0, ios::end);
      fs.write(pData, nDataSize);
      fs.close();
   }
}
This function has been modified slightly to allow me to write binary data, but it does the same as before and the above problem still exists, I have just found a workaround.

For some reason everything goes exactly as planned when I do it like this. The file is opened for reading and writing and in binary mode.

thanks for your help.