File Stream Problem

Programming, for all ages and all languages.
Post Reply
Guest

File Stream Problem

Post 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.
ark

Re:File Stream Problem

Post 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.
ark

Re:File Stream Problem

Post 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;
}
ark

Re:File Stream Problem

Post 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).
Guest

Re:File Stream Problem

Post 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.
ark

Re:File Stream Problem

Post 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.
Guest

Re:File Stream Problem

Post 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.
Post Reply