Thread: help withcreating sequential text file

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    72

    help withcreating sequential text file

    hi! i need rewrite this program using sequential text file!
    Code:
     
    #include<stdio.h>
    #include<string.h>
    
    typedef struct 
    {  char last_name[15];
       char first_name[15];
       double grade_point_index;
    }student;
    typedef struct
    {  char last_name[15];
       char first_name[15];
       double salary;
    }employee;
    
    int 
    main(){
    	int num_student;
    	int num_employee;
    	int i,k,j;
    	student arr_student[5],temp;
    	employee  arr_employee[5],temp1;
    	printf("\nEnter the number of students==>  ");
    	scanf("%d", &num_student);
    	for(i=0;i<num_student;i++)
    	{printf("\nEnter the last name of student %d==>",i+1 );
    	scanf("%s",arr_student[i].last_name);
    	printf("\nEnter the first name of student %d==>",i+1 );
    	scanf("%s",arr_student[i].first_name);
    	printf("\nEnter the grade point index of student %d==>",i+1 );
    	scanf("%lf",&arr_student[i].grade_point_index);}
    	
    	
    	for (k = 0; k <num_student; ++k)
    		for (j = k+1; j <num_student; ++j){
    			if (strcmp(arr_student[k].last_name,arr_student	[j].last_name)>0){
    				temp=arr_student[k];
    				arr_student[k]=arr_student[j];
    				arr_student[j]=temp;}
    			else if (strcmp(arr_student[k].last_name,arr_student[j].last_name)==0){
    					if (strcmp(arr_student[k].first_name,arr_student[j].first_name)>0){
    					temp=arr_student[k];
    					arr_student[k]=arr_student[j];
    					arr_student[j]=temp;}}
    		}
    	printf("Students' records \t last name \t first name \t grade point index  \n");
    	for(i=0;i<num_student;i++)
    	printf("                 \t %-8s    \t %-8s      \t %-8.2f \n",arr_student[i].last_name,arr_student[i].first_name,arr_student[i].grade_point_index);  
    
    
    	printf("Enter the number of employees==>  ");
    	scanf("%d", &num_employee);
    	for(i=0;i<num_employee;i++)
    	{printf("\nEnter the last name of employee %d==>",i+1 );
    	scanf("%s",arr_employee[i].last_name);
    	printf("\nEnter the first name of employee %d==>",i+1 );
    	scanf("%s",arr_employee[i].first_name);
    	printf("\nEnter the salary of employee %d==>",i+1 );
    	scanf("%lf",&arr_employee[i].salary);
    	}
    
    	for (k = 0; k <num_employee; ++k)
    		for (j = k+1; j <num_employee	; ++j){
    			if (strcmp(arr_employee	[k].last_name,arr_employee[j].last_name)>0){
    				temp1=arr_employee[k];
    				arr_employee[k]=arr_employee[j];
    				arr_employee[j]=temp1;}
    			else if (strcmp(arr_employee[k].last_name,arr_employee[j].last_name)==0){
    					if (strcmp(arr_employee	[k].first_name,arr_employee	[j].first_name)>0){
    					temp1=arr_employee[k];
    					arr_employee[k]=arr_employee[j];
    					arr_employee[j]=temp1;}}
    		}
    
    	printf("\nThe Employees' records that you have entered are:  \n\n");
    	printf("Employees' records \t last name \t first name \t salary  \n");
    	for(i=0;i<num_employee;i++)
    		printf("                 \t %-8s    \t %-8s      \t %-8.2f \n",arr_employee[i].last_name,arr_employee[i].first_name,arr_employee[i].salary);
    for(i=0;i<num_student;i++)
    	{for(j=0; j<num_employee; j++)
    		if(strcmp(arr_employee[j].last_name,arr_student[i].last_name)==0 && strcmp(arr_employee[j].first_name,arr_student[i].first_name)==0)
    			if(arr_student[i].grade_point_index>3)
    				arr_employee[j].salary= arr_employee[j].salary * 1.10;}
    
    printf("\n\nThe emplyees' salary after the increase: \n \n");
    printf("Employees' records \t last name \t first name \t salary  \n");
    	for(i=0;i<num_employee;i++)
    		printf("                 \t %-8.3s    \t %-8s      \t %-8.3f \n",arr_employee[i].last_name,arr_employee[i].first_name,arr_employee[i].salary);
    				
    	return (0);
    }

    i need put data in an external files (ordered), one for students (called "students.dat"), one for employees (called "employees.dat").
    2- read it in
    3-change the salary of those employees who had a GPA greater than 3.0 as students
    4- write out the changed employee array (the students' array doesn't change) to a file called "employees_updated.dat".

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And the problem is what? You know how to open and read a file, right?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    72
    i'm a bit confused about the part"put data in an external files (ordered), one for students (called "students.dat"), one for employees (called "employees.dat"). and use of sequential text files (i don't see the difference comparing to random access files)

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Random access files must have records that are the same size, to allow you to access any record. Sequential files do not.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    72
    so if i write something like this what is the problem?
    Code:
    #include<stdio.h>
    #include<string.h>
    typedef struct 
    {  char last_name[15];
       char first_name[10];
       double grade_point_index;
    }student;
    typedef struct
    {  char last_name[15];
       char first_name[15];
       double salary;
    }employee;
    
    int 
    main(){
    	FILE *cfPtr;
    	int num_student;
    	int num_employee;
    	int i,k,j;
    	student arr_student[5],temp;
    	employee  arr_employee[5],temp1;
    	
    	if( ( cfPtr = fopen("file.dat", "wb+") ) == NULL )
    		printf("couldn't open the file\a\a\a");
        else{	
    	
    	printf("\nEnter the number of students==>  ");
    	scanf("&#37;d", &num_student);
    	for(i=0;i<num_student;i++)
    	{printf("\nEnter the last name of student %d==>",i+1 );
    	scanf("%s",arr_student[i].last_name);
    	printf("\nEnter the first name of student %d==>",i+1 );
    	scanf("%s",arr_student[i].first_name);
    	printf("\nEnter the grade point index of student %d==>",i+1 );
    	scanf("%lf",&arr_student[i].grade_point_index);}
    	
    	
    	for (k = 0; k <num_student; ++k)
    		for (j = k+1; j <num_student; ++j){
    			if (strcmp(arr_student[k].last_name,arr_student	[j].last_name)>0){
    				temp=arr_student[k];
    				arr_student[k]=arr_student[j];
    				arr_student[j]=temp;}
    			else if (strcmp(arr_student[k].last_name,arr_student[j].last_name)==0){
    					if (strcmp(arr_student[k].first_name,arr_student[j].first_name)>0){
    					temp=arr_student[k];
    					arr_student[k]=arr_student[j];
    					arr_student[j]=temp;}}
    		}
    	printf("Students' records \t last name \t first name \t grade point index  \n");
    	for(i=0;i<num_student;i++)
    	printf("                 \t %-8s    \t %-8s      \t %-8.2f \n",arr_student[i].last_name,arr_student[i].first_name,arr_student[i].grade_point_index);  
        }
        fclose(cfPtr);
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're still prompting to the screen and waiting for input, rather than trying to use the file you opened? I don't know, why do you think there is a problem?

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    72
    well the file created is empty though

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You also write to the screen instead of writing to the file.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    72
    !!! i don't know how to correct it ((fasting reduces cerebral activity)

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i don't know how to correct it
    What is it?

    fasting reduces cerebral activity
    Stop fasting, or if this is due to Ramadan, then consider that professional programmers who are Muslim face the same problem but have to deal with it, and so do you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    72
    the problem ms laserlight is not about fasting its about how to make the program above print the outputs in the file

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, did you bother to read the link I posted in my very first reply? If not, why not? If so, you wouldn't be asking the question.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    how to make the program above print the outputs in the file
    Ah, so that is it, in which case the solution is to use fprintf() with your file handle, instead of just using printf().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    72
    ....one must be a master in programming to get answered here==(

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    72
    aie! it tells errors as i change printf(too few arguments)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

Tags for this Thread