![]() |
| | #16 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
You probably forgot the FILE argument when using fprintf(), but I lack the mind reading skills to be sure in the absence of the code.
__________________ 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 offline | |
| | #17 |
| Registered User Join Date: May 2008
Posts: 70
| where should i insert it |
| overlord21 is offline | |
| | #18 |
| Registered User Join Date: May 2008
Posts: 70
| exuse for lack of understanding |
| overlord21 is offline | |
| | #19 | ||
| 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 offline | |
| | #20 |
| Registered User Join Date: May 2008
Posts: 70
| i'll just let it for now it just that the instructor did only the lectur with no exemple of an implementation using fprintf and stuff. I'll make my mind later |
| overlord21 is offline | |
| | #21 |
| Registered User Join Date: Aug 2008
Posts: 67
| |
| kpreston is offline | |
| | #22 |
| Guest Join Date: Aug 2001
Posts: 4,923
| The bottom line is you need to have all of the relevent API documentation handy before you start coding. And a little initiative wouldn't hurt either... |
| Sebastiani is offline | |
| | #23 |
| Registered User Join Date: May 2008
Posts: 70
| thanks for the useful links. So the result that i got is this Code: #include<stdio.h>
#include<string.h>
#define size 5
typedef struct {
char last[10]; /* Structure of type student*/
char first[10];
double gpa;
} stud;
typedef struct {
char last[10];
char first[10]; /* structure of type employee*/
double salary;
} emp;
void check_add_salary (emp *e, stud *s);
main(){
FILE *stud_file=NULL;
FILE *employ_file=NULL;
FILE *employ_file2=NULL;
int i=0 , j=0;
stud arr_stud[size]; /* An array of structures (students)*/
emp arr_emp[size]; /*An array of structures (employees)*/
if((stud_file=fopen("student.txt","r"))==NULL)
printf("\nThe file could not be opened\n\n");
else
{ fseek(stud_file,50,SEEK_SET);
while(!feof(stud_file))
{fscanf(stud_file,"%s%s%lf\n", arr_stud[i].last,arr_stud[i].first,&arr_stud[i].gpa);
i++;
}
fclose(stud_file);
}
/*end of else*/
i=0;
if((employ_file=fopen("employees.txt","r"))==NULL)
{
printf("\nThe file could not be opened\n\n");
}
else
{
fseek(employ_file,51,SEEK_SET);
while(!feof(stud_file))
{
fscanf(employ_file,"%s%s%lf\n", arr_emp[i].last,arr_emp[i].first,&arr_emp[i].salary);
i++;
}
fclose(employ_file);
}
/***end of reading from file employess*/
check_salary(arr_emp,arr_stud);
if((employ_file2=fopen("update.txt","w"))==NULL)
{
printf("\nThe file could not be opened\n\n");
}
else
{ fputs("\t\tThe new list of employees(after salary change)\n\n",employ_file2);
fputs("Last name\tFirst name\t\tGPA\n",employ_file2);
fputs("************************************************\n",employ_file2);
for(i=0;i<size;i++)
fprintf(employ_file2,"%-10s %-10s %-10.2f\n", arr_emp[i].last,arr_emp[i].first,arr_emp[i].salary); /* Display the list of employees sorted by alphabetical order*/
fputs("\n************************************************\n\n\n\n",employ_file2);
fclose(employ_file2);
printf("\n The file of employees with the new salary has been created n\" );
}
void check_salary (emp *r, stud *s)
{
int i , j;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
if( strcmp (e[i].last,s[j].last)==0 && strcmp(e[i].first,s[j].first)==0 )
if(s[i].gpa>3.0) /* The if statement is to add 10% to the salary*/
{ /*of employees whose students have GPA>3*/
r[i].salary = r[i].salary + ((r[i].salary)*0.1); j=size-1;
}
}
Thanks |
| overlord21 is offline | |
| | #24 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #25 |
| Registered User Join Date: Jun 2008
Posts: 1,135
| I think you are confused too much with the sequential thing. The difference is only that in a sequential format you don't have a file pointer that can take any value. You start from the beginning reading until you go to the end. That is what you are doing. Most of the times that is what people do. You ONLY fseek() once, do go 50 spaces ahead. That is not random access. If you want, you can read 50 characters and have the same result with 50 times fgetc() which reads one character a time. That will move the file pointer 50 spaces ahead. EDIT: I believe the difference with sequential and random access lies on the hardware, not the software. If you had a tape you will have to read from the beginning to the end. In a HD you can read from wherever you want. If you kind of think though how a tape reader would work, it would be fine to use fseek() if the tape reader wasn't too simple. If the tape reader didn't want to use the same functions with files then it would have its own, thus no fseek(). Anyway. Sequential is really not going back to the file. Thus not using fseek() to go back. But in any case, yeah, just not use fseek Last edited by C_ntua; 09-24-2008 at 08:16 AM. |
| C_ntua is offline | |
| | #26 |
| Registered User Join Date: May 2008
Posts: 70
| i see so the program is fine as it is now right ot should i remove one fseek since i use two?...... and for the " printf("\n The file of employees with the new salary has been created\n\n\");" it tells me that there is some unterminated string or character .. |
| overlord21 is offline | |
| | #27 | |
| Registered User Join Date: May 2008
Posts: 70
| Quote:
| |
| overlord21 is offline | |
| | #28 |
| Registered User Join Date: Sep 2006
Posts: 2,517
| Code:
Example:
#include <stdio.h>
int main(void)
{
FILE *stream;
int i = 100;
char c = 'C';
float f = 1.234;
/* open a file for update */
stream = fopen("DUMMY.FIL", "w+");
/* write some data to the file */
fprintf(stream, "%d %c %f", i, c, f);
/* close the file */
fclose(stream);
return 0;
}
|
| Adak is offline | |
| | #29 |
| Registered User Join Date: May 2008
Posts: 70
| thanks and for the little problem in my last post can you help? Adak? |
| overlord21 is offline | |
| | #30 |
| Registered User Join Date: May 2008
Posts: 70
| well i removed the fseek and it works |
| 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 |