I/O to binary file

This is a discussion on I/O to binary file within the C++ Programming forums, part of the General Programming Boards category; char name[61]; char director[31]; char cast[156]; short int duration, year, rating, category; This will add up to 256 bytes so ...

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

    I/O to binary file

    char name[61];
    char director[31];
    char cast[156];
    short int duration,
    year,
    rating,
    category;

    This will add up to 256 bytes so the recordset is 256 in the binary
    file.
    when the program starts it opens the binary file it it has to read all the records into a dynamic string.

    ifstream infile("m.dat", ios::in | ios:ut | ios::binary);

    infile.seekg(0, ios::end);
    fsize = infile.tellg();
    infile.seekg(0, ios::beg);
    if(fsize){
    current = new char[fsize];

    my problem is that when i copy the records into the string it will not hold the spaces.
    What i mean is if the first ten spaces in name from binary has "jjjjjjjjjj" and the rest of the 51 spaces are empty.
    i want the string current to hold "jjjjjjjjjj" + 51 spaces;
    How can i do that;

    this is a console app on win. using borland
    #include <iostream.h>
    void main(void){
    char t[7] = "Thanks";

    cout<< t <<endl;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Did you use write() to write the records? Assuming you filled name with 10 characters + 50 spaces using strcpy() or at the declaration, when you read it back using infile.read() it will contain the spaces.

    However there is only room for 50 spaces, not 51, as the string terminator takes one space.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    I'm having problems with files myself, but don't know if you can open an ifstream with ios:ut. Never seen it, anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-17-2005, 11:09 PM
  3. Problem with Binary File I/O
    By DirX in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2004, 08:34 AM
  4. Binary FIle I/O
    By MethodMan in forum C Programming
    Replies: 5
    Last Post: 03-21-2003, 01:22 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21