[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Fw: Infinite Joy Ride(example)



please do give consideration to the code.
especially Ajay Kumar Divedi and Binand Raj S.

r /\ |-| |_| |_
~~~~~~~~~~~~~~~~~~~~~~
Always remember you're unique.
Just like everyone else.
~~~~~~~~~~~~~~~~~~~~~~

The Northern Linux lug-northindia@xxxxxxxxxxxxxxx

----- Original Message ----- 
From: "kamesh jayachandran" <kameshj@xxxxxxxxxxxxx>
To: <rahulj@xxxxxxxxxxxx>
Sent: Monday, April 09, 2001 7:53 PM
Subject: Infinite Joy Ride(example)


> hai,
> I am kamesh.Me to a beginner to Linux.I have seen ur mail 
> regarding scanf that takes infinite joy ride when it 
> expects some other data type and recieves something else.
> 
> Now me too faced the same problem.
> I am pasting the code here so that someone can reason out.
> 
> when I enter some character to scanf(that expects the long 
> int) it goes in to infinite loop.
> 
> //code begins here
> #include<stdlib.h>
> #include<stdio.h>
> #define MAX 50
> int main(void)
> {
> FILE *fp;
> int data,count,array[MAX];
> long offset;
> for(count=0;count<MAX;count++)
> array[count]=count*10;
> if((fp=fopen("pp","wb"))==NULL)
> {
> fprintf(stderr,"Error opening File \n");
> exit(1);
> }
> if(fwrite(array,sizeof(int),MAX,fp)!=MAX)
> {
> fprintf(stderr,"Error in writing data to the File\n");
> exit(1);
> }
> fclose(fp);
> if((fp=fopen("pp","rb"))==NULL)
> {
> fprintf(stderr,"Error in opening File\n");
> exit(1);
> }
> while(1)
> {
> printf("\nEnter element to read,0-%d,-1 to quit: ",MAX-1);
> scanf("%d",&offset);
> if(offset<0)
> break;
> else if(offset>MAX-1)
> continue;
> if(fseek(fp,(offset*sizeof(int)),1)!=0)
> {
> fprintf(stderr,"Error using fseek\n");
> exit(1);
> }
> fread(&data,sizeof(int),1,fp);
> printf("\nElement %ld has value %d",offset,data);
> }
> fclose(fp);
> return 0;
> }
> //code ends here
> 
>