Thread: need help link function

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    17

    need help link function

    i want write a program that get name,familyname,student number of ten people
    and then store in a file(students.txt)

    and then with a new function read that file and save in stucture with these
    field name,fname,student_no

    and finally with another function sort studnet_no and save in sortedstudents.txt file

    i do not know how can i link these function
    can someone help me?

    Code:
    #include <stdio.h>
    int main()
    {
    FILE *f;
    char name[10][10],fname[10][10];
    int student_no[10];
    f=fopen("students.txt","w+");
    for (int i=0 ; i<10 ; i++)
    {
    printf("\nEnter Name Of Student %d:",i+1);
    scanf("%s",name[i]);
    printf("Enter Family Name of Student %d:",i+1);
    scanf("%s",fname[i]);
    printf("Enter Student Number Of Student %d:",i+1);
    scanf("%ld",&student_no);
    }
    fprintf(f,"name		fname		student_no\n");
    fprintf(f,"-----		-----		----\n");
    for (i=0 ; i<10 ; i++)
    fprintf(f,"%s		%s		%ld\n",name[i],fname[i],student_no[i]);
    fclose(f);
    return 0;
    }
    
    // store in structure function
    struct studentinfo{
    		char name[20];
    		char fname[20];
    		int student_no[10];
    		}student[10];
    void main()
    {
    FILE *f;
    int k;
    p=fopen("student.txt","rb");
    fread(student,sizeof student,1,p);
    for(k=0 ; k<10 ; k++)
    {
    printf("%s",student[k].name);
    printf("%s",student[k].fname);
    printf("%s",student[k].student_no);
    }
    fclose(p);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by aminpost View Post
    i want write a program that get name,familyname,student number of ten people
    and then store in a file(students.txt)

    and then with a new function read that file and save in stucture with these
    field name,fname,student_no

    and finally with another function sort studnet_no and save in sortedstudents.txt file

    i do not know how can i link these function
    can someone help me?
    Sure. Here's a start. What should we do next?

    Code:
    #include <stdio.h>
    
    // store in structure function 
    struct studentinfo {
    		char name[20];
    		char fname[20];
    		int student_no[10];
    }student[10];
    
    //function definitions
    void readData(void);
    void showData(void);
    
    int main()
    {
       FILE *f;
       /* let's change this to match the size of the name and fname in the student struct */
       char name[10][20],fname[10][20]; 
       int student_no[10];
       
       /*our three arrays will need some memory malloc'd for them would you like to try that, here? */
    
       f=fopen("students.txt","w+");
       for (int i=0 ; i<10 ; i++)
       {
          printf("\nEnter Name Of Student %d:",i+1);
          scanf("%s",name[i]);
          printf("Enter Family Name of Student %d:",i+1);
          scanf("%s",fname[i]);
          printf("Enter Student Number Of Student %d:",i+1);
          scanf("%ld",&student_no);
       }
    
       /* I would not print this header information, into your student data file 
       What about putting it just before the showData() loop that prints the student data
       onto the screen?*/
    
    //   fprintf(f,"name		fname		student_no\n");
    //   fprintf(f,"-----		-----		----\n");
    
       /* this part looks great */
       for (i=0 ; i<10 ; i++)
       fprintf(f,"%s		%s		%ld\n",name[i],fname[i],student_no[i]);
       fclose(f);
    
       //calling the new functions
       readData();
       showData();
    
       return 0;
    }
    
    /* we can't have two functions named "main". So let's give this one a new name.
    How about readData() ? Last function will be showData(), OK?
    */
    
    void readData()
    {
       FILE *f;
       int k;
       p=fopen("student.txt","rb");   //what about f ?? p isn't defined in our variables, yet.
       //you might want to check for an error opening the file, here
    
       //do you want to put this in a loop?
       fread(student,sizeof student,1,p);
    
        fclose(p);
    }
    
    
    void showData()
    {
       int k;
    
       for(k=0 ; k<10 ; k++)
       {
          printf("%s",student[k].name);
          printf("%s",student[k].fname);
          printf("%s",student[k].student_no);
       }
    
    }
    Last edited by Adak; 01-05-2009 at 04:20 AM.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    thank for your fast reply
    you are lovely

    now i want a function to add in this program that sort(high to low) students by student_no and then store in a new file(storedstudent.txt)

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by aminpost View Post
    thank for your fast reply
    you are lovely

    now i want a function to add in this program that sort(high to low) students by student_no and then store in a new file(storedstudent.txt)
    OK, here's *some* of the sort code that would be appropriate for sorting a small number of students:

    Code:
    for(i = 0; i < MaxStudents - 1; i++)  {
       for(j = i + 1; j < MaxStudents; j++)  {
          if(student[i].number > student[j].number)  {
             //a higher numbered student is ranked lower, and needs to be swapped
             tempNum = student[i].number;   //put the higher numbered student into a variable to save it.
    
             /* a sticky bit - You have to copy the student's name into a temp array, but you can't
             copy it by just tempName = student[i].name. Instead you have to use strcpy(destination, source), so */
             strcpy(tempName, student[i].name);
             /*I need you to make the tempName array, in this function, and malloc it memory, so it's equal
             with the space in student.name. Also, you'll need another tempfname array, to hold the 
             family name student.fname. It will also need to have memory malloc'd in this function, 
             and with space equal to the space in student.fname.
             
             So, you do that, and post it back up, and then we'll continue with this selection sort. */
             
    
       }
    }

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    Quote Originally Posted by Adak View Post
    OK, here's *some* of the sort code that would be appropriate for sorting a small number of students:
    thx for your hot guide
    but now can help me to solve these error(i think error is in call sort function)

    Code:
    #include <stdio.h>
    
    #include <stdio.h>
    
    struct studentinfo {
    		char name[20];
    		char fname[20];
    		int student_no[10];
    }student[10];
    
    void readData(void);
    void showData(void);
    void sort(int,char,char);
    
    int main()
    {
    	FILE *f;
    	char name[10][20],fname[10][20];
    	int student_no[10];
    
    	f=fopen("students.txt","w+");
    	for (int i=0 ; i<10 ; i++)
    	{
    		printf("\nEnter Name Of Student %d:",i+1);
    		scanf("%s",name[i]);
    		printf("Enter Family Name of Student %d:",i+1);
    		scanf("%s",fname[i]);
    		printf("Enter Student Number Of Student %d:",i+1);
    		scanf("%d",&student_no);
    	}
    
    	for (i=0 ; i<10 ; i++)
    	fprintf(f,"%s		%s		%d\n",name[i],fname[i],student_no[i]);
    	fclose(f);
    
    	readData();
    	showData();
    	sort(student[i].student_no,student[i].name,student[i].fname);
    	return 0;
    }
    
    void readData()
    {
    	FILE *f;
    	f=fopen("student.txt","rb");
    	fread(student,sizeof student,1,f);
    	 fclose(f);
    }
    
    void showData()
    {
    	int k;
    	for(k=0 ; k<10 ; k++)
    	{
    		printf("%s",student[k].name);
    		printf("%s",student[k].fname);
    		printf("%s",student[k].student_no);
    	}
    }
    
    void sort(int student[].student_no,char student[].name,char student[].fname)
    {
    int i,j;
    char tempname[10],tempfamily[10];
    int tempNum[10];
    FILE *p;
    p=fopen("sortedStudents.txt","w+");
    for(i = 0 ; i < 10 ; i++)
    {
    for(j=i+1 ; j<10 ; j++)
    if(student[i].student_no > student[j].student_no)
    {
    tempNum=student[i].student_no;
    tempname=student[i].name;
    tempfamily=student[i].fname;
    
    student[i].student_no=student[j].student_no;
    student[i].name=student[j].name;
    student[i].fname=student[j].fname;
    
    student[j].student_no=tempNum;
    student[j].name=tempname;
    student[j].fname=tempfamily;
    }
    for(i=0 ; i<10 ; i++)
    fprintf(p,"%s		%s		%d\n",name[i],fname[i],student_no[i]);
    fclose(p);
    }
    Last edited by aminpost; 01-05-2009 at 01:22 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by aminpost View Post
    thx for your hot guide
    but now can help me to solve these error(i think error is in call sort function)
    Yes, we're not done with the sort function. Note the function definition (aka prototype), for sort() - you've changed it to pass an int, and 2 char's. Now look at the sort() function itself, and what is it being passed? An int - fine, and two arrays - but two arrays are not two char's!!! <eek!>

    Our student array of structs is now global - so all functions can access it directly, without it being a passed in parameter. So there is no need for any parameters for sort(), at all.

    If you do want student[] array of structs to be local, and to be passed to each function, then you need to:

    1) Move the declaration of student array, into main(), and 2) pass a pointer to that array, to each function, because every function needs the student array.

    So sort() could be a void function (as student array is now), or sort() could need a pointer to that array, if you move student[] array into main().

    You decide.

    Code:
    #include <stdio.h>
    
    #include <stdio.h>
    
    struct studentinfo {
    		char name[20];
    		char fname[20];
    		int student_no[10];
    }student[10];
    
    void readData(void);
    void showData(void);
    void sort(int,char,char);
    
    int main()
    {
    	FILE *f;
    	char name[10][20],fname[10][20];
    	int student_no[10];
    
    	f=fopen("students.txt","w+");
    	for (int i=0 ; i<10 ; i++)
    	{
    		printf("\nEnter Name Of Student %d:",i+1);
    		scanf("%s",name[i]);
    		printf("Enter Family Name of Student %d:",i+1);
    		scanf("%s",fname[i]);
    		printf("Enter Student Number Of Student %d:",i+1);
    		scanf("%d",&student_no);
    	}
    
    	for (i=0 ; i<10 ; i++)
    	fprintf(f,"%s		%s		%d\n",name[i],fname[i],student_no[i]);
    	fclose(f);
    
    	readData();
    	showData();
    	sort(student[i].student_no,student[i].name,student[i].fname);
    	return 0;
    }
    
    void readData()
    {
    	FILE *f;
    	f=fopen("student.txt","rb");
    	fread(student,sizeof student,1,f);
    	 fclose(f);
    }
    
    void showData()
    {
    	int k;
    	for(k=0 ; k<10 ; k++)
    	{
    		printf("%s",student[k].name);
    		printf("%s",student[k].fname);
    		printf("%s",student[k].student_no);
    	}
    }
    
    void sort(int student[].student_no,char student[].name,char student[].fname)
    {
    int i,j;
    char tempname[10],tempfamily[10];
    int tempNum[10];
    FILE *p;
    p=fopen("sortedStudents.txt","w+");
    for(i = 0 ; i < 10 ; i++)
    {
    for(j=i+1 ; j<10 ; j++)
    if(student[i].student_no > student[j].student_no)
    {
    tempNum=student[i].student_no;
    tempname=student[i].name;
    tempfamily=student[i].fname;
    
    student[i].student_no=student[j].student_no;
    student[i].name=student[j].name;
    student[i].fname=student[j].fname;
    
    student[j].student_no=tempNum;
    student[j].name=tempname;
    student[j].fname=tempfamily;
    }
    for(i=0 ; i<10 ; i++)
    fprintf(p,"%s		%s		%d\n",name[i],fname[i],student_no[i]);
    fclose(p);
    }
    First, please learn to indent your code properly. It helps you see errors in your code, very quickly, and it helps me and everyone else, see them as well. Our eyes just become trained to see things in your code, when you indent properly.

    I use 3 spaces between levels of indentation, but anywhere between 2 and 6 spaces, is fine. Pick a number and be consistent. If you use spaces, then always use the space bar for making those spaces. If you use tabs, then set it for this, and always use tabs. The forum makes a dogs breakfast if you use tabs and mix in spaces, sometimes. Looks horrid.

    Code:
    void sort(int student[].student_no,char student[].name,char student[].fname)
    {
       int i,j;
       char tempname[10],tempfamily[10];
       int tempNum[10];
       FILE *p;
       p=fopen("sortedStudents.txt","w+");
    
    
    /* You need to malloc some memory for tempname[], tempfamily[], and tempNum[]. The compiler 
    *may* give you "safe" memory as is, but that's just good luck. Don't count on it.
    put the malloc 
    code, right here. */
    
       //this is incorrect. make it i < 10 - 1;  j goes to 9, i must stop at < 9. (8)
       for(i = 0 ; i < 10 ; i++)  //i stops at 8
       {
          for(j = i+1 ; j < 10 ; j++) //j stops at 9 (highest array element is 9)
          {      
             if(student[i].student_no > student[j].student_no)
             {
       
                tempNum=student[i].student_no;
    
    /* No way! You have to use strcpy(destination, source), to copy strings in C. */
    
                tempname=student[i].name;
                tempfamily=student[i].fname;
    
                student[i].student_no=student[j].student_no;
    
    /* same here - use strcpy */
    
                student[i].name=student[j].name;
                student[i].fname=student[j].fname;
    
                student[j].student_no=tempNum;
    
    /* and again - use strcpy */
                student[j].name=tempname;
                student[j].fname=tempfamily;
            }   
    
    
    //You need two right facing brace in here, somewhere.
    
    /* Look at the red curly braces {}. You have 4 facing left {, but only 2 facing right, }. 
    They must have an equal number. Now that the indentation is good, it's easy 
    to see that. */
    
             //this needs to go *outside* the sorting loops, not inside them.
             for(i=0 ; i<10 ; i++)
                fprintf(p,"%s		%s		%d\n",name[i],fname[i],student_no[i]);
    
             fclose(p);
    }

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    @Adak: your indenting is not so sweet either. as K&R suggests, you should only put brackets on their own lines at functions' initialization.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by abraham2119 View Post
    @Adak: your indenting is not so sweet either. as K&R suggests, you should only put brackets on their own lines at functions' initialization.
    You're incorrect, as is shown by this example code from page 105 of K & R, second edition:

    Code:
    while (( *s = *t ) != '\0' )  {
        s++;
        t++;
    }
    Now, does that last curly brace, look like it's on it's own line, or not?

    It's the same throughout the book, every closing brace, is on it's own line.

    I diverge from K & R's indentation style in my functions, by including the functions first curly brace, on the same line as the function name, similar to the while loop above.

    I did not reformat this program, entirely to my own style of indentation. I try to use the OP's style, if he / she, has one that looks OK.

    My style of indentation is s-w-e-e-t, but not necessarily better than any other consistent style with moderate indentation.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    i was talking about 'opening-brackets' ({) only.

    and does this:
    Code:
    while (( *s = *t ) != '\0' )  {
        s++;
        t++;
    }
    look like a function initialization to you?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you mean "opening brackets", then write "opening brackets". The char in question is a brace, not a bracket, but no matter.

    I like my style of indentation, and you don't.

    OK.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    abraham2119, Adak is trying to get aminpost to use a consistent and reasonable style. On the other hand, you are trying to impose a particular style, so you can stop trying to restart the holy war now.
    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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are several styles available, not just K&R. And, in fact, not all people use or like K&R.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM