View Single Post
Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

Re: read each line of file

 
0
  #10
Feb 14th, 2007
Here is my code :

#include<conio.h>
#include<stdio.h>
main()
{
    clrscr();
    int v[10],v1[10],i=0,j,t,k,number,count=0;
    FILE *f ;
    f=fopen("numbers.inp","r");
/* open file numbers.inp to read, for e.g, you inputs two rows ( can be more than two ) of number 
    5 4 4 3 3 3 2
    2 2 1 
*/
     do
     {
        i++;
        fscanf(f,"%d",&v[i]);
        count++;
     }
/* after these above commands, I change all chars in numbers.inp to integers belong to array v[i] so I think 5 4 4 3 3 3 2 and 2 2 1 now are integer not char */
     while((number=getc(f))!=EOF);
/* I think problem is here, when I use fscanf I lost two rows and now
it read to end of file and it considers my two rows as one row v[i]={5 4 4 3 3 3 2 2 2 1} and it apply the beneath algorithm for it.
What I want is it does the algorithm to the first row 5 4 4 3 3 3 2 and export result to result.out, then does with the second row 2 2 1 and export result to result.out */
     k=count;
     while(v[1]>0)
     {
        for(i=2;i<=(v[1]+1);i++)
            v1[i-1]=v[i]-1;
        for(i=(v[1]+2);i<=count;i++)
            v1[i-1]=v[i];
        count--;
        for(i=1;i<=count;i++)
            v[i]=v1[i];
        for(i=1;i<=(count-1);i++)
        for(j=(i+1);j<=count;j++)
        if(v[i]<v[j])
        {
            t=v[i];
            v[i]=v[j];
            v[j]=t;
        }
        for(i=1;i<=count;i++)
        {
            printf("%2d",v[i]);
            if(i==count)     printf("\n");
        }
        k--;
     }
     f=fopen("result.out","w");
     i=k;
     if(v[i]>(k-1)||(v[i]<0)) fprintf(f,"NO");
     if(v[i]==0) fprintf(f,"YES");
     getch();
     fclose(f);
    }
Hope you understand me.
Reply With Quote