How can I restrict fread()s data?

This is a discussion on How can I restrict fread()s data? within the C Programming forums, part of the General Programming Boards category; Iwe been looking up fread() man page and it seems like I can put restrictions in the argument. I havent ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    How can I restrict fread()s data?

    Iwe been looking up fread() man page and it seems like I can put restrictions in the argument.
    I havent found out exactly how that should be done however, in a proper syntax.

    At the moment Im using this statement to read 4 blocks of 4 bytes each to the array: file.

    Code:
    fread(file, 4, 4, fp);
    Is it possible to put a restriction before fp to only read binary data and no text? And what effect would it have to put the restriction before file?
    Last edited by Subsonics; 01-09-2009 at 06:59 PM.

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    fread() will read what you give it. If you only want it to read binary data, be sure to open the file to be read in a binary mode.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by carrotcake1029 View Post
    fread() will read what you give it. If you only want it to read binary data, be sure to open the file to be read in a binary mode.
    Do you mean with the "b" option in fopen?

    Im reading in a fileheader which first 12 bytes is: COMM0000AIFF

    Now, Im only interested in the value between comm and aiff. In the man page for fread()
    it says:
    Code:
    fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
    So what is the *restrict parameter that is mentioned here?

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    I'm not sure what the restrict thing is, but to my knowledge there is no way to just ignore some data. Its all binary when it really comes down to it. Just read it in and you will have to filter it yourself.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The restrict means that ptr and stream can't point to the same object.

    So read in four bytes, throw it away, read in four more bytes and that's what you want.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hmm, ok. So far I have been reading into an array. But the ideal would be to get the data into some specific struct members. I have tried to make an array with these members but then I need to some how update the file pointer. Is that possible to do? Like adding 4 to fp to make it jump 4 bytes into the file?

  7. #7
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    fseek()

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Perfect! Thank you guys. So I guess that it would now be possible to add an offset between each struct member in my member array, and then update the index between the fseek and fread function.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Regarding the syntax of fseek(). I have come up with a starting point for what Im trying to do, would this approach work, in your oppinion?


    Code:
            // members[] contain {offset, struct.member, datasize}
    
    	int members[] = {4, form.ckDataSize, 4, 4, comm.ckDataSize, 4};
    	int i = 5;

    Code:
    	
            // Reading the data to the appropriate struct members.
    
            while(some loop condition) {
    		fseek(fp, members[i], SEEK_CUR);
    		i++;
    		fread(members[i], 1, members[(i +1)], fp);
    		i += 2;
    	}
    Last edited by Subsonics; 01-09-2009 at 09:51 PM.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So I have tested above code and get this error message:

    passing argument 1 of ‘fread’ makes pointer from integer without a cast.

    I have stripped it down to only the fread() statement, and it seems that I cant read into an int,
    it will allow me to read into a string however I want the numeric value at offset 4 of the file. So what are my options here?

    Here is the current simplified code:
    Code:
    	if(fp){
    			fseek(fp, 4, SEEK_CUR);
    			fread(form.ckDataSize, 1, 4, fp);
    	       } else {
                            perror("");
                   {
    So as noted, form.ckDataSize is an int.
    Last edited by Subsonics; 01-10-2009 at 12:47 AM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Fread expects a pointer!
    If you don't understand what that means, then you need re-schooling in pointers and why they are necessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Elysia View Post
    Fread expects a pointer!
    If you don't understand what that means, then you need re-schooling in pointers and why they are necessary.
    Hey, thanks. I know what a pointer is, but I have been using c for about a month now.

    So I guess a correct statement would be:

    Code:
    fread(&form.ckDataSize, 1, 4, fp);
    Cheers
    Last edited by Subsonics; 01-10-2009 at 03:47 AM.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    So did that mean "Oh, I see, I get it, I know what I have to do now" or "Hey, don't yell at me, I'm a newbie!"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Elysia View Post
    So did that mean "Oh, I see, I get it, I know what I have to do now" or "Hey, don't yell at me, I'm a newbie!"?
    He he, I get it now, I think. But I have a legitime reson to be a bit lost, it can get confusing.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Yes, it can be confusing... And your edit is correct, too!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM

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