Reading a file, my program misses out the first entry.

This is a discussion on Reading a file, my program misses out the first entry. within the C Programming forums, part of the General Programming Boards category; Hi there, I am trying to read some data from a file, sort it and then write it into a ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    9

    Reading a file, my program misses out the first entry.

    Hi there,

    I am trying to read some data from a file, sort it and then write it into a new file. I cannot see where I am going wrong. The TESTfile.dat just contains integers from 1-10. When the program runs it misses off the 1 and adds an extra 10 at the end. Can anyone tell me where I am going wrong. My code is
    Code:
    #include<stdio.h>
    #include<math.h>
    
    int NDD[5]={0};
    main()
    {
      FILE *fp;
      int DD=0; //[10]={0};
      int i=0;
      if ((fp=fopen("/home/angela/Desktop/TESTfile.dat","r"))==NULL) printf("\nFile not opened\n");
    
      else printf ("File opened\n");
      
      const int bsz=100; char buf[bsz];
      while (fgets(buf, bsz, fp) != NULL ) {
        fscanf(fp,"%d",&DD);
        printf("DD= %d\n",DD);   
        int k;
        k=i-5*floor(i/5);
        NDD[k]+= DD;
        i++;
      }
      fclose(fp);
    
    FILE *fout;
    fout=fopen("sortedTESTFILE.dat","w");
    int j;
    for (j=0;j<5;j++) fprintf(fout,"%d\n",NDD[j]);
    fclose(fout);
    
    }
    thanks.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're using both fgets() and fscanf(). You can use either one, but you need to process the data from them, before they are overwritten with new data from the file.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    So should I make DD an array i integers long, use a for loop over i and get rid of line 14 and 15?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And you need to work on your text layout too... don't put multiple statements on a line and indent according to some reasonable pattern.

    Code:
      fp=fopen("/home/angela/Desktop/TESTfile.dat","r");
      if (fp ==NULL) 
        {  
           printf("\nFile not opened\n");
           exit(255);
         }
      else 
         printf ("File opened\n");
      
      const int bsz=100; 
      char buf[bsz];
    See how much easier that is to follow?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-08-2010, 02:03 PM
  2. Replies: 2
    Last Post: 09-13-2009, 04:14 PM
  3. Program Entry Point
    By Sentral in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2006, 04:36 AM
  4. fgets misses first character in file :?
    By rkooij in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 09:27 AM
  5. Replies: 2
    Last Post: 02-20-2005, 01:48 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21