mikeboyd941
mikeboyd941

Reputation: 15

File Input/Output on a Mac using Sublime Text and Xcode. C language

So my first question is, how do I do a file input/output program on a Mac using Xcode and terminal?

Second, until I figure that out, would someone mind telling me if this is correct since I am unable to compile and run it currently? I am going to partition my drive this week and throw windows on it but until then I would like to study for my exam.

Here's the Practice problem: Write a program that reads in from the first line an integer n, and thereafter on n subsequent lines two things per line (an int SSN and a float wages-earned). All this is form a file. You must prompt for the filename as a string that is certain to be less than 30 chars long. Open the file, etc., read everything, and keep a running total of all the wages earned, then when reading is complete, prompt for an output filename string, open it and write to it the average value of all the earnings.

Here's my code:

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

FILE *F1, *F2;

void main () {

int SSN, n, i;
float wages, total;
char f1name, f2name;

scanf("%s", &f1name);
F1 = fopen("f1name", "r");

fscanf(F1,"%d", &n);
{

// Reading in the 
for(i=0; i<n; i++)
    {
    fscanf(F1,"%d%f", &SSN, &wages);
    total += wages;
    }

// Scanning in file name and opening it
scanf("%s", &f2name);
F2 = fopen(fname, "w");

// Writing to the file the average of all earnings
fprintf(F2,"%d%f", SSN, total/n);
}

// Closing the file
fclose(F1);
fclose(F2);

}

Upvotes: 0

Views: 1569

Answers (1)

ajay
ajay

Reputation: 9680

f1name and f2name should be arrays of characters to store the file name. You have defined them to be characters and trying to store string in them would invoke undefined behaviour because scanf will do illegal memory access.

Also, the signature of the main function should be either of the following.

int main(void);
int main(int argc, char *argv[]);

You should modify your program to

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

int main(void) {
    // variable name SSN change to lowercase
    int ssn, n, i;
    int retval;  // to save the return value of fscanf
    float wages, total;
    char f1name[30+1], f2name[30+1];

    // define file pointers inside main
    // also change the name to lowercase
    FILE *f1, *f2;

    scanf("%30s", f1name);
    f1 = fopen(f1name, "r");

    // check for error in opening file
    if(f1 == NULL) {
        // print error message to stderr
        perror("error in opening file\n");
        // handle it
    }

    retval = fscanf(f1, "%d", &n);
    if(retval != 1) {
        perror("error in reading from the file\n");
        // handle it
    }

    for(i = 0; i < n; i++) {
        retval = fscanf(f1,"%d%f", &ssn, &wages);
        if(retval != 2) {
            perror("error in reading from the file\n");
            // handle it
        }
        total += wages;
    }

    scanf("%30s", f2name);
    f2 = fopen(f2name, "w");

    // check for error in opening file
    if(f2 == NULL) {
        // print error message to stderr
        perror("error in opening file\n");
        // handle it
    }

    // Writing to the file the average of all earnings
    fprintf(f2,"%d %f", ssn, total / n);

    // Closing the file
    fclose(f1);
    fclose(f2);
    return 0;
}

Upvotes: 1

Related Questions