简体   繁体   中英

Read a textfile.txt of random numbers from the command line argument and find the sum

hope some experts can help me. It's my first time posting on Stack so please be patient with me. Thanks. Here are a list of my problems:

  1. Tried to open a text file from argv[1] but the program showed NULL
  2. Tried to use fgetc to read in one character at a time. When hit "white_space", put ('2.2') in a buff, add a '\\0', then use sscanf to turn it into a double type. Not sure if this approach is right.

Here is my code:

/*
 * This program computes simple statistics from a file of ASCII numbers
 */

/* 
 * File:   statsMain.c
 * Author: William Vu T Nguyen
 * ID: C0436847
 * Created on February 6, 2017, 12:10 PM
 */

#include "statistics.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void printStats(FILE *dest, const int count, const double mean,
        const double ssdev);

// main program

int main(int argc, char** argv) {
    FILE *pFile;
    int count;
    int x;
    char buff[100] = {0};
    char *pBuff = buff;
    double numVal;
    double sum;
    double sumsq;
    double calculatedMean;
    double calculatedStdDev;

    if (argc != 2) {
        fprintf(stderr, "usage: %s dataFileName\n", argv[0]);
        exit(EXIT_FAILURE);
    } else {
        if ((pFile = fopen(argv[1], "r")) == NULL) {
            fprintf(stderr, "Unable to open input file \"%s\"\n", argv[1]);
            exit(EXIT_FAILURE);
        } else {
            while ((x = fgetc(pFile)) != EOF) {
                *pBuff = x;
                sscanf(pBuff, "%lf", &numVal);
                sum += numVal;
                sumsq += (sum * sum);
                count++;
                pBuff++;                
            }           

            calculatedMean = mean(sum, count);
            calculatedStdDev = ssdev(sum, sumsq, count);
            printStats(pFile, count, calculatedMean, calculatedStdDev);
        }
        fclose(pFile);
    }

    return (EXIT_SUCCESS);
}

void printStats(FILE *dest, const int count, const double mean,
        const double ssdev) {
    char str[100] = {0};

    sprintf(str, "%d Values, Mean = %lf, Sample Standard Deviation = %lf"
            , count, mean, ssdev); //deal with buffer
    puts(str);
    fprintf(dest, "%s", str);
}

If my code shows that I'm a novice in C then please be patient as I'm still in my learning stage. I love programming, circuit design, and microcontroller. Did horribly in school when learning those subjects but I never let it be a determined factor of my life. Thanks.

You're also:

  • passing pFile which was opened as read only, and trying to write to it
  • using sum , sumsq , and count without initializing them
  • probably making a mistake with sumsq += (sum * sum);

It's better to take advantage of inherent stdin and stdout file pointers, then you can do stuff like:

stats < input_file > output_file

Where stats is your program. If you're using Linux, you can chain your program to tee and do exactly what you're trying to do (ie output to console and file) but with no extra code.

How I would write it:

#include <stdlib.h>
#include <stdio.h>
#include "statistics.h"

void printStats(const int count, const double mean, const double ssdev);

int main() {
    int count = 0;
    double numVal, sum = 0.0, sumsq = 0.0;
    double calculatedMean, calculatedStdDev;

    while (scanf("%lf", &numVal)) {
        sum += numVal;
        sumsq += numVal * numVal;
        count++;
    }

    calculatedMean = mean(sum, count);
    calculatedStdDev = ssdev(sum, sumsq, count);
    printStats(count, calculatedMean, calculatedStdDev);

    return EXIT_SUCCESS;
}

void printStats(const int count, const double mean, const double ssdev) {
    printf("%d Values, Mean = %lf, Sample Standard Deviation = %lf\n",
        count, mean, ssdev);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM