#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    52
    Rep Power
    10

    simple c program to find number of data points


    Dear All,
    I am a beginner in C programing. I need to write a simple c program to find the number of numbers in a file. I have a data file containing integers placed one below the other (single column). On opening the file it looks like this (only positive integers!):
    26598
    13232
    32155
    .
    .
    .
    I need to find the number of numbers in that file. I tried the program below but not successful. Please check correct and remove any unwanted meaning less codes and redundant lines. I need the give the file name from command window.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #define LNGMAX 3000
    #define NMAX 3000
    int main(int argc, char *argv[])
    {
      FILE *fi;
      char ipfile[LNGMAX];
      int i,sum=0;
         if(argc != 2)
        {
          puts("!!!!type fold <i/pfilename>");
          return(-1);
        }
        
        strcpy(ipfile,argv[1]);
        if ( (fi=fopen(ipfile,"r")) == NULL )
        { printf("\n !!! Error %s cannot open\n",ipfile);
          return(-1); }
    /*no. of data points*/
    
    for(i=0;i<NMAX;i++)   
       {sum+=i;}
      
       printf("No. of data points: %d\n", sum);
    
     fclose(fi);
     puts("...Done...");
     return(0);
         }
    Thanks,
    Kumar.
    Last edited by kumaaar; February 20th, 2017 at 08:49 AM.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,843
    Rep Power
    509
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #define LNGMAX 3000
    #define NMAX 3000
    int main(int argc, char *argv[])
    {
      FILE *fi;
      char ipfile[LNGMAX];
      int i,sum=0;
      if(argc != 2)
        {
          puts("!!!!type fold <i/pfilename>");
          return(-1);
        }
    
      strcpy(ipfile,argv[1]);
      if ( (fi=fopen(ipfile,"r")) == NULL )
        {
          printf("\n !!! Error %s cannot open\n",ipfile);
          return(-1);
        }
    
    
    
      /* here, instead of computing NMAX*(NMAX-1)/2 slowly, you need to read the file */
      for(i=0;i<NMAX;i++)
        {
          sum+=i;
        }
      /* of the various ways to count data items, counting new lines is the simplest */
      /* idea that could possibly work. */
      /* improvements:
       *    add one if the last character in the file is a digit.
       *    assume that the input might be erroneous
       *      verify that the printable characters are digits.
       *      accept multiple numbers per line.  Error or acceptable?
       */
      /* https://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/ */
    
    
      printf("No. of data points: %d\n", sum);
    
      fclose(fi);
      puts("...Done...");
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    52
    Rep Power
    10
    Hello,
    thanks for your reply.
    I dont want to computer nmax*(nmax-1)/2. Just need to count the number of numbers in a file. And these numbers are not 1, 2, 3, ...
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,843
    Rep Power
    509
    This loop computes NMAX*(NMAX-1)/2 .
    Code:
    for(i=0;i<NMAX;i++)
        {
          sum+=i;
        }
    If you want to count lines in a file you need to read the file. Here's a hint, use fgets . Or something like
    Code:
    #include<stdio.h>
    
    ////////////////
    
    #define LF 10
    
    int c, newlines = 0;
    
    while ((c = fgetc(fi)) != EOF)
      newlines += c == LF;
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    52
    Rep Power
    10
    Hello,
    I managed to write program (just modified my old program) shown below. By running this program I would write a output file. I want to write the first column
    as 0, 1, 2, 3, ... and second column is just the integer number as in the input file.
    The program writes the second column properly and but the 1st column is just 0.0000, 0.0000, 0.0000 ??please help me to modify the program. I also included 'fp'. if I type fp as 512, the the c program should select the corresponding
    value of second column..how to do it?
    Suppose the input file is (my input file has only the second column..so i wrote a program to change as below with 2 columns..but the program writes zeros
    in 1st column and the second column is okay): Later I want to write in a file with 'fp'..as a beginner I start to write c program to write a file as below:
    0 2323
    1 45564
    2 35445
    .
    .
    511 2656
    512 2626
    513 6987
    .
    .
    .
    1023 3694
    After successfully writing the above file I will include fp in c program so that the file will be changed to see below:
    I included 'fp' to modify further..
    e.g., if fp is 512, then the program should find the difference of 511 and 513 (2656 minus 6987, see above), 510 and 514, ....until there is no pairs. and the result
    should be written as a file with 1st column as 0,1,2,3,... and second column with differences...
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #define LNGMAX 3000
    #define NMAX 3000
    
    int main(int argc, char *argv[])
    
    {
      FILE *fpein,*fpaus;
      char *fs,datein[LNGMAX],dataus[LNGMAX],zeile[LNGMAX];
      double x[NMAX],y[NMAX],fp;
      int ndat,i;
    
      if (argc != 4)
        {
          puts(" !!! Call: f <datein> <dataus> <fp>"); 
          puts(" ... fp  : folding point");
          return(-1);
        }
      
      strcpy(datein,argv[1]);
      if ( (fpein=fopen(datein,"r")) == NULL )
        { printf("\n !!! Error %s cannot open\n",datein);
          return(-1); }
      
      strcpy(dataus,argv[2]);
      if ( (fpaus=fopen(dataus,"w")) == NULL )
        { printf("\n !!! Error %s cannot open\n",dataus);
          return(-1); }
     
      sscanf(argv[3],"%lf",&fp);
      printf("Folding point : %.1lf \n", fp);
      
    /* No. of data points */
    
      for(i=0;i<NMAX;i++)   
       {x[i]=0.;
        y[i]=0.;}
        i=0;
        
        do {
    	   fs=fgets(zeile,LNGMAX,fpein);
    	   /*printf("%s", zeile);*/
    	    if (fs!=NULL)      
              { 
               sscanf(zeile,"%lf   \n",&y[i]);
              /* printf("%lf \n",y[i]);*/
    	       i++;
               }
            } while (fs!=NULL);
       ndat = i;
       fclose(fpein);
       printf("No. of data points: %d\n", ndat);
      
     for (i=0;i<ndat;i++)
      {
       fprintf (fpaus,"%lf    %lf\n", x[i],y[i]);
      }
     fclose(fpaus);
     puts("....Done");
     return(0);
    }
    Last edited by kumaaar; February 23rd, 2017 at 11:25 AM.
  10. #6
  11. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,843
    Rep Power
    509
    sscanf(argv[3],"%lf",&fp); /// here you can test that sscanf returned 1 argument converted, success. Program has other similar untested scans.


    strcpy(dataus,argv[2]); ///// no reason to copy this. Can only lead to trouble.
    if ( (fpaus=fopen(dataus,"w")) == NULL )

    if ( (fpaus=fopen(argv[2],"w")) == NULL ) /// argv[2] directly
    (same comment applies to the first file you open)


    And now to answer your question....
    Code:
      for(i=0;i<NMAX;i++)
        {
          x[i]=0.;/// x initialized to zero.  And it is never changed.  You should expect displayed zeros.  Did you intend x[i] = i;  ???
          y[i]=0.;  //// why are x and y floating type if you intend to work with integers?
        }
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    52
    Rep Power
    10
    Dear b49P23TIvg,
    I corrected as per your suggestion. And now the program produces the expected output. Now I want to make some computation to the second column.
    My input file has only integers one below other as a single column. By the c program, see below, I can now now write a new file with two columns,
    i.e. 1st column has 0, 1, 2, ...and 2nd column has the same integers as in the input file.
    Now I want to make some computations on the 2nd column and rewrite the output file.
    As I said earlier there is 'fp'. This fp is given by me. It will be integer or half-integer, i.e. 512, or 513, or 512.5 or 510.5 ( but not 512.25 or -511).
    If I give for example 512, then the c program should perform the following:
    think that the input file has
    0 12313
    1 23423
    2 67867
    3 56765
    .
    .
    .
    510 98655
    511 23453
    512 32434
    513 95454
    514 34556
    .
    .
    .

    if fp is 512
    then the c program should do
    0 (511-513) /*i mean (23453-95454)*/
    1 (510-514)
    2
    3
    .
    .
    .
    and if the fp is 512.5
    then the c program should do
    0 (512-513) /*i mean (32434-95454)*/
    1 (511-514)
    2
    3
    .
    .
    .
    The program shoudl write until there is no pairs!!!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #define LNGMAX 30
    #define NMAX 3000
    int main(int argc, char *argv[])
    {
      FILE *fpein,*fpaus;
      char *fs,datein[LNGMAX],dataus[LNGMAX],zeile[LNGMAX];
      int ndat,i,x[NMAX],y[NMAX];
      double fp;
    
      if (argc != 4)
        {
          puts(" !!! Call: f <datein> <dataus> <fp>"); 
          puts(" ... fp  : folding point");
          return(-1);
        }
      
      if((fpein=fopen(argv[1],"r"))==NULL)
        {printf("\n !!! Error %s cannot open\n",datein);
          return(-1);}
      if((fpaus=fopen(argv[2],"w"))==NULL)
        {printf("\n !!! Error %s cannot open\n",dataus);
          return(-1);}
      sscanf(argv[3],"%lf",&fp);
      printf("Folding point : %lf\n", fp);
      
    /* No. of data points */
    
      for(i=0;i<NMAX;i++)   
       {x[i]=i;
        y[i]=i;}
        i=0;
        
        do{
    	   fs=fgets(zeile,LNGMAX,fpein);
    	   /*printf("%s", zeile);*/
    	   if(fs!=NULL)               
    	    { 
            sscanf(zeile,"%d   \n",&y[i]);
            /* printf("%d \n",y[i]);*/
    	    i++;
            }
           } while(fs!=NULL);
       ndat = i;
       fclose(fpein);
       printf("No. of data points: %d\n", ndat);
    /*folding finding the optimum fp
    HERE I WANT TO WRITE THE CODES
    PLEASE HELP*/
    /* writing to file*/  
     for (i=0;i<ndat;i++)
      {
       fprintf (fpaus,"%d     %d\n", x[i],y[i]);
      }
     fclose(fpaus);
     puts("....Done");
     return(0);
    }
  14. #8
  15. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,843
    Rep Power
    509
    Don't need to store the indexes x because x[i] == i that is you always recompute the count anyway.

    I've changed a few items.
    Code:
    int is_int(double f) {
      /* return 0 iff f is not integral */
      return ((int)f) == f;
    }
    
    int differences(int*y, int n, double fold, int*diffs) {
      /*
       * compute differences about fold
       * return the tally of these.
       */
      int oben, unten, i;
      if (is_int(fold)) {
        oben = (int)fold + 1;
        unten = oben - 2;
      } else {
        unten = (int)fold;
        oben = unten + 1;
      }
      for (i = 0; (oben < n) && (-1 < unten); ++i, ++oben, --unten)
        diffs[i] = y[unten] - y[oben];
      return i;
    }
    
    int main(int argc, char *argv[])
    {
      FILE *fpein,*fpaus;
      /* datein and dataus become pointers to char */
      char *fs,*datein,*dataus,*folding_point, zeile[LNGMAX];
      int ndat,i,y[NMAX], diffs[NMAX], number_of_differences; /* no more x */
      double fp;
    
      if (argc != 4)
        {
          puts(" !!! Call: f <datein> <dataus> <fp>");
          puts(" ... fp  : folding point");
          return(-1);
        }
      datein = argv[1];		/* naming the variables may be smart */
      if((fpein=fopen(datein,"r"))==NULL)
        {
          printf("\n !!! Error %s cannot open\n",datein);
          return(-1);
        }
      dataus = argv[2];
      if((fpaus=fopen(dataus,"w"))==NULL)
        {
          printf("\n !!! Error %s cannot open\n",dataus);
          return(-1);
        }
    
      folding_point = argv[3];
      sscanf(folding_point,"%lf",&fp);
      printf("Folding point : %lf\n", fp);
    
      for(i=0;i<NMAX;i++)
        y[i]=0; /* 0 */
    
      ndat = 0;
      do {
        fs=fgets(zeile,LNGMAX,fpein);
        /*printf("%s", zeile);*/
        if(fs!=NULL)
          {
            sscanf(zeile,"%d   \n",y + ndat);   /* &y[ndat] and y+ndat mean the same thing here */
    	ndat++;
          }
      } while(fs!=NULL);
      fclose(fpein);
      printf("No. of data points: %d\n", ndat);
      number_of_differences = differences(y, ndat, fp, diffs);
      for (i=0;i<ndat;i++)
        {
          fprintf (fpaus,"%d     %d\n", i,y[i]);
        }
      fputs("\ndifferences about fold\n\n", fpaus);
      for (i = 0; i < number_of_differences; ++i)
        fprintf(fpaus,"\t%d\n", diffs[i]);
      fclose(fpaus);
      puts("....Done");
      return(0);
    }
    can also compute the lower and over indexes directly,
    unten = (int)(fold - 0.25), oben = (int)(fold + 1.25);
    Last edited by b49P23TIvg; February 24th, 2017 at 02:09 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    52
    Rep Power
    10
    Thanks b49P23TIvg!

IMN logo majestic logo threadwatch logo seochat tools logo