![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 8
| Reading a Input File and outputing desired information using if-else,loop,and if-else 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;
}
|
| m3rc3n4y is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Code: for each line
print event number
print tasks (you aren't doing this, see, on 15)
print days
print a new line character
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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 |
| m3rc3n4y is offline | |
| | #4 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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 |
| m3rc3n4y is offline | |
| | #6 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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; 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 Quote:
Project completion timetable --------------------------------------------------- Event Num of tasks Max num.of days ------ ------------- --------------- --------------------------------------------------- Total number of days to finish the project: 0 Whats wrong (((((((((((((((((( | |
| m3rc3n4y is offline | |
| | #8 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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);
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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 |
| m3rc3n4y is offline | |
| | #10 |
| Registered User Join Date: Apr 2009
Posts: 8
| by the way thanks for keeping with my posts and the fast repiles |
| m3rc3n4y is offline | |
| | #11 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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;
}
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.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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 |
| m3rc3n4y is offline | |
| | #13 | ||
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Quote:
Quote:
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | ||
| quzah is offline | |
| | #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 |
| m3rc3n4y is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Outputing to a file | warfang | C++ Programming | 17 | 04-24-2007 01:36 AM |
| Outputing Results to a File | SITHDUKE | C++ Programming | 11 | 03-03-2005 07:33 PM |
| File Input | kylesbigdog | C++ Programming | 5 | 04-07-2003 09:42 PM |
| Structure file input | UnknownImage | C Programming | 1 | 11-23-2002 04:19 AM |
| Searching For a Word in a file and outputing a number | bc120 | C Programming | 2 | 01-02-2002 02:05 PM |