Thread: fprintf works in one function but not in the other.

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    23

    fprintf works in one function but not in the other.

    Hi,

    I have a really confusing question. I call fprintf to send some output from my program to a file in my function customer()

    This fprintf statements works fine, however, when i call the same command from a different function teller() i do not get any output into the log file.

    This is my declarations for my functions, followed by the function Customer and the function Teller.

    Code:
    void teller(FILE *fin, FILE *fout, message_buf rbuf, int msqid, int i, int Td, int Tw); 
    
    void customer(FILE *fin, FILE *fout, int msqid, message_buf sbuf, size_t buf_length, int timer);
    Calling of functions

    Code:
    if(i == 0) //using the parent process
    
                {
    
                     customer(fptr2, fptr1, msqid, sbuf, buf_length, atoi(argv[1]));
    
                     exit(1);
    
                }
    
                else //uses the children processes
    
                {
    
                    teller(fptr2, fptr1, rbuf, msqid,i, atoi(argv[5]), atoi(argv[6]));
    
                    exit(1);
    
                }
    customer function

    Code:
    void customer(FILE *fin, FILE *fout, int msqid, message_buf sbuf, size_t buf_length, int timer)
    
    {
    
        //declarations
    
        int j;
    
        time_t now;
    
        char buff[MAX_LEN];
    
     
    
        //read from customer_file, adding customers to queue every TIMER time
    
        while (fgets(buff, MAX_LEN, fin) != NULL)
    
        {
    
           char custAction[MAX_LEN];
    
           char custRef[MAX_LEN];      
    
    
    
           int i=0;
    
           int x=0;
    
    
    
           while((buff[i] != ' ') && (i <= MAX_LEN))
    
           { //read from buffer storing in custRef values up till
    
             //a ' ' is found
    
    
    
             custRef[i] = buff[i];
    
               i++;
    
           }
    
    
    
           i++; //increment i past the space
    
    
    
           while((buff[i] != ' ') && (i <=MAX_LEN) && (buff != '\0'))
    
           { //read the rest of buffer till next space or EOF
    
               custAction[x] = buff[i];
    
              
    
               i++;
    
    	   x++;
    
           }
    
    
    
           //printf("%s\n", buff);
    
           time(&now);
    
           fprintf(fout,"--------------------------------------------------\n");
    
           fprintf(fout,"Customer %s: %s\rArrival time: %s\r", custRef, custAction, ctime(&now));
    
           //fprintf(fout,"--------------------------------------------------\n");
    
          
    
         //Send the customer to the message queue 
    
        (void) strcpy(sbuf.mtext, buff);
    
    
    
        buf_length = strlen(sbuf.mtext) + 1;
    
    
    
            /*
    
         * Send a message.
    
         */
    
        if (msgsnd(0, &sbuf, buf_length, IPC_NOWAIT) < 0) {
    
           printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
    
            perror("msgsnd");
    
            exit(1);
    
        }
    
    
    
       else 
    
          {} //do nothing
    
          
    
           memset(buff, 0, MAX_LEN); //reset buffer
    
           memset(custRef, 0, MAX_LEN); //reset CustRef
    
           sleep(timer); //sleep for timer time (specified in command line)
    
        }
    teller function

    Code:
    void teller(FILE *fin, FILE *fout, message_buf rbuf, int msqid, int i, int Td, int Tw)
    
    {
    
        int e;
    
        char service[MAX_LEN];
    
        char custNum[MAX_LEN];
    
        char custAct[MAX_LEN];
    
        time_t current;
    
        time_t arrival;
    
        int p=0;
    
        int x=0;
    
    
    
        while(1)
    
        {
    
        
    
        /*
    
         * Receive an answer of message type 1.
    
         */
    
        if (msgrcv(0, &rbuf, MSGSZ, 1, 0) < 0) { 
    
            perror("msgrcv");
    
            exit(1);
    
        }
    
    
    
        /*
    
         * Allow teller to service the request.
    
         */
    
        
    
           strcpy(service, rbuf.mtext);
    
    
    
           while((rbuf.mtext[p] != ' ') && (i <= MAX_LEN))
    
           { //read from buffer storing in custNum values up till
    
             //a ' ' is found
    
    
    
             custNum[p] = rbuf.mtext[p];
    
               p++;
    
           }
    
    
    
           p++; //increment i past the space
    
    
    
           while((rbuf.mtext[p] != ' ') && (p <=MAX_LEN) && (rbuf.mtext != '\0'))
    
           { //read the rest of buffer till next space or EOF
    
               custAct[x] = rbuf.mtext[p];
    
              
    
               p++;
    
    	   x++;
    
           }
    
    
    
        fprintf(fout, "Customer statistics for Teller %d\n", getpid());    
    
        fprintf(fout, "Servicing customer %s - Action required %s\n", custNum, custAct);
    
        fprintf(fout, "Customer statistics for Teller %d\n",getpid());
    I'm totally confused on this. And I apologise for the length of the message.

    Matt

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you trying to have two processes write to the same file? It sure looks like it due to your commenting. Also, consider editing your post, to strip out some of those blank lines. There's no need to have:
    Code:
    while((rbuf.mtext[p] != ' ') && (p <=MAX_LEN) && (rbuf.mtext != '\0'))
    
           { //read the rest of buffer till next space or EOF
    
               custAct[x] = rbuf.mtext[p];
    
              
    
               p++;
    
    	   x++;
    
           }
    For our sake at least. Maybe you like it, but when you're saying your post is too long in your eyes, why not take the time to tidy it up?
    Code:
    while((rbuf.mtext[p] != ' ') && (p <=MAX_LEN) && (rbuf.mtext != '\0'))
    { //read the rest of buffer till next space or EOF
        custAct[x] = rbuf.mtext[p];
        p++;
        x++;
    }
    While you're at it, take a moment to fix your indenting. It's horrible.

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

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    23
    I am having 2 processes write to the file.

    If this is not possible then how can I get my results out to my log file. ??

    ps. Sorry bout the look of code, I've been up 48 hours trying to finish this ...

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    If you are in linux and you feel that you like you indentation style and we don't, before you post you can run a program called indent. it workes like so
    Code:
    %indent test.c
    If you don't like the indentation it saves your original as test.c~ It is a good program when you want to look at someone elses code. Look here or if you are in Gentoo
    Code:
    %emerge -u indent
    [EDIT]Sorry I am at school and cannot indent this code[/EDIT]

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    23
    Thanks i'll keep it in mind.

    I still cannot figure out why it won't write to my log file.

    I created an additional file simulation_log_teller, but i cannot write to that either .

    Is it because my child process is executing the statement. Please help me on this i'll at a loss.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your child should be able to write to a file just fine. Take the function in question, put it in a small application by itself, and test to make sure it works correctly. Then merge it back into your main program. But in short, no, you can't make two programs open the same file for writing at the same exact time. They'll have to take turns. One writes and closes, then the other opens and writes.

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

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Here is your code indented. You are missing a } on one file and two } on another
    Code:
    	if(i == 0){
    		customer(fptr2, fptr1, msqid, sbuf, buf_length, atoi(argv[1]));
    		exit(1);
    	}
    	else{
    		teller(fptr2, fptr1, rbuf, msqid,i, atoi(argv[5]), atoi(argv[6]));
    		exit(1);
    	}
    Second one
    Code:
    	if(i == 0){
    		customer(fptr2, fptr1, msqid, sbuf, buf_length, atoi(argv[1]));
    		exit(1);
    	}
    	else{
    		teller(fptr2, fptr1, rbuf, msqid,i, atoi(argv[5]), atoi(argv[6]));
    		exit(1);
    	}
    Third File
    Code:
    void teller(FILE *fin, FILE *fout, message_buf rbuf, int msqid, int i, int Td, int Tw){
    	int e;
    	char service[MAX_LEN];
    	char custNum[MAX_LEN];
    	char custAct[MAX_LEN];
    	time_t current;
    	time_t arrival;
    	int p=0;
    	int x=0;
    	while(1){
    		if (msgrcv(0, &rbuf, MSGSZ, 1, 0) < 0) {
    			perror("msgrcv");
    			exit(1);
    		}
    		strcpy(service, rbuf.mtext);
    		while((rbuf.mtext[p] != ' ') && (i <= MAX_LEN)){
    			custNum[p] = rbuf.mtext[p];
    			p++;
    		}
    		p++;
    		while((rbuf.mtext[p] != ' ') && (p <=MAX_LEN) && (rbuf.mtext != '\0')){
    			custAct[x] = rbuf.mtext[p];
    			p++;
    			x++;
    		}
    		fprintf(fout, "Customer statistics for Teller %d\n", getpid());    
    		fprintf(fout, "Servicing customer %s - Action required %s\n", custNum, custAct);
    		fprintf(fout, "Customer statistics for Teller %d\n",getpid());
    /*MISSING TWO }'S */

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    23
    Missing the }'s because i did'nt copy the entire function.

    Quzah, I ported the teller function to a new program and it works fine,

    However porting it back, it does'nt export to the log file.

    I don't know what is wrong.

    this is the code from the new program i created.

    Code:
    #include <stdio.h>
    
    #include <unistd.h>
    
    #include <sys/types.h>
    
    #include <sys/msg.h>
    
    #include <sys/ipc.h>
    
    
    
    #include <time.h>
      enum
    { SUCCESS, FAIL, MAX_LEN = 81 };
    
    main ()
    {
      
    FILE * log;
      int e;
      
    char service[MAX_LEN];
      
    char custNum[] = "1112";
      
    char custAct[] = "help me";
      char filename3[] = "simulation_log_teller";
      
    int reval = SUCCESS;
      time_t current;
      
    time_t arrival;
      
    int p = 0;
      
    int x = 0;
      
    if ((log = fopen (filename3, "w")) == NULL)
        
    
        {
          
    printf ("Cannot open %s for Writing.\n", filename3);
          
    reval = FAIL;
        
    }
      
    fprintf (log, "Customer statistics for Teller %d\n", getpid ());
      
    fprintf (log, "Servicing customer %s - Action required %s\n", custNum,
    	    custAct);
      
    fprintf (log, "Customer statistics for Teller %d\n", getpid ());
      
    
    
        //simulate processing of services by sleep command
    
        //if(custAct[0] == 'd')
    
        //{
    
        printf ("I'm in the custAct for deposit");
      
    time (&current);
      
    time (&arrival);
      
    printf ("Customer statistics for Teller-%d:\r", getpid ());
      
    printf ("Customer: %d\r", custNum);
      
    printf ("Arrival Time: %s\r", ctime (&current));
      
    printf ("Response Time: %s\n", ctime (&arrival));
      
    fputs (custNum, log);
      
    
        //sleep(Td);
    
        time (&current);
      
    fprintf (log, "Customer statistics for Teller-%d:\r", getpid ());
      
    fprintf (log, "Customer: %d\r", custNum);
      
    fprintf (log, "Arrival Time: %s\r", ctime (&arrival));
      
    fprintf (log, "Completion Time: %s", ctime (&current));
      
    
    
        //}
    
        //else
    
        //{
    
        time (&current);
      
    time (&arrival);
      
    fprintf (log, "Customer statistics for Teller-%d:\r", getpid ());
      
    fprintf (log, "Customer: %d\r", custNum);
      
    fprintf (log, "Arrival Time: %s\r", ctime (&current));
      
    fprintf (log, "Response Time: %s\n", ctime (&arrival));
      
    
    
        //sleep(Tw);
    
        time (&current);
      
    fprintf (log, "Customer statistics for Teller-%d:\r", getpid ());
      
    fprintf (log, "Customer: %d\r", custNum);
      
    fprintf (log, "Arrival Time: %s\r", ctime (&arrival));
      
    fprintf (log, "Completion Time: %s", ctime (&current));
      
    
        //}
    
        
    memset (custNum, 0, MAX_LEN);	//reset custNum
    
      memset (custAct, 0, MAX_LEN);	//reset CustAct
    
      //memset(service, 0, MAX_LEN); //reset service
    
      
    
        //}
    
        exit (0);
    }
    This was indent's output as well.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Change, in your first program, the if statement to this:
    Code:
    if( i != 1 )
    Then, I suspect the other function "won't work", and this one will. This basicly will tell you that your function is fine, but when you try to have two processes write to the same file, it doesn't work. I suspect that your fopen call is actually failing and you're just not paying attention to it. As with the case in your latest example:
    Code:
    if ((log = fopen (filename3, "w")) == NULL)
    {
        printf ("Cannot open %s for Writing.\n", filename3);
        reval = FAIL;
    }
    /* Oh, look, it's failed to open, and we write anyway... */
    fprintf (log, "Customer statistics for Teller %d\n", getpid ());
    fprintf (log, "Servicing customer %s - Action required %s\n", custNum, custAct);
    fprintf (log, "Customer statistics for Teller %d\n", getpid ());
    
    See?

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

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    23
    If that was the problem it would be ok..

    Unfortunately, My fopen is not failing as the files are created, and I am trying to write to an additional file other than the original simulation_log.

    So customer() writes to simulation_log and teller() writes to simulation_log_teller

    This however does'nt seem to happen still..

    changing
    Code:
    i != 1
    in the forking process, does not alter which file is written to.

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    23
    BUGGA!!!

    i found the problem - while(1) infinite loop. .

    i take this out then it works ..

    now the question is can i kill a process after a desired amount of time ??

  12. #12
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    just sleep() for some time, then kill() the process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM