![]() |
| | #1 |
| Registered User Join Date: May 2008
Posts: 70
| help withcreating 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". |
| overlord21 is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| And the problem is what? You know how to open and read a file, right? |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: May 2008
Posts: 70
| 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) |
| overlord21 is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Random access files must have records that are the same size, to allow you to access any record. Sequential files do not. |
| tabstop is offline | |
| | #5 |
| Registered User Join Date: May 2008
Posts: 70
| 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("%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);
}
|
| overlord21 is offline | |
| | #6 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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? |
| tabstop is offline | |
| | #7 |
| Registered User Join Date: May 2008
Posts: 70
| well the file created is empty though |
| overlord21 is offline | |
| | #8 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You also write to the screen instead of writing to the file. |
| tabstop is offline | |
| | #9 |
| Registered User Join Date: May 2008
Posts: 70
| !!! i don't know how to correct it ((fasting reduces cerebral activity) |
| overlord21 is offline | |
| | #10 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
| | #11 |
| Registered User Join Date: May 2008
Posts: 70
| the problem ms laserlight is not about fasting its about how to make the program above print the outputs in the file |
| overlord21 is offline | |
| | #12 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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. |
| tabstop is offline | |
| | #13 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #14 |
| Registered User Join Date: May 2008
Posts: 70
| ....one must be a master in programming to get answered here==( |
| overlord21 is offline | |
| | #15 |
| Registered User Join Date: May 2008
Posts: 70
| aie! it tells errors as i change printf(too few arguments) |
| overlord21 is offline | |
![]() |
| Tags |
| files, program impleùmentation, random, sequential |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can we have vector of vector? | ketu1 | C++ Programming | 24 | 01-03-2008 05:02 AM |
| struct question | caduardo21 | Windows Programming | 5 | 01-31-2005 04:49 PM |
| Simple File encryption | caroundw5h | C Programming | 2 | 10-13-2004 10:51 PM |
| what does this mean to you? | pkananen | C++ Programming | 8 | 02-04-2002 03:58 PM |
| Outputting String arrays in windows | Xterria | Game Programming | 11 | 11-13-2001 07:35 PM |