Like Tree1Likes
  • 1 Post By oogabooga

Help writing a C code for pulling information out of files

This is a discussion on Help writing a C code for pulling information out of files within the C Programming forums, part of the General Programming Boards category; Hello, I am new here entirely, and I am new to C programming, I have to write a program, that ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    5

    Help writing a C code for pulling information out of files

    Hello, I am new here entirely, and I am new to C programming, I have to write a program, that uses a "while loop" and "for loop" my teacher gave me two lines of code that had to be used in order to extract the data out of three files he gave us the name for. I need help getting started on that part, I can do the rest of the math stuff on my own, I am having troubles getting started on the code that will pull out the numbers from a file and making them useable in the code.

    Code:
    //get the file name
        while (fscanf(infile, "%d", &number) ==1)
            for (i=0; i < number; i++);
    that is the two lines he gave us, and I don't understand what they do exactly that much. Any help is very much appreciated

    Stuart

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What do the data files look like?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    3
    5 -4 0
    5
    1 -2 3 -4 5

    they look like this. and i understand what the program is suppose to do, just not how to make it do that.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Code:
        while (fscanf(infile, "%d", &number) == 1)
        {
            for (i=0; i < number; i++) // no semicolon here
            {
                fscanf(infile, "%d", &n)
                // do whatever with n
            }
        }
    stahta01 likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Ok, so this is what i have so far, and what i am suppose to do with this data is
    • the total number of elements
    • the summation of values
    • the range of values (the lowest and highest)
    • the mean value (to 3 places)
    • the variance (to 3 places), and
    • the standard deviation (to 3 places).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #pragma warning(disable: 4996)
    #define MAX 25
    //----------------------------------------------------------------------------
    
    
    int highest = 0;
    int lowest = 0;
    int mean = 0;
    
    
    FILE *infile = NULL;
    FILE *outfile = NULL;
    int number = 0;
    int i = 0;
    char filename [MAX];
    char outfilename [MAX];
    
    
    int main(void)
    {
    //get the file name
    	printf ("Please enter the file name. \n");
    	scanf ("%s", &filename);
    	infile=fopen(filename,"r");
    
    
    	while (fscanf(infile, "%d", &number) ==1)
    		for (i=0; i < number; i++);
    }

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Hey, I am still looking for help on this <bump>

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    7 hours ago...
    > for (i=0; i < number; i++) // no semicolon here

    1 hour ago...
    > for (i=0; i < number; i++);

    Here, try this for the inner loop.
    Code:
            for (i=0; i < number; i++) // no semicolon here
            {
                fscanf(infile, "%d", &n);
                sum += n;
                printf("Sum of all numbers so far=%d\n", sum );
            }
    Now work through your list of stats one at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    ok, so i am working on this with a friend, and we are currently looking on how to track the ranges of the programs, we have the program working through the file and getting the correct value for the datas and i am lost as to how to find the lowest number and the highest number. any ideas? if need be i can post my latest version of the code. thank you so much for your help so far.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting Information from Files
    By Pztar in forum C Programming
    Replies: 20
    Last Post: 05-26-2011, 06:41 PM
  2. Pulling source code?
    By cabers in forum C Programming
    Replies: 2
    Last Post: 03-23-2011, 05:59 PM
  3. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 07:48 PM
  4. Pulling Files off a Server
    By ExxNuker in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2006, 10:20 AM
  5. pulling numbers from files....
    By Confuzz in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2004, 07:49 PM

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