fread() condition problem

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1184 Views - Last Post: 13 May 2013 - 05:45 AM Rate Topic: -----

#31 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,301
  • Joined: 25-December 09

Re: fread() condition problem

Posted 08 May 2013 - 07:22 AM

Do you see all those warnings? They need to be fixed! That is one of the sources of your problem

Here is a small program that illustrates reading your text file, writing the binary file, printing the contents of your binary file and searching for a particular record in your binary file.

#include <stdio.h>
#include <stdlib.h>

//declare functions
void printCustomers(FILE*);
void searchForCustomer(FILE* cfile, int searchId);

//declare structures
struct customer_rec
{
   int id;
   char first_name[21];
   char surname[21];
   int age;
   char gender;
   char address[51];
};

int main()
{
   //declaring variables
   FILE *fpcust,*fpcustbin;
   struct customer_rec cust;;

   //opening the customers files
   fpcust = fopen("customers.txt","r");
   fpcustbin = fopen("customers.bin","w+b");

   while((fscanf(fpcust,"%d%s%s%d %c %[^\n]",&cust.id,cust.first_name,cust.surname,&cust.age,&cust.gender,cust.address))!=EOF)
   {
      int test;
      //all 1's
      printf("%d %s %s %d %c %s\n", cust.id, cust.first_name, cust.surname, cust.age, cust.gender, cust.address);
      test = fwrite(&cust, sizeof(struct customer_rec),1, fpcustbin);
      printf("Test: %d\n\n", test);

   }
   printCustomers(fpcustbin);
   searchForCustomer(fpcustbin, 4568);
   fclose(fpcust);
   fclose(fpcustbin);
   return(0);
}

void printCustomers(FILE* cfile)
{
   struct customer_rec customer;
   rewind(cfile); // Put the file pointer back at the beginning of the file.

   while(fread(&customer, sizeof(struct customer_rec),1,cfile) == 1)
   {
      printf("\nID: %d\n",customer.id);
      printf("Name: %s %s\n",customer.first_name,customer.surname);
      printf("Age: %d\n",customer.age);
      printf("Gender: %c\n",customer.gender);
      printf("Address: %s",customer.address);
      printf("\n");
   }

}

void searchForCustomer(FILE* cfile, int searchId)
{
   struct customer_rec customer;
   rewind(cfile);
   rewind(cfile); // Put the file pointer back at the beginning of the file.

   while(fread(&customer, sizeof(struct customer_rec),1,cfile) == 1)
   {
      if(customer.id == searchId)
      {
         puts("\n\nFound:");
         printf("\nID: %d\n",customer.id);
         printf("Name: %s %s\n",customer.first_name,customer.surname);
         printf("Age: %d\n",customer.age);
         printf("Gender: %c\n",customer.gender);
         printf("Address: %s",customer.address);
         printf("\n");
         break;
      }
   }


}



Notice the difference in the while loop:
while((fscanf(fpcust,"%d%s%s%d %c %[^\n]",&cust.id,cust.first_name,cust.surname,&cust.age,&cust.gender,cust.address))!=EOF)

By using a space instead of the '\n' I ignore all whitespace characters, not just the new line character.


Here is a copy of the text input file I used.

4567
Mike
Green
52
m
grafton street 5
4568
Svetlana
Reed
41
f
baker street 24
4569
Pat
Farrell
60
f
2156 Big Easy Street


This is the output I got when I ran the above program:

Quote

567 Mike Green 52 m grafton street 5
Test: 1

4568 Svetlana Reed 41 f baker street 24
Test: 1

4569 Pat Farrell 60 f 2156 Big Easy Street
Test: 1


ID: 4567
Name: Mike Green
Age: 52
Gender: m
Address: grafton street 5

ID: 4568
Name: Svetlana Reed
Age: 41
Gender: f
Address: baker street 24

ID: 4569
Name: Pat Farrell
Age: 60
Gender: f
Address: 2156 Big Easy Street


Found:

ID: 4568
Name: Svetlana Reed
Age: 41
Gender: f
Address: baker street 24


Jim

This post has been edited by jimblumberg: 08 May 2013 - 07:27 AM

Was This Post Helpful? 1
  • +
  • -

#32 Briscoooe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-May 13

Re: fread() condition problem

Posted 13 May 2013 - 04:35 AM

while((fscanf(fpcust,"%d%s%s%d %c %[^\n]",&cust.id,cust.first_name,cust.surname,&cust.age,&cust.gender,cust.address))!=EOF)


Can you explain this line please? I understand the first 5 delimiters. However I am confused by the %[^\n]. What does that do? Why is not not %s?
Was This Post Helpful? 0
  • +
  • -

#33 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,301
  • Joined: 25-December 09

Re: fread() condition problem

Posted 13 May 2013 - 05:45 AM

Quote

However I am confused by the %[^\n]. What does that do?

It retrieves characters from the stream placing them in the corresponding string variable until it encounters one of the matching characters, in this case the new line character.

Quote

Why is not not %s?

Because the "%s" specifier stops when it encounters a space character. the "%[^\n]" only stops when it encounters a character that matches one of the characters that are between the brackets.

Also you should be specifying the width modifier for those string variables.
while((fscanf(fpcust,"%d%30s%30s%d %c %50[^\n]",&cust.id,cust.first_name,cust.surname,&cust.age,&cust.gender,cust.address))!=EOF)


Jim
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3