Thread: Binary files

  1. #31
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Threads merged.

  2. #32
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Quote Originally Posted by dwks
    ILoveVectors:
    Code:
    file.fread((char*) ch, sizeof( ch ) );
    file is a structure. Also, the first argument is a void pointer, so no cast is required. fread takes three arguments, and is usually used in conjunction with fwrite (otherwise you tend to get gibberish).
    yea thanks, i coudlnt remember about C, i should of just
    paid attention to what going on instead of trying to remember
    back, i was thinking c++.

    for example in c++:
    Code:
    char letter;
    ifstream in("file.txt", ios::binary);
    in.read( (char*)&letter, sizeof(letter) );

  3. #33
    Registered User
    Join Date
    May 2005
    Posts
    207
    Code:
    #include <stdio.h>
    
    //prototypes
    int start_binary (void);
    
    int main (void)
    {
        start_binary ()
    
        return (0);
    }
    
    int start_binary (void)
    {
        int loop = 0;
        char x[10] = "ABCDEFGHIJ"
        char X = ' ';
    
        FILE *fp = fopen ("prime_binary.txt", "wb");
    
        if (!fp)
        {
            printf ("\nERROR! Unable to open!\n\n");
            return (0);
        }
    
        //read file from end to beginning and print to screen
        for (loop = 0; loop < 10; loop++)
        {
            fseek (fp, -1 * loop * sizeof (x[0]), SEEK_END);
            fread (&X, sizeof (x[0]), 1, fp);
            printf ("%c", x);
        }
    
        printf ("\n\n");
        return (0);
    }
    
    /*
    My output:
     JIHGFEDCB
    */
    When I attempt to read from the end of the file, it apparantly fails the initial read. I've tried playing around with the code, but this is the closest I can get.

    How do I read the file properly from end to beginning using fseek/fread in any kind of loop?

    mw

  4. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When I attempt to read from the end of the file, it apparantly fails the initial read.
    Of course it does. Basic math is what's failing you:
    Code:
    for (loop = 0; loop < 10; loop++)
        {
            fseek (fp, -1 * loop * sizeof (x[0]), SEEK_END);
    Anything times zero is zero.


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

  5. #35
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52

    Post

    You could try reading the entire file into a block of sorts then just calling it from end to beginning...

    Example..

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_LINES 10000
    #define MAXSIZE 2048
    
    char buffer[MAXSIZE];
    long posn_block[MAX_LINES];
    long last_line;
    FILE *infp;
    
    void skip_and_read(long);
    void countlines(FILE *);
    
    main(int argc, char** argv)
    {
                    int i;
                    int cur_line;
    
    	if( (infp = fopen(argv[1], "rb")) == NULL)
    		printf("Could not open file %s\n", argv[1]);
    
                     countlines(infp);
                     cur_line = last_line;
                     for(i=0; i<last_line; i++)
                     {
                                    skip_and_read(cur_line);
                                    printf("%s", buffer);
                                    cur_line--;
                     }
                      fclose(infp);
                      return 0;
    }
    
    void skip_and_read(long line)
    {
    	fseek(infp, posn_block[line], SEEK_SET);
    	memset(buffer, '\0', MAXSIZE);
    	fgets(buffer, MAXSIZE, infp);
    }
    void countlines(FILE *fp)
    {
                    int done = FALSE;
                    last_line = 0;
                    while (! done)
                    {
    	                posn_block[last_line] = ftell(fp);
    		
    	                if (fgets(buffer, MAXSIZE, fp) == NULL)
    		                done = TRUE;
    	                else {
    		                last_line++;
    		                if(last_line >= MAX_LINES) {
    			                    last_line = MAX_LINES;
    			                    done = TRUE;
    		                }
    	                }
                      }
                       last_line--;
    }
    That should read a file from end to beginning and output each line to the screen... looking back over it it's a little messy in spots(sorry I'm in a hurry) but you should get the general idea of a way to do it.

    BTW just so no one jumps down my throat I broke it up into functions and used global variables because I figured it would be easier to understand.
    Last edited by Sysop_fb; 08-25-2005 at 02:02 PM.

  6. #36
    Registered User
    Join Date
    May 2005
    Posts
    207
    Code:
        //read file from end to beginning and print to screen
        for (loop = 0; loop < 10; loop++)
        {
            fseek (fp, -1 * (loop + 1) * sizeof (x[0]), SEEK_END);
            fread (&X, sizeof (x[0]), 1, fp);
            printf ("%c", x);
        }
    This fixed it... I had thought the zero calculation would mean "start at the very last position", and sure enough it did. However, the fread function apparantly reads immediately after the pointer's position. Makes sense...

    Once I added 1 to my calculations it repositioned the pointer where I needed it.

    mw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  2. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  3. MFC: CStrings & binary files question(s)
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-24-2004, 05:41 PM
  4. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 01:13 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM