Bubble use , read from file then write to another filed as bubble.

This is a discussion on Bubble use , read from file then write to another filed as bubble. within the C Programming forums, part of the General Programming Boards category; No ,I dont need to create integer in struct for example , that is a simple example , but also ...

  1. #16
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    No ,I dont need to create integer in struct for example , that is a simple example , but also I cant completely understand other people's codes ; maybe the way of thinking is different.

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > if(sscanf(k[i].s,"%d",&c[i])==1) break;
    Post your whole code.

    The loop I posted will read a name followed by an integer into each successive array slot.

    There should be no need to redo sscanf() on each k[ i ] member of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #18
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Like I said before when I erased the duplicate struct decleration , it worked. But I am trying to rewrite it without strpbrk .I have done like this so far :

    Code:
    #include <stdio.h> 
    #include <string.h>
    typedef struct kayit {
    		char s[40];
    	} KAY;
    	KAY k[10];
    void sirala(int *,int,struct kayit *);
    
    int main() {
    FILE *fp;
    fp=fopen("d.txt","r");
    
    	int i=0;
    	while(fgets(k[i].s,20,fp)!=NULL)
    	 i++;
    	int x=i-1;
    
    	int y;
    	int c[40];
    	for(i=0;i<=x;i++) {
    		for(y=0;y<=x;y++) {
    			if(sscanf(k[i].s,"&#37;d",&c[i])==1) break;
    		}
    	 printf("%d\n",c[i]);
    		}
    	fclose(fp);
    sirala(c,x+1,k);
    	printf("%s\n%s\n",k[0].s,k[1].s);
    	fp=fopen("sonuc.txt","w");
    	
    	
    	for(i=0;i<=x;i++) {
    	
    		fprintf(fp,"%s",k[i].s); }
    
    
    
    
    
    
    
    	return 0;
    }
    	
    	
    	
    	
    	
    	
    	
    	
    	
    
    	void sirala(int *d,int y,struct kayit *i) {
     int say,tur,tut;
    		
    		for(tur=0;tur<y-1;tur++) {
    			for(say=0;say<y;say++) {
    				if(d[say]>d[say+1]) {
                                 tut=d[say];
    						d[say]=d[say+1];
    						d[say+1]=tut; 
    				KAY gecici =i[say];
    				i[say]=i[say+1];
    				i[say+1]=gecici;
    				}
    				}}
    		
    	}
    You will see the code part in the middle.I wrote it based on this understanding of sscanf:

    I understood from your post that if use sscanf(s,"%d",&a) for s="car2" string program will try to take c as an integer and fail?I

  4. #19
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I changed the line like this:

    Code:
    for(y=0;y<=x;y++) {
    			if(sscanf(&(k[i].s[y]),"&#37;d",&c[i])==1) break;
    		}
    still doesnt work
    What I was trying to do with this code was this :
    y is the the size of the array ,
    I am trying to do for example for "car 24" trying to get c as integer , then try a ,then try r, each time the next adress should be put in the first argument then when it is the " " for first character it takes the 24 as integer.But It does not work , what is my defect?

    Oh god , anyway I will write the program from the very beginning , I cant find my errors..
    Last edited by ozumsafa; 07-24-2007 at 12:45 PM.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > if use sscanf(s,"&#37;d",&a) for s="car2" string program will try to take c as an integer and fail?
    Yes.
    But your approach seems to be to step along the line until success
    Like
    "car2" fails
    "ar2" fails
    "r2" fails
    "2" success.

    What happened to the indentation - it looks a lot messier compared to mine.

    Hint: don't use hard tabs when posting code. Your editor and HTML rarely agree on the subject of how to expand tabs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #21
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Can you write your loop detailed salem? And thanks for all the explanation.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. SYnchronize read & write of a cache file
    By lei_michelle in forum C Programming
    Replies: 4
    Last Post: 02-26-2008, 05:49 PM
  3. write and read system calls
    By nacho4d in forum C Programming
    Replies: 4
    Last Post: 01-28-2008, 10:59 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Can I write & read file in same program?
    By chad03 in forum C Programming
    Replies: 2
    Last Post: 08-04-2003, 09:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21