Question:
C programming problem use FILE, loop, and if-else?
2009-03-25 07:31:15 UTC
This is what i have so far and I'm stuck, I don't even know if i even did it right but i gave it a try. I'm a beginner.

Can someone please show how this is done.


Suppose we have a data file that has 5 columns in each row. Write a C program that reads such an input file and prints out col1, col4, and the sum of col1 and col4 into output.txt for the rows whose col1 is less than 5 and col4 is greater than or equal to col5.

For instance, your program should process the following input.txt and generate the corresponding output.txt as follows: Please Click Link:

http://i86.photobucket.com/albums/k1...netic86/C1.jpg

If you want to Contact me : rogext1@yahoo.com

If you can't see it let me know.

-------------------------------------------------------------------------------------------------------------------------------------------------------------

#include

main ()
{
int data file, col1, col 2, col3, col4, col5, output file
file * infp , * outfp ;

infp = fopen("Columns Input.txt"); /*Input of File .txt*/
outfp = fopen("Columns Output.txt"); /*Output of File .txt*/

for (i=1; i<=9; i++)
Fscanf (infp, "%d %d &col1,&col4);

Output file = ( col1 + col4);

fprint (outfp,"%d%d\n", data file, output file);

if (col1 < 5)
?????
???
???

if (col4>=col5)
??
??
???
??
Three answers:
2009-03-25 09:37:28 UTC
You have to tell the system how to open the files:



infp = fopen("Columns Input.txt", "r"); /*Input of File .txt*/

outfp = fopen("Columns Output.txt", "w"); /*Output of File .txt*/





You will have to read in all 5 columns, you can't selectively read 1 and 4.

fscanf (infp, "%d %d %d %d %d", &col1,&col2,&col3,&col4,&col5);



Then to write to the output

fprintf(outfp, "%d + %d = ", col1, col4, col1+col4);



Email if you need more help.
cja
2009-03-25 09:48:28 UTC
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
2016-12-24 13:30:19 UTC
From what i will see, you have right here on an analogous time as loop in getLine(): on an analogous time as (myln) { ... } ; what's the cost of myline, and does it ever replace? No, as myln is a pass, so this situation is often going to evaluate as non-0, hence real. you probable meant to try this: on an analogous time as (!myln.eof) { ... };


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...