Thread: Same arraying question.

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Same arraying question.

    I am sorry I know I opened some topic about this before , but I finall used strpbrk with a correct form , tell me if I have bad codes which works slowly.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct content {
    		
    		char name[30];
    	} X;
     void sirala(X *,int,int *);
    int main()
    {
    X liste[10];
    	FILE *fp;
    		int puanlar[30];
    		int x=0;
    		int i=0;
    		char *a;
    	if((fp=fopen("d.txt","r"))==NULL) {
    		perror("d.txt");
    		return 0; }
    	else {
    
    		
    		while(fgets(liste[i].name,30,fp)!=NULL) {
    			a=strpbrk(liste[i].name,"0123456789");
                         sscanf(a,"%d",&puanlar[i]);
    			i++;
    			x+=1;
    		}
      sirala(liste,x,puanlar);
    fclose(fp);
    	}
    	if((fp=fopen("sonuc.txt","w"))==NULL) {
    		perror("sonuc.txt");
    		return 0; }
    	else {
    		for(i=0;i<x+1;i++){
     fprintf(fp,"%s",liste[i].name); 
    			if(i==3) fprintf(fp,"\n");}}
    	return 0;
    }
    
    
    
    void	sirala(X *kayit,int kont,int *not) {
                  int tur,say,tut;
    			X gecici;  
    			for(tur=0;tur<kont;tur++) {
    				for(say=0;say<kont;say++) {
    					if(not[say]<not[say+1]) {
                                           gecici=kayit[say];
    						kayit[say]=kayit[say+1];
    						kayit[say+1]=gecici;
                                               tut=not[say];
    						not[say]=not[say+1];
    						not[say+1]=tut; } } }
    		}
    There is one thing I dont understand which I want to know why :

    If dont use this code line :

    if(i==3) fprintf(fp,"\n");} }

    When writing the third name it does not make \n while it makes new line writing the names and grades. I hope I asked my question clearly. Thank you.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    got the point .. ^^

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think the language barrier might have something to do with your lack of replies. Never the less, I did run your program against a sample d file and I think I figured out what was going on. For a list of students size n, sort n by grade, and then output the list, or something like that.

    About the integer array: I think a significant improvement would be to set up an array of structures that have a name as a member and grade as a member too. If you can identify what a student is all at once than it should be easier to sort, read, and write student structures.

    Code:
    struct rec
    {
      char name[NAME_SIZE];
      unsigned grade;
    };
    And a simple fgets and sscanf combination, once you've set up an array for the new data, would be all that you need to parse the whole file. The trickiest step is probably just gluing the first and last name together, if you even still feel like going that route: it would be just as valid to store the first and last names separately, I think.

    The function that sorts data and the function that writes the output file will also have to be rewritten to accomodate the new structure, but hopefully that work is quite trivial for you.
    Last edited by whiteflags; 07-28-2007 at 07:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM