I have a question:
I create a file named "number.inp". This file has a row of numbers:
5 4 4 3 3 3 2
note: 5 is the first number, haven't any space or char in front of 5
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{clrscr();
int ch,v[10],count=0,i=0,j=1,n=0,a[10];
FILE *f;
f=fopen("number.inp","r");
if(ch!=' ')
while((ch=getc(f))!=EOF)
{i++;
fscanf(f,"%d",&v[i]);
count++;
}
printf("file has %d numbers\n",count);
printf("the first number is %d \n",v[1]);
for(i=1;i<=count;i++)
if(v[i]!=v[i+1])
{a[j]=v[i]; j++;n++;
}
printf("file has %d different chars \n",n);
for(j=1;j<=n;j++)
printf(" %2d",a[j]);
getch();
fclose(f);
}
output of these code is:
file has 6 numbers
the first number is 4
file has 3 different chars:
4 3 2
the question is:
why doesn't have 7 numbers but 6 numbers
why the first number is 4 not 5
why doesn't have 4 different numbers(5 4 3 2) but 3 different numbers (4 3 2)
I think the first 2 chars is ignored.If I change the content of file named: "number.inp" to:
5 4 4 3 3 3 2
note: have a space in front of 5
this time the result is OK:
file has 7 numbers
the first number is 5
file has 4 different numbers:
5 4 3 2
thanks! :o