Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

Anonymous

unregistriert

1

31.01.2004, 17:32

Dateiarbeit

Hallo

Mein Problem ist folgendes:

Ich möchte eine Text-Datei öffnen und aus ihr lesen. Ich arbeite mit der fstream-Klasse. Allerdings funktioniert das Lesen nicht, und das Fehlerbit ios::failbit ist gesetzt. Ich habe keinen blassen Schimmer woran das liegen könnte (die Datei existiert, und die Klasse sgCString und sgCStringPath arbeiten korrekt). Ich hoffe jemand checkt den Fehler! :) :)

Quellcode

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
sgCStringPath StringPath;

    StringPath="C:\\TestText.txt";

    sgPrintInfo("","Pfad valide? %i",StringPath.GetBIsValid());
sgCFile* pFile=NULL;

    sgNewP(pFile,new sgCFile(StringPath,FILEOPENTYPE_READ,FILETYPE_TEXT));

    pFile->Open(false);

    char* pcString=NULL;
    pcString=(char* )pFile->GetP(1);

    sgCString String(1);
    String.Set(pcString,0,1);


    sgPrintInfo("","Pfad-Inhalt: %s",String);

    sgDeleteA(pcString);

    pFile->Close();

    sgDeleteP(pFile);


Es folgt der Quellcode meiner File-Klasse:

Quellcode

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
sgCFile::sgCFile(
                 const sgCStringPath& StringPath,
                 const sgEFileOpenType& FileOpenType,
                 const sgEFileType& FileType
                 )
    :m_StringPath(StringPath),
    m_FileOpenType(FileOpenType),
    m_FileType(FileType),
    m_pFStream(NULL)
{
    sgNewP(m_pFStream,new fstream());
}

sgCFile::~sgCFile(void)
{
    if(m_pFStream)
    {
        if(m_pFStream->is_open())
        {
            m_pFStream->close();
        }
        sgDeleteP(m_pFStream);
    }
    else
    {
        sgPrintError("sgCFile::~sgCFile",SG_WAS_NOT_INITIALIZED_BY,"sgCFile::sgCFile");
    }
}


void* sgCFile::GetP(const unsigned int uiSize)
{
    void* pReturn=NULL;
    if(m_FileOpenType==FILEOPENTYPE_READ)
    {
        if(!m_pFStream->eof())
        {
            sgNewA((char*& )pReturn,uiSize);
            m_pFStream->read((char* )pReturn,uiSize);
            if(m_pFStream->fail())
            {
                sgPrintError("sgCFile::GetP",SG_COULD_NOT_READ,"m_StringPath");
                if(m_pFStream->rdstate()&ios::failbit)
                {
                    sgPrintInfo("sgCFile::GetP","A possibly recoverable formatting or conversion error.");
                }
                if(m_pFStream->rdstate()&ios::badbit)
                {
                    sgPrintInfo("sgCFile::GetP","A severe I/O error or unknown state.");
                }
                m_pFStream->clear();
            }
        }
        else
        {
            sgPrintError("sgCFile::GetP",SG_END_OF_FILE,"m_StringPath");
        }
    }
    else
    {
        sgPrintError("sgCFile::GetP",SG_COULD_NOT_READ,"m_StringPath");
    }
    return pReturn;
}

void sgCFile ::SetP(const void* p,const unsigned int uiSize)
{
    if(p)
    {
        if(m_FileOpenType==FILEOPENTYPE_WRITE)
        {
            if(!m_pFStream->eof())
            {
                m_pFStream->write((const char* )p,uiSize);
                if(m_pFStream->fail())
                {
                    sgPrintError("sgCFile::SetP",SG_COULD_NOT_WRITE,"m_StringPath");
                    m_pFStream->clear();
                }
            }
            else
            {
                sgPrintError("sgCFile::SetP",SG_END_OF_FILE,"m_StringPath");
            }
        }
        else
        {
            sgPrintError("sgCFile::SetP",SG_COULD_NOT_WRITE,"m_StringPath");
        }
    }
    else
    {
        sgPrintError("sgCFile::SetP",SG_IS_NULL,"p");
    }
}

void sgCFile::Open(bool bCreate)
{
    if(bCreate)
    {
        switch(m_FileOpenType)
        {
            case FILEOPENTYPE_READ:
                if(m_FileType==FILETYPE_TEXT)
                {
                    m_pFStream->open(m_StringPath,ios::in);
                }
                else
                {
                    m_pFStream->open(m_StringPath,ios::in|ios::binary);
                }
                if(m_pFStream->fail())
                {
                    sgPrintError("sgCFile::Open",SG_COULD_NOT_OPEN,"m_StringPath");
                    m_pFStream->clear();
                }
            break;
            case FILEOPENTYPE_WRITE:
                if(m_FileType==FILETYPE_TEXT)
                {
                    m_pFStream->open(m_StringPath,ios::out);
                }
                else
                {
                    m_pFStream->open(m_StringPath,ios::out|ios::binary);
                }
                if(m_pFStream->fail())
                {
                    sgPrintError("sgCFile::Open",SG_COULD_NOT_OPEN,"m_StringPath");
                    m_pFStream->clear();
                }
            break;
        }
    }
    else
    {
        //test if the File exists:
        fstream* pFStream=NULL;
        sgNewP(pFStream,new fstream());
        pFStream->open(m_StringPath,ios::in);
        if(pFStream->good())
        {
            pFStream->close();
            switch(m_FileType)
            {
                case FILEOPENTYPE_READ:
                    if(m_FileType==FILETYPE_TEXT)
                    {
                        m_pFStream->open(m_StringPath,ios::in);
                    }
                    else
                    {
                        m_pFStream->open(m_StringPath,ios::in||ios::binary);
                    }
                    if(m_pFStream->fail())
                    {
                        sgPrintError("sgCFile::Open",SG_COULD_NOT_OPEN,"m_StringPath");
                        m_pFStream->clear();
                    }
                break;
                case FILEOPENTYPE_WRITE:
                    if(m_FileType==FILETYPE_TEXT)
                    {
                        m_pFStream->open(m_StringPath,ios::out);
                    }
                    else
                    {
                        m_pFStream->open(m_StringPath,ios::out||ios::binary);
                    }
                    if(m_pFStream->fail())
                    {
                        sgPrintError("sgCFile::Open",SG_COULD_NOT_OPEN,"m_StringPath");
                        m_pFStream->clear();
                    }
                break;
            }
        }
        else
        {
            sgPrintError("sgCFile::Open",SG_FILE_NOT_FOUND,"m_StringPath");
        }
        sgDeleteP(pFStream);
    }
}

void sgCFile::Close(void)
{
    if(m_pFStream)
    {
        if(m_pFStream->is_open())
        {
            m_pFStream->close();
            if(m_pFStream->fail())
            {
                sgPrintError("sgCFile::Close",SG_COULD_NOT_CLOSE);
                m_pFStream->clear();
            }
        }
        else
        {
            sgPrintError("sgCFile::Close",SG_WAS_NOT_INITIALIZED_BY,"sgCFile::Open");
        }
    }
    else
    {
        sgPrintError("sgCFile::Close",SG_WAS_NOT_INITIALIZED_BY,"sgCFile::sgCFile");
    }
}


Samuel G.