As you must have realized, there are many problems with what you have so far. Syntax and missing code aside, the first issue is reading the numbers from the file. Your fscanf call would read just 2 numbers, if there are 2. You need to read all numbers on each line, because you need to use the 1st, 4th and 5th.
See below for how I did it. I generalized the problem, so that by changing #define N it can work for any N. I have N=5 according to your problem statement. You'll also see that I made sure my code doesn't require an input file in a specific format. You'd be tempted to code it to handle only input with 5 rows of 5 numbers each, as required by this assignment, but it's not good to hardcode such dimensions and limit the usefulness of your code. Also, you might be tempted to omit validation of input, thus making your program subject to misbehavior (e.g., crashing) if the expected input is not received.
I'll admit that In my relatively short program there's a lot for a beginner to digest. Anything you don't recognize can be found in a C book or online reference sites. I suggest you study and understand what I've done, you'll certainly learn something in the process. Your solution will likely be somewhere in between what you have now, and what my code looks like.
/*
* Read the specified input text file that contains numbers. For
* each line with at least N numbers, where the 1st number is
* less than N, and (N-1)th number is greater than or equal to
* the Nth number, print the following to the specified output
* file : 1st and (N-1)th numbers, sum of 1st and (N-1)th numbers.
*/
#include
#include
#include
#include
#define MAX_STR_LEN 1024
#define WHITESPACE " ,\t"
#define N 5
FILE *openFile(const char *prompt, const char *mode);
int main(int argc, char *argv[]) {
char numstr[MAX_STR_LEN],*s;
double x,row[N];
int n;
FILE *in, *out;
in = openFile("Enter input file name: ","r");
if (in != NULL) {
out = openFile("Enter output file name: ","w");
if (out == NULL) {
fclose(in);
exit(EXIT_FAILURE);
}
} else {
exit(EXIT_FAILURE);
}
while ((s = fgets(numstr,MAX_STR_LEN,in)) != NULL) {
*(strchr(numstr,'\n')) = '\0';
if (strlen(s) > 0) {
n = 0;
while (s != (char *)NULL) {
if ((sscanf(s,"%lf",&x) == 1) && (n < N)) {
row[n++] = x;
}
if ((s = strpbrk(s,WHITESPACE)) != (char *)NULL) {
s = &s[strspn(s,WHITESPACE)];
}
}
}
if ((n >= N) &&
(row[0] < N) && (row[N-2] >= row[N-1])) {
fprintf(out,"%g %g %g\n",
row[0], row[N-2], row[0]+row[N-2]);
}
}
fclose(in);
fclose(out);
return 0;
}
FILE *openFile(const char *prompt, const char *mode) {
FILE *fp = NULL;
char fileName[MAX_STR_LEN];
printf("%s",prompt);
fgets(fileName,MAX_STR_LEN,stdin);
*(strchr(fileName,'\n')) = '\0';
if ((fp = fopen(fileName,mode)) == NULL) {
printf("\nerror opening %s\n",fileName);
}
return fp;
}
Sample input file:
$ cat nums5.txt
1 2 3 4 5
2 3 4 6 5
3 4 5 6
4
x y z
5.5 6.6 7.7 9.9 8.8
4.9, 5.9, 6.9, 9.9, 8.9
4 3 2 5 3 11 22 33 44
Sample run:
Enter input file name: nums5.txt
Enter output file name: nums5.out
Output file:
$ cat nums5.out
2 6 8
4.9 9.9 14.8
4 5 9