Thread: need a little help reading binary data in from a file

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    4

    need a little help reading binary data in from a file

    ok this is gonna sound extremely dumb, but i have to ask. I have a binary data file and i want to open it to read it into my program using the fopen() function. Now its been a while since i used C, but i as i remember it the way to do this would be to put the following statement in to my code

    Code:
     
    fp=fopen("c:\test.bin", rb);
    where c:\test.bin is the address of the file i want to open. Yet when i compile my code i get this errors

    error C2065 : 'wb' : undeclared identifier

    error C4047: 'function' : 'const char*' differs in levels of direction from 'int'

    Can anybody give me any ideas what the problem is?

    Thanks in advance

    Andy

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >fp=fopen("c:\test.bin", rb);
    Try:
    fp=fopen("c:\\test.bin", "rb");

    And it's always a good idea to check whether fopen() succeeded.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    yeah, in my actual code i do check to make sure fopen worked thanks for the pointer though. Its been a while since i used C and im just testing ideas at the moment to get back into it, but as a second questions. If i input the following code
    Code:
    	char x[10]="ABCDEFGHIJ";
    	FILE *fp;
    	if((fp=fopen("c:\\test.bin", "wb"))==NULL)
    	{
                           printf("Couldnot open file");
                           exit(1);
                    }
    	fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);
    
    	fcloseall();
    	return(0);
    should the file test.bin contain the binary version of ABCDEFGHIJ or should it show ABCDEFGHIJ if opened in notepad?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by andydavenport
    yeah, in my actual code i do check to make sure fopen worked thanks for the pointer though. Its been a while since i used C and im just testing ideas at the moment to get back into it, but as a second questions. If i input the following code
    Code:
    	char x[10]="ABCDEFGHIJ";
    	FILE *fp;
    	if((fp=fopen("c:\\test.bin", "wb"))==NULL)
    	{
                           printf("Couldnot open file");
                           exit(1);
                    }
    	fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);
    
    	fcloseall();
    	return(0);
    should the file test.bin contain the binary version of ABCDEFGHIJ or should it show ABCDEFGHIJ if opened in notepad?
    I don't know about you but I usually use types in sizeof

    Code:
     
                    int len = 10;
    	char x[len]="ABCDEFGHIJ";
    	FILE *fp;
    	if((fp=fopen("c:\\test.bin", "wb"))==NULL)
    	{
                           printf("Couldnot open file");
                           exit(1);
                    }
    	fwrite(x, sizeof(char), len, fp);
    
    	fcloseall();
    	return(0);

    and yes it will show ABCDEFGHIJ in notepad because you are printing ascii characters.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    cheers

    i tell you blowing the cobebs outta the old brain is getting harder to do

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Using types in sizeof is not required it's just something I prefer to do because it makes more sense when I read it.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    ok, how do i get it to make the out put file into binary data, rather that ascii characters?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Binary how? If you write the letter (character) 'a' in text mode, and all you're writing is that letter, it's going to be the same thing even if you open it in binary mode.The ascii values will still be the same in one mode or the next. As a matter of fact, there is no difference on most unix variants between text and binary modes.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Brian
    I don't know about you but I usually use types in sizeof
    Whenever possible, I use sizeof with an actual object. Then if you ever change the object's type (from say a char to a wchar_t) there will be less code that needs additional changes. [And sizeof(char) is by definition 1.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading binary file with different data types
    By larne in forum C Programming
    Replies: 8
    Last Post: 07-29-2008, 10:12 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM