Reading numbers from a text file[Help]

This is a discussion on Reading numbers from a text file[Help] within the C Programming forums, part of the General Programming Boards category; Hey i am an newbie when it comes to c programming..i am from mechanical stream and this piece of coding ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Question Reading numbers from a text file[Help]

    Hey i am an newbie when it comes to c programming..i am from mechanical stream and this piece of coding is useful for our final year project..the program runs fine but i just want it to read the inputs from a text file rather than user manually entering it! what changes should i make in the codes..any type of help would be appreciated :-)

    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    float friction,flow;
    float project (float density,float v,float d,float length,float re,int n,float massflow,float inletpressure,float area,float velocity)
    {
    float exitpressure;
      area=(3.1415785*d*length);
      flow=(massflow/density);
      velocity=(massflow/(density*area));
      printf("The Velocity is %f\n",velocity);
      re=((density*velocity*d)/v);
      printf("The Reynold number is %f\n",re);
      if(re<=2000)
      {
        friction=(64/re);
        printf("Friction Factor Is %f\n",friction);
      }
      else
      {
        friction= 0.3164/(pow(re,.25));
        printf("Friction Factor Is %f\n",friction);
      }
      exitpressure=inletpressure-((8*density*flow*flow*friction*length)/(3.1415785*3.1415785*d*d*d*d*d));
      return exitpressure;
    }
    
    
    void main()
    {
    clrscr();
    
    
    float deltap[100],density,v,d,length,massflow,re,exitpressure,inletpressure,area,velocity;
    int n,i=1;
    printf("Enter number of sections\n");
    scanf("%d",&n);
    if(i<=n)
    {
    
    
    printf("Enter The Mass Flow Rate\n");
    scanf("%f",&massflow);
    printf("Enter The 1st sec Diameter of The Pipe(metre)\n");
    scanf("%f",&d);
    printf("Enter The Density of Fluid(kg/m^3)\n");
    scanf("%f",&density);
    printf("Enter The viscosity\n");
    scanf("%f",&v);
    printf("Enter The 1st sec Length of The Pipe(metre)\n");
    scanf("%f",&length);
    printf("Enter The Inlet Pressure\n");
    scanf("%f",&inletpressure);
    
    
    
    
    exitpressure=project(density,v,d,length,re,n,massflow,inletpressure,area,velocity);
    printf("The Friction Coefficient is %f\n",friction);
    printf("The Volumetric Flow rate(Q) is %f m^3/s\n",flow);
    
    
    printf("The Exit Pressure is %f\n",exitpressure);
    deltap[i]=inletpressure-exitpressure;
    i++;
    
    
    
    
    }
    while(i<=n)
    {
    
    
    inletpressure=exitpressure;
    printf("Enter Diam of %d pipe\n",i);
    scanf("%f",&d);
    printf("Enter Length of %d pipe\n",i);
    scanf("%f",&length);
    exitpressure=project(density,v,d,length,re,n,massflow,inletpressure,area,velocity);
    printf("The Friction Coefficient is %f\n",friction);
    printf("The Volumetric Flow rate(Q) is %f m^3/s\n",flow);
    printf("The Exit Pressure is %f\n",exitpressure);
    deltap[i]=inletpressure-exitpressure;
    i++;
    }
    printf("The Pressure Drop\n");
    for(i=1;i<=n;i++)
      printf("\n %d sec drop is %f",i,deltap[i]);
    
    
    getch();
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    You need to first open the text file with fopen() (and close it when you're done with fclose()).

    How you do the file reading implementation depends on whether you can guarantee that the file is free from errors/mistakes. Since you don't seem to do any validation of the inputs the user types in now, I guess you're allowed to assume the file is good.

    Then you can pretty much replace your scanf() calls with fscanf() calls. fscanf is like scanf but reads from a file.

    It'll probably work pretty easily -- but you should give some thought to the robustness of the program. Check the return values of the fscanfs and fopen, etc. It's best to get into checking return values and ferror() as a matter of habit sooner rather than later.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,690
    If your text file is properly formatted you may also be able to use file redirection.

    Jim

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    The suggestion from smokeyangel is correct, but don't assume the file is correct. The return value of scanf should be the number of items that were read. So for example your section where you ask for massflow, d, density, v, length and inletpressure could be written like this

    Code:
    if (fscanf(fp, "%f %f %f %f %f %f", &massflow, &d, &density, &v, &length, &inletpressure) != 6)
       panic();
    Calling panic in this case signals an invalid input file

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Thank You :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a text file and sorting a column of numbers
    By txmusic in forum C Programming
    Replies: 9
    Last Post: 02-26-2012, 08:49 PM
  2. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  3. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  4. Reading numbers from a text file
    By wolfindark in forum C++ Programming
    Replies: 12
    Last Post: 03-24-2007, 01:57 PM
  5. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 06:56 AM

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