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.
fgetsor POSIXgetlineand 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...)for (ptimes[0]; ptimes[jobs] ; ptimes[jobs++])?forloop 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 incrementingjobsto 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 aforloop.for (int i = 0; i < 5; ++i)would be a for-loop that iterates five times as it starts withiinitialized to zero, and keeps looping as long asiis less than 5. As it incrementsiwith each iteration, it will loop 5 times asiequals 0 through 4.