1

So I'm supposed to do basically convert what the user wrote into a text document. However, the amount of lines will vary. It will always be 3 columns.

So this is an example instance of the program running:

How many jobs?: 4
Enter processing time for each: 10 13 12
20 39 10
7 29 13
13 18 19

Since the user inputs '4' for number of jobs. There were 4 different lines where the user could input. I want to save this input so I can make a text document of this once the program finishes.

So here's the part of my code that does this:

int jobs;
printf("How many jobs?: ");
scanf("%d", &jobs);

int ptimes[jobs];
int n = 0;
printf("Enter processing time for each: ");

for (ptimes[0]; ptimes[jobs] ; ptimes[jobs++]) {
     scanf("%d", ptimes[n]);
     n = n + 1;

    }

The problem I have is I can't store each number individually in the line. Also, I can't do 3 columns. Also, this code doesn't work properly for me. I tested by adding a for loop with the same conditions but printing each variable in ptimes, but it didn't work.

6
  • Read each line of input into a character buffer and then write the buffer out to the file. No need to read as integers, just read the line with, e.g. fgets or POSIX getline and then write it back out to the file (both line-oriented function read-and-include the trailing '\n' in the buffer so that is already taken care of...) Commented Oct 9, 2018 at 4:44
  • 2
    I think you need to brush up on exactly how a for-loop works. Can you explain the purpose of each thing in for (ptimes[0]; ptimes[jobs] ; ptimes[jobs++])? Commented Oct 9, 2018 at 4:44
  • @ChristianGibbons new to C, I'm familiar with how they work in python so I tried to do something similar. If 'jobs' was 3, ptimes[jobs] should've created ptimes values for [0-3] if I'm correct. So for the for loop, I wanted it to run from ptimes[0] to the last value which would be ptimes [jobs]. Commented Oct 9, 2018 at 4:50
  • The for loop makes no sense. The initializer portion just evaluates an undefined array entry, then discards it, so it serves no purpose. The exit test indexes beyond the array bounds to a value that is not only undefined, but outside of the array. Whether it's true or false is unpredictable. And the increment is incrementing jobs to be even further outside of the array bounds, then using it as an index even further outside of the array bounds (again, undefined behavior), then discarding the result. Nothing in that lines makes any sense, other than the fact that it's a for loop. Commented Oct 9, 2018 at 4:52
  • In C, the way a for-loop works is the first part is initialization, the second is condition, and third is what you do when you reach the end of each iteration (usually an increment or something of the sort). So for example for (int i = 0; i < 5; ++i) would be a for-loop that iterates five times as it starts with i initialized to zero, and keeps looping as long as i is less than 5. As it increments i with each iteration, it will loop 5 times as i equals 0 through 4. Commented Oct 9, 2018 at 4:53

1 Answer 1

2

Despite your for loop making no sense at all, you are approaching the problem in a very awkward way. When your goal is read a line of data, then you should be thinking about using a line-oriented input function and not reading individual integers.

For instance if the user wants to enter data for 4-jobs, then you can either read and validate 3 integers per-job, or simply require each job data to be entered on a single line and then read the line and write the line to a file.

You do this very simply by providing a buffer of sufficient size to hold all the anticipated characters the user will enter on each line (don't skimp on buffer size). Then simply read each line with one of the line-oriented input functions like fgets or POSIX getline and then write the line back out to your output file using fputs (fgets and fputs make a good combination here)

For example, to minimally handle your circumstance, you could do:

#include <stdio.h>

#define MAXC  1024
#define OFILE "jobs.txt"

int main (void) {

    char buf[MAXC];
    int nlines = 0, n = 0;
    FILE *fp = fopen (OFILE, "w");

    if (!fp) {  /* validate file is open for writing */
        perror ("fopen-file open failed");
        return 1;
    }

    printf ("How many Jobs? ");
    if (scanf ("%d", &nlines) != 1) {
        fputs ("error: invalid integer input.\n", stderr);
        return 1;
    }
    fgets (buf, MAXC, stdin);   /* read and discard trailing '\n' */

    while (n < nlines && fgets (buf, MAXC, stdin)) { /* read lines */
        fputs (buf, fp);    /* write lines  */
        n++;                /* update count */
    }
    if (fclose(fp) == EOF)
        perror ("fclose-stream error");

    return 0;
}

Example Use/Output

$ ./bin/fgets_fputs
How many Jobs? 4
10 13 12
20 39 10
7 29 13
13 18 19

Example Output File

$ cat jobs.txt
10 13 12
20 39 10
7 29 13
13 18 19

Now beyond simply reading each line of data entered by the user, it would be wise to validate that each contained 3-integers before writing to the file, and if not providing an error and require the user to re-input the data. It takes little more effort, e.g.

    /* no changes above */
    ...
    fgets (buf, MAXC, stdin);   /* read and discard trailing '\n' */

    printf (" enter data for job[%2d]: ", n + 1);   /* prompt for data */
    while (fgets (buf, MAXC, stdin)) { /* read lines */
        int i1, i2, i3;
        /* validate 3-integers provided */
        if (sscanf (buf, "%d %d %d", &i1, &i2, &i3) == 3) {
            fputs (buf, fp);    /* write lines  */
            n++;                /* update count */
        }
        else    /* otherwise, handle error */
            fputs ("  error: invalid input.\n", stderr);

        if (n < nlines) /* if lines remain to read, prompt */
            printf (" enter data for job[%2d]: ", n + 1);
        else    /* otherwise break the loop */
            break;
    }
    ...
    /* no changes below */

Example Use/Output

$ ./bin/fgets_fputs
How many Jobs? 4
 enter data for job[ 1]: 10 13 12
 enter data for job[ 2]: 20 39
  error: invalid input.
 enter data for job[ 2]: 20 39 10
 enter data for job[ 3]: 7 29 13
 enter data for job[ 4]: 13 18 xjf;slfj (cat steps on keyboard)
  error: invalid input.
 enter data for job[ 4]: 13 18 19

The output file is the same.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.