Thread: Help compiling random access file

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    Help compiling random access file

    well what I want to do is create 9 accounts then add data to them and finally display and save the result but I am stuck with com pile errors.

    I am using gcc to compile; like this

    gcc nameofprogram.c

    is this ok?

    it keeps saying cfptr is never declared. and I don't know what to do.

    here is my code.

    Code:
    #include <stdio.h>
    
    
    int main (void ){
    
    
    struct clientdata{
    	int acct;
    	char name[20];
    	char lastn[20];
    	int age;
    	char sex[2];	
    	float	height;	
    	float weight;
    	int membership;
    	};//end struct
    
    
    
      int i;
      int j=101;
     
      struct clientdata client={0, "", "", 0, "", 0.0, 0.0, 0};
      
      FILE *cfptr;
    
    
      if((cfprt=fopen( "customers.dat", "wb+" ))== NULL) {
      printf( "file could not be opened\n");}
    
      else{ 
    
      for(i=101; i <= 109;i++){
      fwrite(&client, sizeof( struct clientdata), 1, cfptr);
      }
    
    
      printf("Enter acct number"
    	"(101 to 105, 0 to end input\n");
    
      scanf("%d", &client.acct);
    
      while(client.acct != 0){
    
    	printf("Enter name, last name, age, sex\n");
    	printf("height, weight, and membership years\n");
    
    	fscanf( stdin, "%s%S%d%s%f%f%d", client.name, client.lastn,
    	&client.age, client.sex, &client.height, &client.weight, &client.membership);
    
    	fseek( cfptr, (client.acct - 1) * 
    	sizeof( struct clientdata), SEEK_SET);
    
    	fwrite(&client, sizeof( struct clientdata), 1, cfptr);
    
    	printf("Enter acct number\n");
    	scanf("%d", &client.acct);
    
      }  // end while
    
    
      printf("%-5s%-20S%-20s%-4s%-5s%-5s%-5s%-11s\n", "Acct", "Name",
      "last name", "Age", "sex", "height", "weight", "membership");
    
      
      while(j<=109){
    	fread(&client, sizeof(struct clientdata), 1, cfptr);
    	if(client.acct !=0){
    	  printf("%-5d%-20S%-20s%-4d%-5s%-5f%-5f%-11d\n",
    	  client.acct, client.name, client.lastn,
    	  client.age, client.sex, client.height, client.weight, client.membership);
    	} //end if
      } //end 2nd.while
    
    
      fclose(cfptr);
    
    } //end else
    
      return 0;
    
    } //ends main

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Check your spelling.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    LoL, my eyes hurt by now thanks I was going crazy.

    second question so will I be able to update everytime with out a problem? becuase the second part is to delete an account, and modify one. I new to file processing and I pick (wb+)

    ok so my code stop after I enter the account I made some changes to the code but is still doesn't work.

    Code:
     while(!feof(cfptr)){
    	fread(&client, sizeof(struct clientdata), 1, cfptr);
    	if(client.acct !=0){
    	  printf("%-5d%-20S%-20s%-4d%-5s%-5f%-5f%-11d\n",
    	  client.acct, client.name, client.lastn,
    	  client.age, client.sex, client.height, client.weight, client.membership);
    	} //end if
      } //end 2nd.while
    
    
      fclose(cfptr);
    Last edited by newbc; 04-07-2011 at 10:36 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look very closely....
    Code:
     FILE *cfptr;
    
      if((cfprt=
    See anything wrong?

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    gcc nameofprogram.c

    Recommended is

    Code:
    gcc -Wall -ansi -pedantic prog.c
    If you don't specify -ansi, gcc will assume gcc C. and you'll have many non-standard things compile cleanly. like nested function, void pointer arithmetic. etc,

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When I compile your code I get these messages:
    main.c||In function ‘main’:|
    main.c|49|warning: ISO C does not support the ‘%S’ gnu_scanf format|
    main.c|49|warning: format ‘%S’ expects type ‘wchar_t *’, but argument 4 has type ‘char *’|
    main.c|63|warning: ISO C does not support the ‘%S’ gnu_printf format|
    main.c|63|warning: format ‘%-20S’ expects type ‘wchar_t *’, but argument 3 has type ‘char *’|
    main.c|71|warning: ISO C does not support the ‘%S’ gnu_printf format|
    main.c|71|warning: format ‘%-20S’ expects type ‘wchar_t *’, but argument 3 has type ‘char *’|
    ||=== Build finished: 0 errors, 6 warnings ===|
    I also suspect the fseek line will cause problems.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. question on random file access
    By ygfperson in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2002, 05:21 PM