What is wrong?

This is a discussion on What is wrong? within the C Programming forums, part of the General Programming Boards category; Hi, I have 2 files: student.dat {ID, Telephone,Name,Facalty} and facalty.dat {Code, Facalty} My program to do is: 1 - Enter ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    What is wrong?

    Hi,

    I have 2 files:
    student.dat {ID, Telephone,Name,Facalty}
    and facalty.dat {Code, Facalty}

    My program to do is:

    1 - Enter a new record in student.dat file
    [when entering Facalty name, I have to seek for this info from facalty.dat file, so the user first should enter the code inorder to pick the correct facalty name]
    2 - Edit a student record
    3 - Enter a new facalty/ facalty.dat
    4 - List all facalties
    5 - List all students
    6 - exit

    This what I have done, and nothing works correctly:

    Code:
    #include <stdio.h>
    
    
    struct facalty{
    	int code;
    	char fac[30];
    };
    
    struct student{
    	int id,tel;
    	char name[30];
    	char facl[30];
    };
    
    /*prototypes*/
    
    int choose();
    void enterStudent( FILE *fptr2,FILE *fptr);
    void editStudent( FILE *fptr2);//2
    void newFacalty( FILE *fptr);
    void listFacalties( FILE *fptr);
    void listStudents( FILE *fptr2);//2
    
    
    int main()
    {
    	FILE *ffac;
    	FILE *fstud;
    	int ch;
    	ffac=fopen("facalty.dat","rb+");//rb+
    	if(ffac==NULL)
    		printf("File facalty cannot be created:\n");
    	fstud=fopen("student.dat","rb+");//rb+
    	if(fstud==NULL)
    		printf("File student cannot be created:\n");
    	while((ch = choose())!=7)
    	{
    		switch(ch)
    		{
    		case 1:
    			enterStudent( fstud, ffac);
    			break;
    
    		case 2:
    			editStudent( fstud );
    			break;
    
    		case 3:
    			newFacalty( ffac );
    			break;
    
    		case 4:
    			listFacalties( ffac );
    			break;
    
    		case 5:
    			listStudents( fstud );
    			break;
    
    		case 6:
    			//exit(1);
    			break;
    
    		default:
    			printf("Wrong choice!");
    			break;
    
    		}
    	}
    	fclose(ffac);
    	fclose(fstud);
    	
    	return 0;
    }
    
    int choose()
    {
    	int tmp;
    	printf("What to do:\n"
    		"1 - Enter a new record\n"
    		"2 - Edit a student record\n"
    		"3 - Enter a new facalty\n"
    		"4 - List all facalties\n"
    		"5 - List all students\n"
    		"6 - exit\n");
    	
    	scanf("%d",&tmp);
    	return tmp;
    }
    void enterStudent( FILE *fptr2,FILE *fptr)
    {
    	int tmprCode;
    	//char found[30];
    	struct student st = {0,0,"",""};
    	struct facalty ft = {0,""};
    	printf("Enter code for facalty");
    	scanf("%d",&tmprCode);
    	if(tmprCode==0) 
    		printf("The code dose not exist (0):");
    	if (fseek(fptr,(tmprCode-1)*sizeof(struct facalty),SEEK_SET)!=-1){
    		fread(&ft,sizeof(struct facalty),1,fptr);
    		//st.facl=ft.fac;
    		printf("%s for STUDENT\n",st.facl);
    		printf("%s for FACALTY\n",ft.fac);
    		printf("Enter ID,telephone,name\n");
    		scanf("%d %d %s",&st.id,&st.tel,st.name);
    		fseek(fptr2,(tmprCode-1)*sizeof(struct student),SEEK_SET);//changed 2/student
    		//fread(&st,sizeof(struct student),1,fptr2);
    		printf("%d\t%d\t%s\t%s\n",st.id,st.tel,st.name,st.facl);
    		fwrite(&st,sizeof(struct student),1,fptr2);
    		printf("Finish writing!");}
    	else{
    		printf("The code is not in the list\n\n");
    	}
    }
    
    void editStudent( FILE *fptr2)//2
    {
    	int st_Id;
    	struct student st_re = {0,0,"",""};
    	printf("Enter the ID of the student to ubdate the record\n");
    	scanf("%d",&st_Id);
    	//rewind(fptr2);
    	//if(fseek(fptr2,(st_Id-1)*sizeof(struct student),SEEK_SET)==-1)
    	//	printf("The ID is not exists:\n");
    	//st_re.id=st_Id;
    	fseek(fptr2,(st_Id-1)*sizeof(struct student),SEEK_SET);
    	fread(&st_re,sizeof(struct student),1,fptr2);
    	printf("\nThe record Before SCAN: %d\t%d\t%s\t%s\t\n",st_re.id,st_re.tel,st_re.name,st_re.facl); 
    	if(st_re.id==0)
    		printf("No information in the record\n");
    	else
    	{
    		printf("Enter the new Telephone, Name, Facalty");
    		scanf("%d %s %s",&st_re.id, st_re.name, st_re.facl);
    		//fseek(fptr2,(st_Id-1)*sizeof(struct student),SEEK_SET);
    		//fread(&st_re,sizeof(struct student),1,fptr2);
    		fseek(fptr2,(st_Id-1)*sizeof(struct student),SEEK_SET);
    		printf("\nThe record to write is: %d\t%d\t%s\t%s\t",st_re.id,st_re.tel,st_re.name,st_re.facl); 
    		fwrite(&st_Id,sizeof(struct student),1,fptr2);
    		printf("\nThe record to write is: %d\t%d\t%s\t%s\t",st_re.id,st_re.tel,st_re.name,st_re.facl); 
    	}
    }
    
    void newFacalty( FILE *fptr)
    {
    	struct facalty fac_re = {0," "}; 
    	int tmpCode;
    	printf("Enter code for the new record:\n");
    	scanf("%d",&tmpCode);
    	fseek(fptr,(tmpCode-1)*sizeof(struct facalty),SEEK_SET);
    	fread(&fac_re,sizeof(struct facalty),1,fptr);
    	if(fac_re.code!=0)
    		printf("The record contains informatioin\n");
    	else
    	{
    		printf("Enter facalty name\n");
    		scanf("%s",fac_re.fac);
    		fac_re.code=tmpCode;
    		fseek(fptr,(fac_re.code-1)*sizeof(struct facalty),SEEK_SET);
    		fwrite(&fac_re,sizeof(struct facalty),1,fptr);
    	}
    }
    
    void listFacalties( FILE *fptr)
    {
    	
    	struct facalty fac_re = {0," "}; 
    	printf("Code\tFacalty name\n");
    	rewind(fptr);
    	while(fread(&fac_re,sizeof(struct facalty),1,fptr))
    	{
    		if(fac_re.code!=0)
    			printf("%d\t%s\n",fac_re.code, fac_re.fac);
    	}
    	
    }
    
    
    void listStudents( FILE *fptr2)//2
    {
    	struct student st_re = {0,0,"",""}; 
    	printf("ID\tTelephone\tName\tFacalty\n");
    	rewind(fptr2);
    	while(fread(&st_re,sizeof(struct student),1,fptr2))
    	{
    		if(st_re.id!=0)
    			printf("%d\t%d\t\t%s\t%s\n",st_re.id,st_re.tel,st_re.name,st_re.facl);
    	}
    }
    Last edited by Moony; 06-29-2006 at 05:04 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    if (fseek(fptr,(tmprCode-1)*sizeof(struct facalty),SEEK_SET)!=NULL){
    fseek() returns -1 on error, not NULL.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Code:
    void enterStudent( FILE *fptr2,FILE *fptr)
    {
    	int tmprCode;
    	char found[30];
    	struct student st = {0,0,"",""};
    	struct facalty ft = {0,""};
    	printf("Enter code for facalty");
    	scanf("%d",&tmprCode);
    	if(tmprCode==0) 
    		printf("The code dose not exist (0):");
    	if (fseek(fptr,(tmprCode-1)*sizeof(struct facalty),SEEK_SET)!=-1){
    		fread(&ft,sizeof(struct facalty),1,fptr);
    		st.facl=ft.fac;
    		printf("%s for STUDENT\n",st.facl);/*debug*/
    		printf("%s for FACALTY\n",ft.fac);/*debug*/
    		printf("Enter ID,telephone,name\n");
    		scanf("%d %d %s",&st.id,&st.tel,st.name);
    		fseek(fptr,(tmprCode-1)*sizeof(struct facalty),SEEK_SET);
    		//fread(&st,sizeof(struct student),1,fptr2);
    		fwrite(&st,sizeof(struct student),1,fptr2);}
    	else{
    		printf("The code is not in the list\n\n");
    	}
    }
    Code:
    st.facl=ft.fac;
    I recieve this error:
    error C2106: '=' : left operand must be l-value

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    I have updated some issues in the code
    Now, I have only 2 problems:
    The first one is mentioned in my previous post
    The second one is in
    Code:
    void editStudent( FILE *fptr2)
    function

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    What exactly is the second problem you're having? Compile error? Runtime error? Garbage values?

    The problem with st.facl = ft.fac is that st.facl is an array. If you want to copy a string use strcpy()
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Example:
    student.dat:
    ID Telephone Name Facalty
    123 666789 JA IT

    I want to edit this information using the function, so:
    First, I will enter the ID to edit it:

    Enter the ID of the student to update the record
    123

    The record Before SCANF: 123 -858993460 ▄■↕ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╣♂/*debug to see if the function works*/
    Enter the new Telephone, Name, Facalty
    3345677 abc Eng

    When I use liststudents() function I find that this:
    ID Telephone Name Facalty
    123 666789 JA IT
    123 -858993460 ▄■↕ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠▄■↕
    So that creats new record, and even don't work
    New C learner

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    		fwrite(&st_Id,sizeof(struct student),1,fptr2);
    You're only writing the Id (an int) instead of the whole record.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    You are right!
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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