Reading a Input File and outputing desired information using if-else,loop,and if-else

This is a discussion on Reading a Input File and outputing desired information using if-else,loop,and if-else within the C Programming forums, part of the General Programming Boards category; So basically i think i have the roots of this program down im getting an output.txt file but its not ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Reading a Input File and outputing desired information using if-else,loop,and if-else

    So basically i think i have the roots of this program down im getting an output.txt file but its not what im wanting here is the assignment.

    Critical Path Analysis. A critical path analysis is a technique used to determine the time schedule for a project. This information is important in the planning stages before a project is begun, and it is also useful to evaluate the progress of a project that is partially completed. One method for this analysis starts by dividing a project into sequential events and then dividing each event into various tasks. Although one event must be completed before the next one is started, various tasks within an event can occur simultaneously. The time it takes to complete an event, therefore, depends on the number of days required to finish its longest task. Similarly, the total time it takes to finish a project is the sum of time it takes to finish each event.

    Assume that the critical path information for a major construction project has been stored in a data file. Each line of the data file contains an event number, a task number, and the number of days required to complete the task. The data have been stored such that all the task data for the first event are followed by all the task data for the second event, and so on. A typical set of data is shown in the following table.

    Event__Task___Number of Days

    1______15_____3
    1______27_____6
    1______36_____4
    2______15_____5
    3______18_____4
    3______26_____1
    4______15_____2
    4______26_____7
    4______27_____7
    5______16_____4


    Write a program to read the critical path information from critical1.txt file and to print a project completion timetable into output1.txt.
    Timetable lists each event number, number of tasks under this event, the maximum number of days for this event, and the total number of days for the project completion.

    For example, your output1.txt will look like as follows based on the above input file:

    Project completion timetable
    ---------------------------------------------------------------
    Event____Num of tasks_____Max num.of days
    ------ ----------------- ----------------------
    1__________3_________________6
    2__________1_________________5
    3__________2_________________4
    4__________3_________________7
    5__________1_________________4
    ---------------------------------------------------------------
    Total number of days to finish the project: 26 days


    My Output:
    Project completion timetable
    ---------------------------------------------------
    Event Num of tasks Max num.of days
    ------ ------------- ----------------------
    ---------------------------------------------------
    Total number of days to finish the project: 0


    As you can see it is not doing what its supposed to im guessing this is somthing wrong with my loops or i am not using fprintf right.

    Please take a look at my code and give me some suggestions.

    Thanks a lot




    Code:
    #include <stdio.h>
    
    int main(void)
    {
        FILE *infp, *outfp;
        int event, task, days, partevent, maxday, totday;
        
        if ((infp = fopen("critical1.txt", "r"))==NULL)
        {
          printf("Input file cannot be opened\n");       
          return -1;
       }
       if ((outfp = fopen("output1.txt", "w"))==NULL)
       {
          printf("Output file cannot be opened\n");         
          return -1;
       }
       fprintf(outfp, "Project completion timetable\n");
       fprintf(outfp, "---------------------------------------------------\n");
       fprintf(outfp, "Event    Num of tasks    Max num.of days\n");
       fprintf(outfp, " ------    -------------   ----------------------\n");
        
        partevent=-1;
        totday=0;        
       while(fscanf(infp, "%d %d %d",&event, &task, &days)==3)
       {
          if(partevent!=event)
          {
                if(partevent!=-1)
                {
                fprintf(outfp,"%d \t %d \t\t\t %d\n", partevent, task, maxday);
                totday +=maxday;
                }
          task=1;
          maxday=days;
          partevent=event;
          }
          else
          {
              task++;
              if(maxday<days)
              {
                      maxday=days;
              }
          }
       }
       if(partevent!=-1)
       {
                fprintf(outfp,"%d \t %d \t\t\t %d\n", partevent, task, maxday);
                totday +=maxday;
       }
       fprintf(outfp, "---------------------------------------------------\n");
       fprintf(outfp,"Total number of days to finish the project: %d\n", totday);
    fclose(infp);
    fclose(outfp);
       return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for each line
        print event number
        print tasks (you aren't doing this, see, on 15)
        print days
        print a new line character
    Just break what you are trying to do down into simple steps. It's easy.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Sorry i really dont understand what you mean

    Am i printing things wrong?

    Are my loops wrong?

    Whats the specific problem?

    Thanks again,
    m3rc

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I can't teach you to think for yourself. Look at your input. Look at your output. Is everything in the right spot? If it isn't, what isn't?

    I could write the program for you, but that won't make you any smarter.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    I understand you are trying to make me think for myself, but to be frank i have. I've been working on this for the last 2 and a half days when it should only be a 30 min program. I appreciate you going through the teacher student thing here but im about ready to pull my hair out. I've done everything in my power to figure this out but I just cant. I'm sure its a matter of moving this thing there and that thing here, I just dont see it.

    This is probably wrong of me to ask but can you please tell me what needs to be done or correct the code for me. I understand this is a forum for help, however i feel like i have done most of the work/thinking here. It's just a matter of moving things here and there maybe changing a couple things. This project is due soon and I'm stressing out here

    So please if its not to much just give me the solution ; )

    p.s. I know the above statement seems terrible. I would not have asked that if i truly have not tried everything in my power to figure out the solution to get the output to print right.

    Thanks,
    m3rc

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's what you want:

    1______15_____3
    1______27_____6

    You want column 1 to be Event, so print the event number.
    You want column 2 to be Tasks, so print the tasks next.
    You want column 3 to be Days, so print that next.
    Now print the end of line, so everything starts on the next line.

    You are getting:

    1__________3_________________6
    2__________1_________________5

    Review my above example, and try to write a simple example that does that. Print three columns, then a newline, for as many lines as you feel like.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    I assumed this was doing exactly that
    Code:
    fprintf(outfp,"%d \t %d \t\t\t %d\n", partevent, task, maxday);
    totday +=maxday;
    "the /t repressing the spaces in between the columns

    prevevent = days
    task= tasks
    maxday= the days.

    So that is like you said :

    You want column 1 to be Event, so print the event number.
    You want column 2 to be Tasks, so print the tasks next.
    You want column 3 to be Days, so print that next.
    Now print the end of line, so everything starts on the next line



    You are getting:

    1__________3_________________6
    2__________1_________________5
    The only thing that is in my output file is this


    Project completion timetable

    ---------------------------------------------------
    Event Num of tasks Max num.of days
    ------ ------------- ---------------
    ---------------------------------------------------

    Total number of days to finish the project: 0


    Whats wrong ((((((((((((((((((

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
      while(fscanf(infp, "%d %d %d",&event, &task, &days)==3)
       {
          if(partevent!=event)
          {
                if(partevent!=-1)
                {
                fprintf(outfp,"%d \t %d \t\t\t %d\n", partevent, task, maxday);
    Well, for starters, you're reading into event, but you're checking partevent.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    That didnt change anything

    I had a friend try this exact code and he said it worked for him

    I just got done emailing this to a friend and he checked it and said it was working maybe im doing to imput file wrong

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    by the way thanks for keeping with my posts and the fast repiles

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdio.h>
    int main( void )
    {
        FILE *fpi, *fpo;
        char buf[ BUFSIZ ] = {0};
        int d1, d2, d3;
        
        fpi = fopen( "threein.dat", "r" );
        fpo = fopen( "threeout.dat", "w" );
    
        while( fgets( buf, BUFSIZ, fpi ) && sscanf( buf, "%d %d %d", &d1, &d2, &d3 ) == 3 )
        {
            printf( "%d %d %d\n", d1, d2, d3 );
            fprintf( fpo, "%d %d %d\n", d1, d2, d3 );
        }
        
        fclose( fpi );
        fclose( fpo );
        
        return 0;
    }
    Sans error checking. Open two files, one for reading, one for writing. Read a line of three variables from the first, display it to the screen, write it to the output file. The input file looks like:

    1 2 3
    2 3 4
    3 4 5

    When stuck, it's often best to write the smallest simple version of what you're trying to do, and test it that way.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    I think i've given up I have no idea what your talking about in the last post and that has nothing to do with my code...


    Is it beyond you to just correct my code so that the program prints the desired output? Im on the edge here.

    Im an engineering student not a computer science major and trying to make it through here can you just give me a break and cut to it?

    Thanks,
    m3rc

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by m3rc3n4y View Post
    I think i've given up I have no idea what your talking about in the last post and that has nothing to do with my code...
    You clearly have no idea what your code does, or what your assignment is then. My code was a simple example for you so that you could make sure you were simply reading and writing correctly. Once you know you've done that, you move onto the next step.
    Quote Originally Posted by m3rc3n4y View Post
    Is it beyond you to just correct my code so that the program prints the desired output? Im on the edge here.
    I don't care that you're on edge. It's not beyond my ability to do all your work for you, it's beyond my desire. I've helped you as much as I'm going to.
    Quote Originally Posted by m3rc3n4y View Post
    Im an engineering student not a computer science major
    That's funny, I could have sworn basic logic and troubleshooting abilities would be useful for an engineer to learn.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Lmao thanks for trying to help me but i finally found out why i was not getting an output. You want to know why?

    My input file looked like this

    Event__Task___Number of Days

    1______15_____3
    1______27_____6
    1______36_____4
    2______15_____5
    3______18_____4
    3______26_____1
    4______15_____2
    4______26_____7
    4______27_____7
    5______16_____4


    When it should look like this



    1______15_____3
    1______27_____6
    1______36_____4
    2______15_____5
    3______18_____4
    3______26_____1
    4______15_____2
    4______26_____7
    4______27_____7
    5______16_____4

    My loop was reading the 3 words in the columns there not the numbers. Thats all i need SO

    Maybe that explains why i didnt understand what you where talking about.

    I still don't understand what you where trying to tell me though because instead of using something relevant to the program you make up something on your own

    Thanks anyways

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outputing to a file
    By warfang in forum C++ Programming
    Replies: 17
    Last Post: 04-24-2007, 01:36 AM
  2. Outputing Results to a File
    By SITHDUKE in forum C++ Programming
    Replies: 11
    Last Post: 03-03-2005, 06:33 PM
  3. File Input
    By kylesbigdog in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2003, 09:42 PM
  4. Structure file input
    By UnknownImage in forum C Programming
    Replies: 1
    Last Post: 11-23-2002, 03:19 AM
  5. Replies: 2
    Last Post: 01-02-2002, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21