Thread: Simple File Read Program...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    Unhappy Simple File Read Program...

    Hello all, i am pretty new with C coding, i am currently working on a hw assignment and ran into trouble...It seems i cant get it to accuratly compare entered value with data file vales and display that CORRECT result...here is the source code, and the file it reads from.
    The file it reads from is just a simple txt file containing this data exactly:
    2.3 4.5 2.1 -1.5
    32.4 16.5 2.2 65.0
    11.1 -2.2 -999
    The source code is:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main (void)
    {
      int x,c;
      double y,z,a,b,d,k=0,e,f,g,h,i;
      FILE *input;
      FILE *input2;
    
      input=fopen("F:\\Eng c projects\\sentinal.txt","r");
     if(input==NULL)
     {
     printf("File Error");
     }
      printf("1=Sentinal.txt\n");
      printf("2=Prenum.txt\n");
      printf("Select File number:\n");
      scanf("%i",&x);
    if(x==1)
    {
     printf("File 1 selected.\n");
     printf("Enter threshold value:\n");
     scanf("%lf",&y);
      for(c=0;z>-990;k++) 
      {
        fscanf(input,"%lf",&z);
        if (y>z)
        { 
         c++;
         printf("c is %i\n",c);
        }
        else
        {
         continue;
        }    
      }
    }
    printf("The num of values less than thresh is %i\n",c); 
    printf(" k value %f",k);
    system("pause");
    return(0);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what's wrong with it? (That is: what are you getting that you don't want to get, or not getting that you do want to get?)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    Question re:re

    Ok i made some progress, it previously wasnt giving me the exact number of digits less than the user input..thats ok now....but i have hit a stall and cant figure out what to do next....please help!!!
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main (void)
    {
      int x,c;
      double y,z,a,b,d,k=0,e,f,g,h,i;
      FILE *input;
      FILE *input2;
    
      input=fopen("F:\\Eng c projects\\sentinal.txt","r");
      input2=fopen("F:\\Eng c projects\\prenum.txt","r");
     if(input==NULL)
     {
     printf("File Error");
     }
      printf("1=Sentinal.txt\n");
      printf("2=Prenum.txt\n");
      printf("Select File number:\n");
      scanf("%i",&x);
    if(x==1)
    {
     printf("File 1 selected.\n");
     printf("Enter threshold value:\n");
     scanf("%lf",&y);
      for(c=-1;z>-990;k++) 
      {
        fscanf(input,"%lf",&z);
        if (y>z)
        { 
         c++;
         printf("count is: %i\n",c);
        }
        else
        {
         continue;
        }    
      }
    
      printf("The num of values less than threshold is:\n %i\n",c); 
      b=(c/(k-1))*100.00;
      d=k-1;
      printf("The percent of data below threshold is:\n %6.2fpercent.\n",b);
      printf("Total number of data in file:  %6.2f \n",d);
      printf(" k value %f",k);
    }
    if(x==2)
    {
     printf("File 2 selected.");
     printf("Enter threshold value:\n");
     scanf("%lf",&y);
     fscanf(input2,"%i",&b);
     for(c=b;c<=b;   )
     {
      fscanf(input2,"%lf",&z)
      if (y>                
    }
    } 
    system("pause");
    return(0);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Sometimes I try to pretend to be psychic, but really. How are we supposed to know what your assignment even is, let alone why you stopped, let alone what you're concerned about?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    ok The program is supposed to ask user to select a data file to be read from...which contains the values listed above for the file 1 and file 2 contains the data below. Also the program asks the user (after they select which file to read from) to enter a threshold. The second file starts with an integer indicating how much data follows. (FILE 1 process now works) After they enter the selected threshold, the program is to read the first data figure out of file 2 as an integer then check to see if the rest of the data(excluding the first integer, somehow) is below the threshold, if so, then it counts how many are, and displays the # of data below threshold onto screen. I think thats all the info u need, if not let me know!
    10 3.5 7.6 33.5 14.7
    44.8 -16.4 1.7 0.04
    0.5 33.3
    See u coulda guess that!!.....THX 4 Helpin!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So that's exactly the same question, therefore requiring exactly the same code, except instead of stopping when your sentinel value is read in, you stop when your count equals the first number in the file (which obviously you have to store somewhere special).

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    ok i understand what your saying, but without moiving the integer around(as per instructor) how would i get it to skip that integer? Simple question im sure.......im new....TY tho for ^ help.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't compare it to your threshold? In fact that's going to be really really really really really really really easy to do, since it won't be in your for loop.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    Question ok...

    yeah sorry to ask, but, could u explain that like your talking to a two year old(without being rude plz). Im not familiar with how to do that, this is my first 3 months with C and im sure im way behind for that time length. To make the program compare all the values except that, what would be the best way to do so?? THX for your time.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't write the comparison. Physically restrain yourself from writing "if (number_of_numbers < threshold) count++". Since you read the number of numbers ahead of time, it won't get read again when you go through the for loop, so you automatically without actually doing anything already are not counting the first number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread