http://s3.supload.com/free/lastscan-...3716.jpg/view/
up there is description of code
and here;'s code

#include<stdio.h>
#include<string.h>
float avg(float);
main()
{
FILE *p[2];
int k,students=10,subjects=4,i,j,id_number,date_of_birth,fail=0;
float marks[100],total=0,avg;
char name[30],course_enrolled[30],filename[10];
for(i=1;i<=2;i++)
{
printf("filename"); 
scanf("%s",&filename);
p[i]=fopen(filename,"w");
printf("Enter Name of Student\n",p[i]);
fscanf(stdin,"%s",&name,p[i]);
printf("Enter Course In Which Student Is Enrolled\n",p[i]); 
fscanf(stdin,"%s",&course_enrolled,p[i]);
printf("Enter ID Number Of %s Enrolled in Course %s\n",name,course_enrolled,p[i]);
fscanf(stdin,"%d",&id_number,p[i]);
printf("Enter Date Of Birth Of %s Enrolled in Course %s and\n ID-Number is %d\n",name,course_enrolled,id_number,p[i]);
fscanf(stdin,"%d",&date_of_birth,p[i]);
for(j=1;j<=4;j++)
{
printf("Enter Marks for Subject %d for Student %s\n",j,name);
fscanf(stdin,"%f",&marks[j]);
total=total+marks[j];
if(marks[j]<50)
fail=fail+1;
else
continue;
} 
avg=(total/400)*100;
printf("AVERAGE=%f\n",avg);
if(avg>=70)
printf("Grade A\n");
if(avg>=60&avg<70)
printf("Grade B\n");
else if(avg>=50&avg<60)
printf("Grade C\n");
else if(avg>=45&avg<50) 
printf("Grade D\n");
else if(avg>=40&avg<45)
printf("Grade E\n");
else if(avg<40)
printf("Fail\n");
if(fail>=2)
printf("Student is failed and cann;t promote\n");
else
continue;
fclose (p[i]); 
}
for(k=1;k<=2;k++)
{
printf("Input filename"); 
scanf("%s",&filename);
p[k]=fopen(filename,"r");
fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %d\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,avg);
fclose (p[k]);
}
getch();
}

The link doesn't work and you didn't describe the problem with your code.

oh sorry for that link !
here;s another link
http://www.MegaShare.com/443191

and the main problem with code is that
when i save data in different files and ask them to fetch the same data from files but it just show me the data of last file no matters if i enter the name of first file

and it is adding average of last student with the current one

and i want to use 1 user defined function here can u tell me which function should i make except average !

see the link and according to that please suggest me any changes

thanks

The indentation of your program is terrible -- hopefully it was just a problem with posting here. So here is a corrected copy

#include<stdio.h>
#include<string.h>
#pragma warning(disable: 4996)

float avg(float);
main()
{
    FILE *p[2];
    int k,students=10,subjects=4,i,j,id_number,date_of_birth,fail=0;
    float marks[100],total=0,avg;
    char name[30],course_enrolled[30],filename[10];
    for(i=1;i<=2;i++)
    {
        printf("filename"); 
        scanf("%s",&filename);
        p[i]=fopen(filename,"w");
        printf("Enter Name of Student\n",p[i]);
        fscanf(stdin,"%s",&name,p[i]);
        printf("Enter Course In Which Student Is Enrolled\n",p[i]); 
        fscanf(stdin,"%s",&course_enrolled,p[i]);
        printf("Enter ID Number Of %s Enrolled in Course %s\n",name,course_enrolled,p[i]);
        fscanf(stdin,"%d",&id_number,p[i]);
        printf("Enter Date Of Birth Of %s Enrolled in Course %s and\n ID-Number is %d\n",name,course_enrolled,id_number,p[i]);
        fscanf(stdin,"%d",&date_of_birth,p[i]);
        for(j=1;j<=4;j++)
        {
            printf("Enter Marks for Subject %d for Student %s\n",j,name);
            fscanf(stdin,"%f",&marks[j]);
            total=total+marks[j];
            if(marks[j]<50)
                fail=fail+1;
            else
                continue;
        } 
        avg=(total/400)*100;
        printf("AVERAGE=%f\n",avg);
        if(avg>=70)
            printf("Grade A\n");
        if(avg>=60&avg<70)
            printf("Grade B\n");
        else if(avg>=50&avg<60)
            printf("Grade C\n");
        else if(avg>=45&avg<50) 
            printf("Grade D\n");
        else if(avg>=40&avg<45)
            printf("Grade E\n");
        else if(avg<40)
            printf("Fail\n");
        if(fail>=2)
            printf("Student is failed and cann;t promote\n");
        else
            continue;
        fclose (p[i]); 
    }
    for(k=1;k<=2;k++)
    {
        printf("Input filename"); 
        scanf("%s",&filename);
        p[k]=fopen(filename,"r");
        fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %d\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,avg);
        fclose (p[k]);
    }
    getch();
}

When I compiled it using VC++ 2008 Express I got several warnings which you need to correct. NEVER EVER ignore warnings because most of the time they are really errors.

>>test1.c(39) : warning C4554: '&' : check operator precedence for possible error; use parentheses to clarify precedence

That warning message tells you to look at line 39 and check of the & operator is correct. Its not what you want -- you wanted the boolean && operator, not the bitwise & operator. The code has similar problem on other lines.

Another problem: lines 15 and 58: remove the & address operator before filename. Character arrays like filename are always passed by address, and adding the & address operator passes a pointer to a pointer, which is not what you want.

I don't have your input file(s) so I can't run the program to test it out.

About the files: I see now that your program creates them. Its not necessary to have an array of file pointers, e.g. FILE *p[2]; . All you need is one and just reuse it. FILE* p; You are asking people to enter the birth date as a single integer -- I don't know about you but I like to enter my birthdate in MM/DD/YYYY or MM-DD-YYYY format and you can't do that with your program. Suggest you change the data type of birthdate to a character array so that you can enter it in normal fashion.

>>when i save data in different files and ask them to fetch the same data from files but it just show me the data of last file no matters if i enter the name of first file

You have to add some more code to read the file after it is opened on line 62. Your program opens the file then just ignores it.

>>and it is adding average of last student with the current one
You have to reinitialize total to 0 before starting the loop on line 25.

thanks alot for your useful suggestions i am gonna work on it now :)

>> You have to add some more code to read the file after it is opened on line 62. Your program opens the file then just ignores it.

can u give me any idea or guess about what type of code i will have to write !
thanks

Actually, now that I look at your program closer, your program doesn't write anything to those files. In post #5 above, the program opens the file on line 16 and closes it on line 53, never writes anything to it inbetween those two lines. So, somewhere before line 53 you have to add code to write the data to the file. What to write? Look at line 60 -- you have to write those entries (name, course title, id number, dob, and average) to the file. Then after line 59 you have to insert another line or so that reads those same variables from the file. Use fscanf() or fgets() to do that.

commented: THANKS ALOT FOR UR USEFUL SUGGESTIONS +1
#include<stdio.h>
#include<string.h>
 
float avg(float x);
main()
{
    FILE *fp;
    int k,students=10,subjects=4,i,j,id_number,fail=0;
    float marks[100],total=0.0,average=0.0;
    char name[30],course_enrolled[30],filename[10],date_of_birth[10];
    for(i=0;i<10;i++)
    {
        fp=fopen("filename","w");
        printf("Enter Name of Student\n");
        fscanf(stdin,"%s",name,fp);
        printf("Enter Course In Which Student Is Enrolled\n"); 
        fscanf(stdin,"%s",course_enrolled,fp);
        printf("Enter ID Number Of %s Enrolled in Course %s\n",name,course_enrolled);
        fscanf(stdin,"%d",&id_number,fp);
        printf("Enter Date Of Birth Of %s Enrolled in Course %s and\n ID-Number is %d\n",name,course_enrolled,id_number);
        fscanf(stdin,"%s",date_of_birth,fp);
        for(j=1;j<=4;j++)
        {
            printf("Enter Marks for Subject %d for Student %s\n",j,name);
            fscanf(stdin,"%f",&marks[j],fp);
            total=total+marks[j];
            if(marks[j]<50)
                fail=fail+1;
            else
                continue;
        } 
        average=avg(total);
        total=0.0;
         printf("AVERAGE=%f\n",average);
        if(average>=70)
            printf("Grade A\n");
        if(average>=60&&average<70)
            printf("Grade B\n");
        else if(average>=50&&average<60)
            printf("Grade C\n");
        else if(average>=45&&average<50) 
            printf("Grade D\n");
        else if(average>=40&&average<45)
            printf("Grade E\n");
        else if(average<40)
            printf("Fail\n");
        if(fail>=2)
            printf("Student is failed and cann;t promote\n");
        else
            continue;
        fclose (fp); 
    }
    for(k=1;k<=10;k++)
    {
        fp=fopen("filename","r");
        fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average,fp);
        fclose (fp);
    }
    getch();
}
float avg(float x)
{
      float a=0.0;
      a=(x/400)*100;
      return (a);
}

bbcode

here's new code wit some modifications but still confused with file management , can anyone modify it for me, i think i am really have some problems with file management.
Thanks

No, I'm not going to try and run your code to find out what the problem(s) may be. I'm going to wait for you to tell me what *specific* problem(s) you have found with the program.

It is your program, after all, not mine.

Is the input wrong? What's wrong with it?
Is the output wrong? What's wrong with it?

It takes time for even a professional programmer to discover your program's problem(s), let alone a hobby programmer, like myself.

Time is valuable, you should respect that.

the problem with program is i want to save records of students in a file but
when the output comes it keep showing record of last student i have entered.
this is the only problem with program.
please do something i have to compelete it in 4 hours for good result


thanks alot for giving me your Important work :).

You've opened the file for reading, not writing, and your fprintf line is wrong. Do it like this:

//example for writing to a file
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;
}
#include<stdio.h>
#include<string.h>
#include<conio.h> 
float avg(float x);
main()
{
    FILE *strea;
    int k,students=10,subjects=4,i,j,id_number,fail=0;
    float marks[15],total=0.0,average=0.0;
    char name[10],course_enrolled[10],date_of_birth[10];
    for(i=1;i<=10;i++)
    {
        strea=fopen("STUDENT.FIL","w+");
        printf("Enter Name of Student,Course,ID-NUMBER, AND DATE OF BIRTH\n");
        fprintf(strea, "%s %s %d %s",&name[10],&course_enrolled[10],&id_number,&date_of_birth[10]);
        for(j=1;j<=4;j++)
        {
            printf("Enter Marks for Subject %d for Student %s\n",j,name[10]);
            fscanf(strea,"%f",&marks[j]);
            total=total+marks[j];
            if(marks[j]<50)
                fail=fail+1;
            else
                continue;
        } 
        average=avg(total);
        total=0.0;
         fprintf(strea,"AVERAGE=%f\n",average);
        if(average>=70)
            fprintf(strea,"Grade A\n");
        else if(average>=60&&average<70)
            fprintf(strea,"Grade B\n");
        else if(average>=50&&average<60)
            fprintf(strea,"Grade C\n");
        else if(average>=45&&average<50) 
            fprintf(strea,"Grade D\n");
        else if(average>=40&&average<45)
            fprintf(strea,"Grade E\n");
        else if(average<40)
            fprintf(strea,"Fail\n");
        if(fail>=2)
            fprintf(strea,"Student is failed and cann;t promote\n");
        else
            continue;
        fclose(strea); 
    }
    for(k=1;k<=10;k++)
    {
        strea=fopen("STUDENT.FIL","r");
        fprintf(strea,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);
        fclose(strea);
    }
    getch();
}
float avg(float x)
{
      float a=0.0;
      a=(x/400)*100;
      return (a);
}

this is modified form and there is an error screenshot is here
http://www.MegaShare.com/448818

:( giving error
program has encountered a problem and needs to close. we are sorry for inconvenience..............send report....
tell me what to do now !

:( giving error
program has encountered a problem and needs to close. we are sorry for inconvenience..............send report....
tell me what to do now !

This is incorrect:

fprintf(strea, "%s %s %d %s",&name[10],&course_enrolled[10],&id_number,&date_of_birth[10]);

Try:
fprintf(strea, %s %s %d %s", name[i], course_enrolled[i], &id_number, date_of_birth[i]);

//The name of the array is an address to the base of the array, so array's need no & in //them, here.

In your first for loop, you need to do (as I understand your program), three things:

1) get input, either from file, or from the user, using fscanf(), or scanf().
2) print the data onto the screen with printf()
3) write the data to a file, with fprintf().

EACH part of the input loops may need a printf() and fprintf() line of code in them.

I don't see that in your program.

Just post your compiler errors or warnings. Operating system warnings do no good - but almost always refer to your program trying to read or write data somewhere it shouldn't, in memory.

Also, this is wrong. You open the file for reading, and then you are writing to the file:

strea=fopen("STUDENT.FIL","r");
        fprintf(strea,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);
        fclose(strea);

Open the file for append or writing, or writing and reading, if you want to write to it.

It's after 2:00 a.m. here, so I have to go to bed. Good luck. You need to study harder on these basic concepts! If you aren't using an IDE with help for C syntax, please get one (some are free).

You have to learn the basics of C language, before you can use it, in a program. You are struggling with that, clearly.

Good luck!

commented: Good help +7


1) get input, either from file, or from the user, using fscanf(), or scanf().

A bit of-topic, but a moderator (WaltP) once wrote this article about scanf(), it's worth the read. (also click the other parts about scanf)

For the rest I completely agree with your post!

Every student of C should know that scanf() is very "fragile" and should only be learned because it's a part of the language, and every book on C seems to use it.

scanf() should thereafter *never* be used, instead use fgets() and send the input to a buffer than can't be over-run.

It takes 10 minutes to learn fgets(buffer_name, size_of_input_you_want, pointer_to_source (could be stdin) ), and once you learn it, you'll be *so* much better off than you ever could be using scanf().

everyone is telling me how to manage a single file but no one is telling me how to save different records in a file and at last read them all.
i read alot about it, searched also but couldnot understand
and some people are saying i even know the basics!
i am really upset
For God sake help me !

I don't want to modify your code. I want you to get smart enough to modify your own code.

If you can write out one record, you can write out a million records, but you can't write out records using incorrect data type specifiers. For instance, date of birth is an array, but you're trying to write it out as an int "%d". That won't work!

Why haven't you checked over these simple errors and fixed them by now? They're very basic, but you haven't done that.

Go through your program and find out specifically what does not work, and ask specific questions about it. If you don't know about something, ask about it, again specifically. We can't read your mind, and have no desire to re-write your code from top to bottom or replace a C programming text book. This is just a forum.

You're going to have to get a lot more actively involved if you want to get this program done right. There are three foundations to programming: Study, Work, and Practice. They will help you, pleading to God will not.

>>>If you can write out one record, you can write out a million records, but you can't write out records using incorrect data type specifiers. For instance, date of birth is an array, but you're trying to write it out as an int "%d". That won't work!

actually when i get some errors from my program then i modify program to simple things to make program easy to understand.
when i modified my program last time it was giving
DATE OF Birth="*OIW:FWFDW"
so i changed date of birth from string to integer that;s why it was "%d"
anyhow right now i am going to try to make my program done. by reading and creating my own logic
thanks for making me to try it myself instead of asking others to do it
but i will contact again if i got some problem.
Thanks alot for giving ur important Time

#include<stdio.h>
#include<string.h>
#include<conio.h>
#pragma warning(disable: 4996)
 
float avg(float x);
main()
{
    FILE *fp;
    int k,students=10,subjects=4,i,j,id_number,fail;
    float marks[100],total=0,average;
    char name[30],course_enrolled[30],filename[10],date_of_birth[30];
    /*printf("filename\n"); 
    scanf("%s",filename);*/
    fp=fopen("filename","w");
    for(i=1;i<=10;i++)
    {     
        printf("Enter Name of Student\n");
        fscanf(stdin,"%s",name);
        fprintf(fp,"%s\n",name);
        
        printf("Enter Course In Which Student Is Enrolled\n"); 
        fscanf(stdin,"%s",course_enrolled);
        fprintf(fp,"%s\n",course_enrolled);
        
        printf("Enter Date Of Birth\n");
        fscanf(stdin,"%s",date_of_birth);
        fprintf(fp," %s\n",date_of_birth);
        
        printf("Enter ID Number\n");
        fscanf(stdin,"%d",&id_number);
        fprintf(fp," %d\n",id_number);
        
        for(j=1;j<=4;j++)
        {
            printf("Enter Marks for Subject\n");
            scanf("%f",&marks[j]);
            total=total+marks[j];
            if(marks[j]<50)
                fail=fail+1;
            else
                continue;
        } 
        average=avg(total);
        total=0.0;
        printf("AVERAGE=%f\n",average);
        fprintf(fp," %f\n",average);
        if(average>=70)
            printf("Grade A\n");
        else if(average>=60&&average<70)
            printf("Grade B\n");
        else if(average>=50&&average<60)
            printf("Grade C\n");
        else if(average>=45&&average<50) 
            printf("Grade D\n");
        else if(average>=40&&average<45)
            printf("Grade E\n");
        else if(average<40)
            printf("Fail\n");
        else 
             continue;
        
        
        
        
        if(fail>=2)
           { 
                   printf("Student is failed and cann;t promote\n");
                   fprintf(fp,"Student is failed and cann;t promote\n");
           }        
        else
            continue;
        fail=0;
        fclose(fp); 
    }
    for(k=1;k<=10;k++)
    {
        /*printf("Input filename"); 
        scanf("%s",filename);*/
        fp=fopen("filename","r");
        fscanf(fp,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,&id_number,date_of_birth,average);
        fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);
        fclose(fp);
    }
    getch();
}
float avg(float x)
{
      float a=0.0;
      a=(x/400)*100;
      return (a);
}

now the only problem with program is that
in last when i open the file to print all records of students on screen it just shows last student records ten time instead of each student .
thanks alot
waiting for reply :)

just shows last student records ten time instead of each student .

Isn't that quite obvious if you take a close look at the loop,

for(k=1;k<=10;k++)
{
    // do this ten times ...

    // ask the user for the file name <-> shouldn't you 
    // open the file before this loop?
    scanf("%s",filename);*/

    // open the file, start reading from the very beginning
    fp=fopen("filename","r");
    
    // grab the information ...
    fscanf(fp,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,&id_number,date_of_birth,average);

    // and display it ...
    fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);

    // now close the file <-> shouldn't you close the file after this loop?
    fclose(fp);
}
#include<stdio.h>
#include<string.h>
#include<conio.h>
#pragma warning(disable: 4996)
 
float avg(float x);
main()
{
    FILE *fp;
    int k,students=10,subjects=4,i,j,id_number,fail;
    float marks[100],total=0,average;
    char name[30],course_enrolled[30],filename[10],date_of_birth[30];
    /*printf("filename\n"); 
    scanf("%s",filename);*/
    fp=fopen("filename","w");
    for(i=1;i<=10;i++)
    {     
        printf("Enter Name of Student\n");
        fscanf(stdin,"%s",name);
        fprintf(fp,"%s\n",name);
        
        printf("Enter Course In Which Student Is Enrolled\n"); 
        fscanf(stdin,"%s",course_enrolled);
        fprintf(fp,"%s\n",course_enrolled);
        
        printf("Enter Date Of Birth\n");
        fscanf(stdin,"%s",date_of_birth);
        fprintf(fp," %s\n",date_of_birth);
        
        printf("Enter ID Number\n");
        fscanf(stdin,"%d",&id_number);
        fprintf(fp," %d\n",id_number);
        
        for(j=1;j<=4;j++)
        {
            printf("Enter Marks for Subject\n");
            scanf("%f",&marks[j]);
            total=total+marks[j];
            if(marks[j]<50)
                fail=fail+1;
            else
                continue;
        } 
        average=avg(total);
        total=0.0;
        printf("AVERAGE=%f\n",average);
        fprintf(fp," %f\n",average);
        if(average>=70)
            printf("Grade A\n");
        else if(average>=60&&average<70)
            printf("Grade B\n");
        else if(average>=50&&average<60)
            printf("Grade C\n");
        else if(average>=45&&average<50) 
            printf("Grade D\n");
        else if(average>=40&&average<45)
            printf("Grade E\n");
        else if(average<40)
            printf("Fail\n");
        else 
             continue;
        
        
        
        
        if(fail>=2)
           { 
                   printf("Student is failed and cann;t promote\n");
                   fprintf(fp,"Student is failed and cann;t promote\n");
           }        
        else
            continue;
        fail=0;
        fclose(fp); 
    }
        fp=fopen("filename","r");
    for(k=1;k<=10;k++)
    {
        /*printf("Input filename"); 
        scanf("%s",filename);*/
        fscanf(fp,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,&id_number,date_of_birth,average);
        fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);
    }
    fclose(fp);
    getch();
}
float avg(float x)
{
      float a=0.0;
      a=(x/400)*100;
      return (a);
}

still same problem ! :(
sorry if there is any stupid mistake in code (please let me know)
thanks for helping Thanks alot

line 12: increase the array size of filename to 255 characters so that it can hold the maximum number of characters the operating system allows for a filename.

lines 13 and 14: uncomment those two lines.

line 15: remove the quotes around filename.

line 74: you close the file at the end of the first loop iteration. Move that line down after the }

waiting and if anyone can chat with me on yahoo or msn please tell me i want to slove this code tomorrow i am from london now's it's 10 30 pm here

i did but now it displays a a message
"Enter your name"
but when i enter my name then it shows error

final1.c encountered a problem and need to be closed sorry for enconvience

:(

>>>>>line 15: remove the quotes around filename.
i removed quotes at line 15 and


>>>>line 74: you close the file at the end of the first loop iteration. Move that line down after the }
closed file after first for loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.