File operation problem using fstream

Hey, I'm writing a simple program that will mask a string of bytes - a file - with another string of bytes using xor. I've wrote about 600 lines of code, quite eazy to read.

The problem occured when my program stopped writing data, reporting badbit exceptions when writing a char. I dont know the reason, but it seems that when I debug the project in MSVS9 program works fine, however when I run it in cmd I cant write a thing.

Here is the main.cpp with my write function, I have zipped the rest and uploaded it on http://myfreefilehosting.com/f/f634aa52de_0.01MB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//FILE: main.cpp PROJECT: bitmask testing
//  by Gregor Časar
#include <iostream>
#include <fstream>

#include "commands.h"
#include "mask.h"

void quitf(char * parameters)
{
    exit(1);
};


_MASK * mask;
_COMMANDS * cmd;

std::fstream * stream;

void changeMaskf(char * parameters)
{
    if(mask->change(parameters))
        std::cout<<"Mask changed."<<std::endl;
};

void displayCommands(char * parameters)
{
    char** cbuffer=cmd->getCommands();
    char** dbuffer=cmd->getDescriptions();
    std::cout<<"Avalible commands:"<<std::endl;
    for(int i=0; i<cmd->getCount(); i++)
    {
        std::cout<<cbuffer[i]<<std::endl;
        std::cout<<' '<<((dbuffer[i]=='\0')?"No description avalible":dbuffer[i])<<std::endl;
    }
};

void changeFile(char * parameters)
{
    if(stream->is_open())
        stream->close();
    stream->open(parameters, std::fstream::in|std::fstream::out|std::fstream::binary);
    if(stream->is_open())
        std::cout<<"Operation complete"<<std::endl;
    else
    {
        stream->open(parameters, std::fstream::in|std::fstream::out|std::fstream::trunc|std::fstream::binary);
        if(stream->is_open())
            std::cout<<"Created file \""<<parameters<<"\"."<<std::endl;
        else
            std::cout<<"Could not open \""<<parameters<<"\"."<<std::endl;
    }        
};

void closeFile(char * parameters)
{
    if(stream->is_open())
        stream->close();   
};

void write(char * parameters)
{
    if(stream->is_open())
    {
        char* p=parameters;//This is necesery.
        int i; char ch;
        for(i=0; p[i]!='\0'; i++)
        {
            try
            {
                stream->put(p[i]);
            }
            catch (std::fstream::failure e)
            {
                break;
            }          
        }
        std::cout<<"Wrote "<<i<<" bytes of data."<<std::endl;
    }
    else
        std::cout<<"Could not open output file."<<std::endl;
};

void main(void)
{
    void (*fp)(char *);

    mask=new _MASK();//Create an instance of the mask class
    cmd=new _COMMANDS();//Create an instance of the commands class
    stream=new std::fstream();
    stream->exceptions(std::fstream::badbit);
    //populate the command class
    cmd->changeStream(NULL, _ERROR|_LOG);//Stop displaying _ERROR and _LOG streams
    fp=quitf;
    cmd->add("quit", fp);
    cmd->add("exit", fp);
    fp=changeMaskf;
    cmd->add("cm", fp, "Changes the mask");
    fp=changeFile;
    cmd->add("open", fp, "Changes the input/output file");
    fp=closeFile;
    cmd->add("close", fp, "Closes the input/output file");
    fp=write;
    cmd->add("write", fp, "Writes to the output file");
    fp=displayCommands;
    cmd->add("help", fp, "Displays a list of avalible commands");
    
    std::cout<<"Bitmask program v 1.0, 2008"<<std::endl<<" For a list of commands enter \"help\"..."<<std::endl;//Display some user-friendly instructions

    while(1)//Handle user input line by line
        cmd->handleInput();
}
Correction: When I debug in MSVS9 I USUALY dont get errors but when I do exceptions
First-chance exception at 0x7c812a5b in Win32 console test.exe: Microsoft C++ exception: std::ios_base::failure at memory location 0x0012f97c..
First-chance exception at 0x7c812a5b in Win32 console test.exe: Microsoft C++ exception: std::ios_base::failure at memory location 0x0012f9bc..
occure.

Any help would be aprechiated,
thanks
Last edited on
Update!

I've noticed that writing sucseeds, if I dont use truncate option on opening, in other words - If I open an existing file.
Topic archived. No new replies allowed.