Thread: Example needed

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    Example needed

    Hi, could anyone provide a small working example on how to write and read binary files. Let say i have 3 arrays , a char *array , int *array1 and unsigned int *array2. How would i store this on disc in binary format and then read it from the disc (i need all 3 arrays to be in a single binary on disc , is this possible ?)


    thank you very much,

    baxy

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by baxy View Post
    Hi, could anyone provide a small working example on how to write and read binary files. Let say i have 3 arrays , a char *array , int *array1 and unsigned int *array2. How would i store this on disc in binary format and then read it from the disc (i need all 3 arrays to be in a single binary on disc , is this possible ?)


    thank you very much,

    baxy
    It sounds like you want to use a struct, which takes disparate datatypes that relate to one object, and puts them together into a single object. Think of a student with an ID, a major, an age, a year in school, and several classes and a GPA. They can all go into one struct, and you can make an array of those structs if you like.

    So you'd wind up with just one file of structs - and there are zillions of examples of writing out binary files. Google one of them.

    If you get stuck, come back and post up your code, and tell us what you're stuck on. We're here to help, but want to see some starting effort - which is forum policy.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    The declarations: char *array, int *array1, and unsigned int *array2 do not specify arrays. They specify pointers.

    arrays are not pointers and pointers are not arrays. I suggest you read comp.lang.c.FAQ. particularly section 6.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Presumably you're talking about dynamically-allocated arrays.
    You can dump anything you want to a binary file.

    Open the file in binary mode.
    Write the data with fwrite.
    Close the file.

    Post your attempt.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    hi, ok here is my attempt but i don't deg why is it not working. so i took Adak's advice and it is writing sonething but i cannot read it ????

    write.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "eprintf.h"
    
    
    struct data	{
    		int *count;
    		long *Info;
    
    }data;
    
    int main(){
    		FILE *fileOut;
    		struct data *myData = emalloc(sizeof(*myData));
    		myData->count = (int *) emalloc(sizeof(int) * 100);
    		myData->Info = (long *) emalloc(sizeof(long) * 100);
    		int i = 0;
    		for (i=0;i<100;i++){
    			myData->count[i] = i+i;
    			myData->Info[i] = i*i;
    		}
    
    		fileOut=fopen("test.bin","wb");
    		if (!fileOut)
    		{
    			printf("Unable to open file!");
    			return 1;
    		}
    		fwrite(&myData, sizeof(struct data), 1, fileOut);
    
    		fclose(fileOut);
    		
    		return 0;
    }
    read.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "eprintf.h"
    
    	/* Our structure */
    struct data	{
    		int *count;
    		long *Info;
    
    }data;
    
    
    int main(){
    		FILE *fileIn;
    		struct data *myData = emalloc(sizeof(*myData));
            int i;
            myData->count = (int *) emalloc(sizeof(int) * 100);
    		myData->Info = (long *) emalloc(sizeof(long) * 100);
    		fileIn=fopen("test.bin","rb");
    		if (!fileIn)
    		{
    			printf("Unable to open file!");
    			return 1;
    		}
            printf("ggg\n");
    		fread(&myData,sizeof(struct data),1,fileIn);
    		        printf("ggg\n");
    	    for (i=0;i<100;i++){
    	      printf("%d..%ld\n",myData->count[i],myData->Info[i] );
    	    }
    
    		fclose(fileIn);
    		return 0;
    	}

    so it is writing but when it comes to reading i get: Segmentation fault (core dumped)

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You don't need a struct.

    Your problem is that all you're writing to the file is the two pointers in the struct, and no actual data at all. The values of the pointers are useless when read back in.

    Here's an example of reading and writing an integer array to a binary file.
    Code:
    /* binwrite.c */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    void die(const char *msg) {
        perror(msg);
        exit(1);
    }
    
    int main(void) {
        int a[] = {1, 2, 3, 4, 5};
        size_t i;
        FILE *fp;
    
        fp = fopen("data.bin", "wb");
        if (!fp) die("Can't open data.bin for writing");
    
        for (i = 0; i < sizeof a/sizeof*a; i++)
            fwrite(&a[i], sizeof*a, 1, fp);
    
        fclose(fp);
    
        return 0;
    }

    Code:
    /* binread.c */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    void die(const char *msg) {
        perror(msg);
        exit(1);
    }
    
    int main(void) {
        int a[5];
        size_t i, n = 0;
        FILE *fp;
    
        fp = fopen("data.bin", "rb");
        if (!fp) die("Can't open data.bin for reading");
    
        while (fread(&a[n], sizeof*a, 1, fp) == 1)
            n++;
    
        fclose(fp);
    
        for (i = 0; i < n; i++)
            printf("%d\n", a[i]);
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    aha...

    thanks a million

    PS

    but still if i had 3 arrays then every array would need to be in a single file or ... ???
    Last edited by baxy; 10-11-2012 at 09:54 AM.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by baxy View Post
    but still if i had 3 arrays then every array would need to be in a single file or ... ???
    That's up to you. Write them all to the same file, one after the other (and read them back in the same order), or write them to three separate files. Give it a try.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    Thnx everything is working, i learned something new today

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    Hi

    well more problems. So now i want to increase the reading and writing buffer. I don't want it to write one byte at the time or read one byte at the time. so i did this when writting:

    Code:
    FILE *fileOut;
    fileOut=fopen("test.bin","wb");
    int tmp[100];
    int i;
    
      if (!fileOut){
    	printf("Unable to open file!");
      }
      
      for(i=0;i<10000;i++){
        
        if (i%100 == 0){
          fwrite(&tmp, sizeof(Int64), 100, fileOut);
        }
        tmp[i%100] = i;
          
      }
    
      fclose(fileOut);
    but when i read it i get all the zerros

    ??

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    When you say "one byte at a time" you actually mean "one integer at a time" since an integer is more than one byte (usually 4 or 8).

    I see three problems in the code you've posted.

    1. You use sizeof(Int64) whereas you should use sizeof(int) (or even better sizeof(tmp[0]), which will be correct even if you change the type of tmp).

    2. i % 100 will be zero when i is 0, so fwrite will be executed once before tmp has been filled.

    3. i % 100 will NOT be zero when i is it's last value (9999) and so the last 100 values won't be output.

    EDIT: One way to fix it:
    Code:
        for (i = 0; i < 10000; i++) {
            tmp[i % 100] = i;
            if ((i + 1) % 100 == 0) {
                fwrite(&tmp, sizeof(tmp[0]), 100, fileOut);
            }
        }
    Last edited by oogabooga; 10-11-2012 at 01:14 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    THANK YOU ; it is working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. little help here needed.
    By santarus in forum C Programming
    Replies: 5
    Last Post: 11-22-2011, 07:04 AM
  2. Help needed!
    By cherek in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2006, 03:34 AM
  3. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  4. Some Help Needed!
    By ob1cnobe in forum C++ Programming
    Replies: 7
    Last Post: 05-29-2002, 03:08 PM
  5. Help Needed!!!
    By colw in forum C Programming
    Replies: 2
    Last Post: 03-17-2002, 12:02 PM