Thread: file pointer and loops

  1. #1
    Registered User
    Join Date
    Oct 2019

    Question file pointer and loops

    I can't seem to get my program to read in my .txt file correctly and apply the loops to sort the information. I'm not just getting wrong answers back, they're all error returns. Can anyone tell what's going on wrong here?

    #include <stdio.h>
    int main(){
        // Initialize variables
        char fileName [20];
        int SIZE, bestRatingID, bestCostID, bestBatteryID, i;
        double bestCost, bestRating, bestBattery;
        // User prompt
        printf ("What is the name of the file?\n");
        scanf ("%s", &fileName);
        // Initialize file pointer
        FILE *ifp = fopen (fileName, "r");
        // Search for file length
        fscanf (ifp, "%d", &SIZE);
        // Initialize variables that rely on SIZE
        int manufacturingID [SIZE];
        double efficiencyRating [SIZE], cost [SIZE], batteryLife [SIZE];
        // Initialize for loop to scan whole document
        for (i = 0; i < SIZE; i++){
        fscanf (ifp, "%d %f %f %f", &manufacturingID [i], &cost [i], &efficiencyRating [i], &batteryLife [i]);
        //For loop to determine best values
        for (i = 0; i < SIZE; i++){
            // if statement for best cost assignment
            bestCost = cost [0];
            if (cost [i] < bestCost){
                bestCost = cost [i];
                bestCostID = manufacturingID [i];
            // if statement for best efficiency rating
            bestRating = efficiencyRating [0];
            if (efficiencyRating [i] > bestRating){
                bestRating = efficiencyRating [i];
                bestRatingID = manufacturingID [i];
            // if statement for best battery life
            bestBattery = batteryLife [0];
            if (batteryLife [i] > bestBattery){
                bestBattery = batteryLife [i];
                bestBatteryID = manufacturingID [i];
        // Final user output
        printf ("\nAttribute \t Best Option \t Best Value \n");
        printf ("Cost \t\t %d \t \t %f \n", bestCostID, bestCost);
        printf ("Efficiency \t %d \t \t %f \n", bestRatingID, bestRating);
        printf ("Battery \t %d \t \t %f \n", bestBatteryID, bestBattery);
        // if statement for when a robot is best in more than one category
        if (bestCostID == bestRatingID || bestCostID == bestBatteryID || bestBatteryID == bestRatingID)
            printf("\nRobot %d has the best rating in 2 or more attributes!\n", manufacturingID [i]);
        // Close file pointer
        fclose (ifp);
    return 0;

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    First, line 20:

    for (i = 0; i < SIZE; i++){
    is missing a closing brace (left curly brace) so the code you've supplied couldn't possibly even compile.

    Second, you may want to review the documentation for scanf().

    /* ... */
    scanf ("%s", &fileName);   // &filename is a char**, but the scanf %s specifier wants a char*
    /*... */
    fscanf (ifp, "%d %f %f %f", &manufacturingID [i], &cost [i], &efficiencyRating [i], &batteryLife [i]); 
        // &cost[i] is a pointer to a double. %f wants a pointer to float. Ditto with the other two pointers you've supplied.
    As an additional idea, it might have been nice to have a test file as well and a statement along the lines of what you expect the program to do compared to what it actually is doing (assuming the program compiles). Narrow down the bug by writing really short programs to try and reproduce the bug that you're experiencing; this can help find out what's going wrong. I think @laserlight has a good signature.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer with for loops
    By bab27 in forum C Programming
    Replies: 5
    Last Post: 10-02-2019, 02:29 PM
  2. Replies: 5
    Last Post: 10-12-2012, 03:18 PM
  3. True Beginner - Pointer/Array For loops
    By KyussRyn in forum C Programming
    Replies: 7
    Last Post: 03-04-2009, 03:53 AM
  4. Help with a code using File I/O and Loops
    By bns1201 in forum C Programming
    Replies: 27
    Last Post: 10-17-2008, 09:19 PM
  5. loops and I/O file.
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2001, 09:41 AM

Tags for this Thread