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.
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.
> 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.
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 :
You will see the code part in the middle.I wrote it based on this understanding of sscanf: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,"%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; } }} }
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
I changed the line like this:
still doesnt workCode:for(y=0;y<=x;y++) { if(sscanf(&(k[i].s[y]),"%d",&c[i])==1) break; }
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 11:45 AM.
> if use sscanf(s,"%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.
Can you write your loop detailed salem? And thanks for all the explanation.