Thread: Opening a file in binary

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    11

    Opening a file in binary

    I am writing a function to open a binary file. It has two arguments, the first is a struct which the files info will be read and stored to, the second is a C style string containing the name of the file to open. I was given the following code by my instructor to start off with:

    Code:
    
    int read_stuff(Stuff records[], char filename[])
       {
       ifstream infile;
    
       infile.open("filename", ios::in | ios::binary);
       assert(infile);
       infile.read(0, sizeof(records));
    
       infile.close();
       }
    Just to let you know, the function is supposed to return an int; if the file opens, it returns a positive value. If the file does not open correctly, the function returns a negative value. I have not written this part of the code yet, and did not think it relative to my question, but if it is, let me know. Anyway, my question is what do the arguments of infile.read() mean, and how does the read file use them. Thanks for reading. Any help would be appreciative.

  2. #2
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    The prototype of the read() function:
    Code:
    istream &read(char *buf,streamsize num);
    Where *buf is the target variable you want to hold the value read. Much like cin>>x. streamsize is a typedef for unsigned long int (i think) and num is the number of bytes to be read.
    Im not sure why your instructer input 0 as the variable to be read.
    I AM WINNER!!!1!111oneoneomne

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Opening a file in binary

    Originally posted by niudago
    infile.read(0, sizeof(records));
    The first argument is where you will store the data being read. 0 means a NULL poi nter, which seems wrong.
    Also, always check if the open methods fails or not.

    Code:
    typedef struct RECORDS *whatever*
    
    ...
    
    const int NrOfRecords = 12;
    
    RECORDS MyRecords[NrOfRecords];
    
    InFile.open("Filename.sav", ios::in | ios::binary);
    
    if(!InFile.fail())
    {
       InFile.read((char*)MyRecords, sizeof(RECORDS) * NrOfRecords);
       InFile.close();
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM