enum problems in fstream

Hello all.

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
enum TransactionType {Add, Delete, ChangeOnhand, ChangePrice};
.
.
.
struct BookRec {
        long isbn;
        String name;
        String author;
        int onhand;
        float price;
        String type;
};

struct TransactionRec {
        TransactionType ToDo;
        BookRec b;
};
.
.
.
 while(xactIn.read ((char *) &xBuf, sizeof(xBuf)) ){
                xactLine++;
                switch(xBuf.ToDo) {
                        case Add:
                                addRec(update, xBuf.b, ERRORfile);
                        case Delete:
                                deleteRec(update, xBuf.b, ERRORfile);
                        case ChangeOnhand:
                                changeOnHandRec(update, xBuf.b, ERRORfile);
                        case ChangePrice:
                                changePriceRec(update, xBuf.b, ERRORfile);
                        default:
                                cerr << "Invalaid ToDo on line " << xactLine << endl;
                                ERRORfile << "Invalid ToDo on line " << xactLine << endl;
                                break;
                }


Will not run properly. The ToDo field has correct input but switch will only work for first line. What looks wrong?
I added the breaks and it's still not reading properly past the first line of my xactIn file.
What is xBuf?
What is a String? Is it similar to a std::string?

You can't do binary read/write with classes that manage dynamic allocated data (like std::string or std::vector)
You will be just saving the pointer, and that is useless.
Last edited on
String=
1
2
typedef char String[25];
TransactionRec xBuf;
closed account (zb0S216C)
What does the file your reading from look like?
closed account (zb0S216C)
What does the file your reading from look like?
I got all excited when i read this because i saw the missing break and thought i actually knew this one but then i heard it doesnt fix the problem so i guess it doesnt count :(
You can always implement it with cascaded if-s when desperate.

The ToDo field has correct input but switch will only work for first line.

Man, you must be wrong. How do you debug it?

PS: You could print the value of xBuf.ToDo in each iteration to be sure. (cout << xBuf.ToDo) Debuggers are sometimes buggy.
Last edited on
I've done all that. When I cout it shows me the correct information on the first line and then it shows me numbers that aren't in the file at all.
Sample file:
Add 0123766891 Tom Sawyer Twain, Mark 2 8.50 fiction
Delete 0243578325 Romeo and Juliet Shakespeare, William 6 4.99 drama
Sample file:
Add 0123766891 Tom Sawyer Twain, Mark 2 8.50 fiction
Delete 0243578325 Romeo and Juliet Shakespeare, William 6 4.99 drama
Is this a text file? You read it using unformatted i/o - that is the read(..) method. If indeed the data is in text form, you will need to convert the action strings (Add/Delete..) into the corresponding numbers manually in your code. But that is only if the data is stored as text. Then, you will need to implement the parsing.
it's binary
You've got to be kidding me. :(
What?
The ToDo field has correct input but switch will only work for first line.
What do you mean by "for the first line?" Do you mean that only one iteration of the loop is performed or that only the first action is always opted. I assumed the latter, but if it is the former, then may be an exception takes place.

I mean, to have the proper value and still go to the wrong case means a compiler bug. Stuff happens, but...
It may be something in the addRec function and the file pointer is getting misdirected. Let me experiment with that and I'll get back to ya.
If someone could explain why changing a long to an unsigned int suddenly makes the pointer move I would be greatly appreciated.
Topic archived. No new replies allowed.