Thread: strange Output

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11

    strange Output

    I have a program reading from a txt file and then printing what was in the text file.

    For some reason, part of the output is different than the input. I cannot figure out why.

    This was working until I added stuff that I thought was unrelated. I'll post all of the code but i believe the problem is in main().

    Can any one offer an explanation? Thanks you.

    Input:
    PS1 90001 100 90500 TXT

    PS2 90010 50 90400 TXT

    PS3 90011 40 90520 FP

    PS4 90050 300 91000 FP

    PS5 90065 150 90765 FP

    PS6 90070 500 90170 FP

    PS7 90080 100 91100 TXT

    PS8 90090 150 90690 TXT

    PS9 90167 130 91500 TXT

    PS10 90170 500 93500 FP

    PS11 90172 50 90900 FP

    PS12 90180 105 92190 TXT

    PS13 90190 99 93200 TXT

    PS14 90195 475 91300 FP

    PS15 90200 100 91900 TXT

    PS16 91000 130 93100 FP

    PS17 91001 150 93200 MM

    PS18 91002 350 93800 MM

    PS19 91050 399 93999 MM

    PS20 91100 300 94000 TXT

    PS21 91110 100 91900 MM

    PS22 91215 500 92125 MM

    PS23 91235 150 92500 MM

    PS24 91240 300 92600 FP

    PS25 91320 500 93700 FP

    PS26 91350 700 94500 TXT

    PS27 91630 170 92630 MM

    PS28 91999 119 93999 TXT

    PS29 92001 240 94001 FP

    PS30 92019 191 94900 FP

    PS31 92201 110 94950 FP

    PS32 92299 299 95000 TXT

    PS33 92301 199 95010 FP

    PS34 92319 100 95700 MM

    PS35 93001 200 96000 TXT

    Code:
    #include <stdio.h>
    #include <malloc.h>
    //////////////////////////////////////////////////////////////////////////
    //
    //	Process Structure, Global Variables, and Functions
    //
    //////////////////////////////////////////////////////////////////////////
    
    const unsigned int TRUE = 1;
    const unsigned int FALSE = 0;
    
    struct process {
    	char 		name[5];
    	char 		type[3];
    	unsigned int 	arrival;
    	unsigned int 	length;
    	unsigned int 	timeLeft;
    	unsigned int 	deadline;
    	unsigned int	startTime;
    	unsigned int 	running;	// Boolean: Is the process currently running?
    	unsigned int 	expired;	// Boolean: Is the process past its deadline?
    	unsigned int	hasArrived;	// Boolean: Has the process arrived yet?
    };
    struct process SP[35];	// an array of processes that have not arrived yet
    unsigned int 		time 		= 90000;
    const unsigned int 	QUANTUM_LENGTH 	= 100;
    unsigned int		quantum;
    
    void tick()
    // represents a single millisecond tick of the system time
    {
    	quantum--;
    	time++;
    }
    
    /////////////////////////////////////////////////////////////////////////
    //
    //	Doubly Linked List
    //
    /////////////////////////////////////////////////////////////////////////
    typedef struct process ITEMTYPE;
    
    struct node {
    	ITEMTYPE 	item;			// Member with the ITEMTYPE type
    	struct node 	*next;			// Pointer to next node
    	struct node 	*prev;			// Pointer to previous node
    };
    struct node * AddElement( struct node *h , ITEMTYPE p)
    {
    	struct node *tmp;
    
    	if(h == NULL)
    	// if head is NULL, initialize the list
    	{
    		h = (struct node *)malloc(sizeof(struct node));
    		h->item = p;
    		h->next = NULL;
    		h->prev = NULL;
    		return h;
    	}
    	else if(h->next == NULL && h->prev == NULL) // List is empty
    	{
    		h->next = (struct node *)malloc(sizeof(struct node));
    		h->next->item = p;
    		h->next->next = h;
    		h->prev = h->next;
    		h->next->prev = h;
    		return h;
    	}
    	else
    	{
    		tmp = (struct node *)malloc(sizeof(struct node));
    		tmp->item = p;
    		tmp->next = h;
    		tmp->prev = h->prev;
    		h->prev->next = tmp;
    		h->prev = tmp;
    		return h;
    	}
    	return 0;
    }
    void PrintQueue( struct node *h , int format)
    // format == 1 prints the info for the snapshot of jobs that have been implemented
    // format == 2 prints the info for the snapshot of jobs that missed their deadline
    {
    	struct node *current = h;
    	struct node *tmp = h;
    	while(current!=NULL)
    	{
    		if(format == 1)
    			printf("%s,%u,%u,%u,%u,%s\n", current->item.name, current->item.length, current->item.arrival,  current->item.startTime, current->item.timeLeft, current->item.type);
    		else if(format == 2)
    			printf("%s,%u,%u,%u,%u\n", current->item.name, current->item.length, current->item.arrival,  current->item.startTime, current->item.deadline);
    		current=current->next;
    		if(current == h)
    			return;	
    	}
    	printf("\n");
    	return;
    }
    
    
    
    
    //////////////////////////////////////////////////////////////////////////
    //
    //	MAIN()
    //
    //////////////////////////////////////////////////////////////////////////
    int main()
    {
    	// Read All Processes in from "input_2011.txt"
    	FILE *fr;	// file pointer
    	fr = fopen ("input_2011.txt", "rt");  // open the file for reading
    	if (fr == NULL) perror("Error Opening File.");
    	else {
    		int i = 0;
    		for (i = 0; i < 35; i++)
    		{
    			fscanf(fr,"%s\t%u\t%u\t%u\t%s\t", SP[i].name, &SP[i].arrival, &SP[i].length, &SP[i].deadline, SP[i].type);
    			printf("%s,%u,%u,%u,%s\n", SP[i].name, SP[i].arrival,  SP[i].length, SP[i].deadline, SP[i].type);
    		}
    		// Initialize the rest of the processes:
    		for (i = 0; i< 35; i++)
    		{
    			SP[i].timeLeft	= SP[i].length;
    			SP[i].startTime	= 0;
    			SP[i].running 	= FALSE;
    			SP[i].expired	= FALSE;
    			SP[i].hasArrived= FALSE;
    		}
    	}
    	fclose(fr);
    
    	// Create queues for finished jobs:
    	struct node *complete_head	= NULL;
    	struct node *pastDeadline_head	= NULL;
    	// Create queue for each job type:
    	struct node *MM_head	= NULL;
    	struct node *TXT_head	= NULL;
    	struct node *FP_head	= NULL;
    
    	quantum = QUANTUM_LENGTH; // set time quantum
    
    	while(time < 100000)
    	{
    
    		tick();
    	}
    
    	return 0;
    }

    Output:
    alan@alan-laptop:~$ gcc scheduling.c
    alan@alan-laptop:~$ ./a.out
    PS1,89856,100,90500,TXT
    PS2,89856,50,90400,TXT
    PS3,90011,40,90520,FP
    PS4,90050,300,91000,FP
    PS5,90065,150,90765,FP
    PS6,90070,500,90170,FP
    PS7,89856,100,91100,TXT
    PS8,89856,150,90690,TXT
    PS9,90112,130,91500,TXT
    PS10,90170,500,93500,FP
    PS11,90172,50,90900,FP
    PS12,90112,105,92190,TXT
    PS13,90112,99,93200,TXT
    PS14,90195,475,91300,FP
    PS15,90112,100,91900,TXT
    PS16,91000,130,93100,FP
    PS17,91001,150,93200,MM
    PS18,91002,350,93800,MM
    PS19,91050,399,93999,MM
    PS20,90880,300,94000,TXT
    PS21,91110,100,91900,MM
    PS22,91215,500,92125,MM
    PS23,91235,150,92500,MM
    PS24,91240,300,92600,FP
    PS25,91320,500,93700,FP
    PS26,91136,700,94500,TXT
    PS27,91630,170,92630,MM
    PS28,91904,119,93999,TXT
    PS29,92001,240,94001,FP
    PS30,92019,191,94900,FP
    PS31,92201,110,94950,FP
    PS32,92160,299,95000,TXT
    PS33,92301,199,95010,FP
    PS34,92319,100,95700,MM
    PS35,92928,200,96000,TXT
    alan@alan-laptop:~$

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Would you care to help us out and point out the differences in the output? As opposed to what you expect?

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Were you not expecting the commas or...? You can just replace them with spaces in your printf() call.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11
    I am just reading in, and then printing out each line. But the second number in some of the outputs is different.

    The first of input is: PS1 90001 100 90500 TXT
    and the first output is: PS1,89856,100,90500,TXT

    I have no idea why it is different.

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    What was the stuff you added that you thought was unrelated?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't matter, and no, I don't know why it's doing it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct process {
    	char 		name[5];
    	char 		type[3];
    	unsigned int 	arrival;
    	unsigned int 	length;
    	unsigned int 	timeLeft;
    	unsigned int 	deadline;
    	unsigned int	startTime;
    	unsigned int 	running;	// Boolean: Is the process currently running?
    	unsigned int 	expired;	// Boolean: Is the process past its deadline?
    	unsigned int	hasArrived;	// Boolean: Has the process arrived yet?
    };
    struct process SP[35];	// an array of processes that have not arrived yet
    
    int main()
    {
    	// Read All Processes in from "input_2011.txt"
    	FILE *fr;	// file pointer
    	fr = fopen ("awrschedule.txt", "rt");  // open the file for reading
    	if (fr == NULL) perror("Error Opening File.");
    	else {
    		int i = 0;
    		for (i = 0; i < 35; i++)
    		{
                char buf[ BUFSIZ ] = {0};
                fgets( buf, BUFSIZ, fr );
                printf( "> %s", buf );
    			/* fscanf(fr,"%s\t%u\t%u\t%u\t%s\t", SP[i].name, &SP[i].arrival, &SP[i].length, &SP[i].deadline, SP[i].type); */
                sscanf( buf,"%s\t%u\t%u\t%u\t%s\t", SP[i].name, &SP[i].arrival, &SP[i].length, &SP[i].deadline, SP[i].type);
    			printf("%s,%u,%u,%u,%s\n", SP[i].name, SP[i].arrival,  SP[i].length, SP[i].deadline, SP[i].type);
    		}
        }
    	fclose(fr);
    
    	return 0;
    }
    It's fine when you scan it in, because I get the right line from the buffer if I immediately print it out. Something breaks in the conversion when you call *scanf on it.

    Edit - I'm not seeing it. It's not a memory thing. It doesn't matter if you start the array at a different point. It's something weird when it converts 90001 and 90010 to %u. I don't know why 90011 works and those two don't. Maybe I'm too tired to see it. I'll let someone else figure it out.

    Quzah.
    Last edited by quzah; 03-24-2011 at 08:58 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I've got it.

    If SP[i].type is "TXT", you don't have enough room to store it and the terminating '\0'. My guess is that SP[i].arrival is stored right next to SP[i].type in memory and some of it gets overwritten if SP[i].type is too long.
    Last edited by TheBigH; 03-24-2011 at 08:59 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nice catch. Changing 3 to a 4 here fixes it.
    Code:
    struct process {
    	char 		name[5];
    	char 		type[4];

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

  9. #9
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11
    Thank you, guys! This is a really great forum; a lot better than some other forums I have been on.

    Btw, It stopped working when I added more variables to the process structure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working through H&K... Strange Output
    By Ghiofish in forum C Programming
    Replies: 12
    Last Post: 10-07-2009, 07:46 AM
  2. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  3. Binary Search - Strange Output
    By mike_g in forum C Programming
    Replies: 13
    Last Post: 06-16-2007, 02:55 PM
  4. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  5. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM