File creation Problem, Txt-Binary

This is a discussion on File creation Problem, Txt-Binary within the C Programming forums, part of the General Programming Boards category; Hi, just starting to work with creating files, reading and writing to them. I want to create a file that ...

  1. #1
    DirtyHarry
    Guest

    File creation Problem, Txt-Binary

    Hi, just starting to work with creating files, reading and writing to them. I want to create a file that will list some customer details. For some reason though, the file appears to be written in binary. This is of no use to me as I want to be able to print the file as a record of customers. Any suggestions. I'll append somne code below.

    Thanks a lot,
    Hugh

    void add_customer()
    {
    struct customer new_cust={0,"",0,0};

    if((cfPtr=fopen("cust_file.txt","r+"))==NULL)
    {
    printf("File could not be opened.\n");
    }
    else
    {
    printf("\n\nEnter account number. 1 to 100\n\n?");
    scanf("%d",&new_cust.acct_num);

    printf("\n\nEnter Customer Name\n\n?");
    fscanf(stdin,"%s",new_cust.cust_name);

    fseek(cfPtr,(new_cust.acct_num-1)*sizeof(struct customer), SEEK_SET);

    fwrite(&new_cust, sizeof(struct customer),1,cfPtr);

    system ("cls");
    }
    fclose(cfPtr);
    }

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Use code-tags, when you post a code here in the board

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    What do you mean with the file is written in binary?

    A possible solution. Assuming the record is represented by a structure S of size sizeof(S), then you could read sizeof(S) bytes and map those bytes onto the structure. And then print each member of the struct in the way you want to present it to the user.

    Also the fclose() is in the wrong place in your function. Now you also call fclose() when the file is not opened.

  4. #4
    DirtyHarry
    Guest

    Question Don't quite understand???

    Hello Shiro, thankyou for your reply. I was wondering if you had time to elaborate.

    In regard to binary, this is how the file appears when I open it in notepad. Visual studio will not open the file and warns me that the file is a binary file?. As I say, below is how it appears.

     Healy +  Onifri d  Fallon    Kettle  W  Thomas c  Dunne +  Raven


    struct customer
    {
    int acct_num;
    char cust_name[20];
    int barcode_start;
    int barcode_end;

    };

    Above is the structure. Also you mentioned that fclose was in the wrong place, where would you suggest it should be? Could you explain?
    Thank you,
    H

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I'm not sure, but I think the write position of the fclose() should be after you write the data with fwrite(), after this:
    fwrite(&new_cust, sizeof(struct customer),1,cfPtr);

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The function fwrite() is for writing, the function fread() can be used for reading. Calculate the size of your struct customer, assume this size is N bytes. Then read in N bytes into a buffer of N bytes, for example a char array of N elements. Finally use pointer conversion or copy the bytes into the appropriate structure members.

    Pointer conversion:

    Code:
    char binary_data [N];
    struct customer *customer_data;
    
    // read from file
    fread (binary_data, sizeof (char), N, cfPtr);
    
    // map data onto structure
    customer_data = (struct customer *) data;
    About the fclose():

    Code:
    if((cfPtr=fopen("cust_file.txt","r+"))==NULL)
    {
      printf("File could not be opened.\n");
    }
    else
    {
      printf("\n\nEnter account number. 1 to 100\n\n?");
      scanf("%d",&new_cust.acct_num);
    
      printf("\n\nEnter Customer Name\n\n?");
      fscanf(stdin,"%s",new_cust.cust_name);
    
      fseek(cfPtr,(new_cust.acct_num-1)*sizeof(struct customer),   
        SEEK_SET);
    
      fwrite(&new_cust, sizeof(struct customer),1,cfPtr);
    
      // You are now done with the file, so close it here.
      fclose (cfPtr);
    
      system ("cls");  
    }
    
    // Not that if you get here, fopen() may have failed. If fopen()
    // returns NULL, then after the if-branch, you get here and try
    // to close a not opened file.
    fclose(cfPtr);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Print binary numbers to disk file, problem
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 10-04-2004, 07:33 AM

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