Thread: Data file problem use FILE, loop, and if-else

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question Data file problem use FILE, loop, and if-else

    This is what i have so far and I don't know if its right and i don't know how to finish it.
    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 can't see it let me know.

    Code:
    #include <stdio.h>
    
    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)
         ??
         ??
         ???
         ??

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Break the problem down to it's basics:

    1) Input: You need to input all the data. Can you get the data input into variables like col1, col2, col3, col4, etc. We need one variable for each column, only.

    A while loop might be good for opening and reading the file.

    2) Processing: An if statement or two, and this is done, no problem

    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.
    Code:
    if((col1 < 5) && (col4 >= col5))  
    {
       //print out col1, col4, and (col1 + col4)
    }
    3) Output: Here, you'll want to probably use fprintf()

    Code:
    fprintf("%d %d %d", col1, col4, (col1 + col4));
    That should help get you going.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question

    I have two different codes because i tried it 2 different ways i think. I have some examples with me and i just tried to follow thats how i did it, i know i probably did a mess but i tried it. I'm a beginner.
    I still kind of lost and all help will appreciated.


    1
    Code:
    #include <stdio.h>
    
    main ()
    {
         int col1, col 2, col3, col4, col5,  
         file * infp , * outfp ;
         
         infp = fopen("Columns Input.txt", "r"); /*Input of File .txt*/
         outfp = fopen("Columns Output.txt", "w"); /*Output of File .txt*/ 
         
         for (i=1; i<=count; i++)
         fscanf (infp, "%d %d %d %d %d", &col1,&col2,&col3,&col4,&col5);
         
         if((col1 < 5) && (col4 >= col5))  
    {
         fprintf(outfp, "%d + %d = ", col1, col4, col1+col4);
         
         
         
         
         
         
         
         fclose(infp);
         fclose(outfp);
        
        
        return 0;
         
         }

    2
    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main(void)
    {
        FILE *infp, *outfp;
        
        int col1, col2, col3, col4, col5, i, count;
        if ((infp = fopen("input.txt", "r"))==NULL{
                  printf("input file cannot be opened\n");
                  return -1;
                   }
    
        if ((outfp = fopen("output.txt", "w"))==NULL){
                   printf("output file cannot be opened\n");
                   return -1;
                   }
          
        for (i=1; i<=count; i++)
         fscanf (infp, "%d %d %d %d %d", &col1,&col2,&col3,&col4,&col5);
         
        if((col1 < 5) && (col4 >= col5))
         {
         fprintf(outfp, "%d + %d = ", col1, col4, col1+col4);
         
         
         
         fclose(infp);
         fclose(outfp);
        
        
        return 0;
         
         }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you havee problem with indentation wich leads to another problem you will see as sonn as indentation will be fixed

    and you fprintf format has only two %d while you are trying to print 3 numbers
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed