Thread: Need help on fstream...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    10

    Need help on fstream...

    Say I have an array of ojects, one of the members in each object is a class called string that I have made myself, the one member in the string class is a pointer to char (char* str).
    Now I want to write all elements in the array to file, and later read them from the file. I save in biary mode, but since the objects in the array contains objects whith pointers to dynamic memory, I cant use the sizeof operator(?) to know each objects size (where each object starts and stops) in memory, any suggestion on how to solve this?
    Last edited by codewarrior; 10-14-2001 at 03:56 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    #include <string.h>

    int size=strlen(array);

    Hope it helps...
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I think that if you named your class string it might be illegal because that is likely a keyword since there is an STL string class. Call it something else.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    10
    No, it works fine the file is called strang.h/strang.cpp and the class is named String, so that is not the problem. The problem is to find out how many bytes to read/write to/from file, since one member allocates dynamic memory

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    String classes must reallocate themselves and scan input one character at a time validating it. It should reallocate small blocks of memory such as 20 bytes at a time if it's size is breached. See realloc(...);

    String is okay because C++ is case sensitive. Beware of using string or CString.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    10
    This is how Im going to use my classes, and whats the problem is about. Sorry if I was unclear on the subject.
    In main() I create an array of Customer objects whith the new operator, each of the Customer objects contain members of type String object, which in turn has members of type char*.
    Now i want to write and read the array in main to/from file in binary mode, how do I do this?

    strang.h
    class String
    {
    private:
    char* str;
    int len;
    ........more, but not relevant code here
    }

    customer.h
    class Customer
    {
    private:
    int custNo;
    String custName;
    .........more, but not relevant code here
    }

    main_file
    void main()
    {
    Customer* cArray = new Customer[10];
    .........more, but not relevant code here
    }

    Thanks for your kind reply

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    For C++ steams read a chapter in a textbook. Many people will use fread to read binary data. Open the file in "rb" mode.

    FILE *fptr = fopen("filename","rb");
    fread(...); //check compiler help

    You can also use fgets or fgetc depending on they type of input that you are loading. The best way to load structures is to use fread.

    I can't comment on C++ steams. I have not learned them myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM