Thread: Opening a file in binary

  1. #1
    Registered User
    Join Date
    Feb 2003

    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:

    int read_stuff(Stuff records[], char filename[])
       ifstream infile;
  "filename", ios::in | ios::binary);
       assert(infile);, sizeof(records));
    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 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
    The prototype of the read() function:
    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

    Re: Opening a file in binary

    Originally posted by niudago, 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.

    typedef struct RECORDS *whatever*
    const int NrOfRecords = 12;
    RECORDS MyRecords[NrOfRecords];"Filename.sav", ios::in | ios::binary);
    {*)MyRecords, sizeof(RECORDS) * NrOfRecords);

    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