Thread: Need help in reading data

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Need help in reading data

    Hello

    Say in a buffer the data 12345678 are stored I want to be able to read 1234 every 1 second. How will I go about doing that as I never did this sort of thing before I have no idea of where to start.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *mumbles something about not having enough time*
    *mumbles something about not having enough time*
    *mumbles something about not having enough time*
    *mumbles something about not having enough time*


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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You didn't mention your OS or compiler, HAssan. That would help.

    You need to understand how to put in a delay, and then figure out how you want to handle the digits (or numbers) in the buffer.

    My questions are:

    How will the program get the address of the buffer?

    What if the first number is 56? Is that always going to be treated as a 5 and a 6, or could that 5 and 6, really be a fifty-six?

    The program should be easy, but your description is too brief.

    Just to give you some idea's for working with time in your program. This works for DOS and Windows XP:
    Code:
    #include <time.h>
    #include <stdio.h>
    #include <dos.h>
    
    int main(void)
    {
       clock_t start, end;
       start = clock();
    
       delay(2000);
    
       end = clock();
       printf("The time was: %f\n", (end - start) / CLK_TCK);
    
       return 0;
    }
    Adak
    Last edited by Adak; 10-26-2006 at 07:27 AM.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    You can use setitimer() and and signal() calls in order to achive the task. Just set your times and catch the signal raised. Upan cathing the signal, read required amount of chars. That's it!

    Here is a code doing a similar task
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <signal.h>
    #include <unistd.h>
    
    static struct itimerval real, virt, prof;
    static int real_sec = 0, virt_sec = 0, prof_sec = 0;
    
    /**************************************************
    * Function prototypes
    **************************************************/
    long unsigned int fibonacci (unsigned int n);
    static void handler_real(int sig);
    static void handler_virt(int sig);
    static void handler_prof(int sig);
    void settimers(struct itimerval *r, struct itimerval *v, struct itimerval *p);
    void gettimers(struct itimerval *r, struct itimerval *v, struct itimerval *p);
    
    int main(int argc, char *argv[])
    {
    	if (argc!=2)
    	{
    		fprintf(stderr,"Usage : %s fibonacci_number\n",argv[0]);
    		exit(0);
    	}
    
    	pid_t pid1, pid2;
    	struct sigaction sa_real, sa_virt, sa_prof;
    	static struct itimerval rt, vt, pt;
    
    /*****************************************
    * SA_RESTART flag is set to avoid 
    * Interrupted System call (EINTR) error.
    *****************************************/
            sa_real.sa_handler = handler_real;
            sigemptyset(&sa_real.sa_mask);
            sa_real.sa_flags = SA_RESTART;
    
            sa_virt.sa_handler = handler_virt;
            sigemptyset(&sa_virt.sa_mask);
            sa_virt.sa_flags = SA_RESTART;
    
            sa_prof.sa_handler = handler_prof;
            sigemptyset(&sa_prof.sa_mask);
            sa_prof.sa_flags = SA_RESTART;
    
            if (sigaction(SIGALRM,&sa_real,NULL)<0)
            {
                    perror("real : sigaction");
                    exit(1);
            }
    
            if (sigaction(SIGVTALRM,&sa_virt,NULL)<0)
            {
                    perror("virt : sigaction");
                    exit(1);
            }
    
            if (sigaction(SIGPROF,&sa_prof,NULL)<0)
            {
                    perror("prof : sigaction");
                    exit(1);
            }
    
    /*******************************************
    * Set timer expiration to 1 second.
    *******************************************/
    	real.it_value.tv_sec  = 1;
    	real.it_value.tv_usec = 0;
    	virt.it_value.tv_sec  = 1;
    	virt.it_value.tv_usec = 0;
    	prof.it_value.tv_sec  = 1;
    	prof.it_value.tv_usec = 0;
    
    	pid1=fork();
    
    	if (pid1 == 0)
    	{
    /************************************************
    * First child process 
    ************************************************/
    		settimers(&real,&virt,&prof);
    		fibonacci(atoi(argv[1]));
    		gettimers(&rt,&vt,&pt);
    
    		printf("Child1 : Real > %d sec %ld msec\n",real_sec,rt.it_value.tv_usec);
    		printf("Child1 : Virt > %d sec %ld msec\n",virt_sec,vt.it_value.tv_usec);
    		printf("Child1 : Prof > %d sec %ld msec\n",prof_sec,pt.it_value.tv_usec);
    		printf("\n");
    		
    		fflush(stdout);
    	}
    	else if (pid1>0)
    	{
    
    		pid2=fork();
    		if (pid2==0)
    		{
    /************************************************
    * Second child process 
    ************************************************/
    			settimers(&real,&virt,&prof);
    			fibonacci(atoi(argv[1]));
    			gettimers(&rt,&vt,&pt);
    
    			printf("Child2 : Real > %d sec %ld msec\n",real_sec,rt.it_value.tv_usec);
    			printf("Child2 : Virt > %d sec %ld msec\n",virt_sec,vt.it_value.tv_usec);
    			printf("Child2 : Prof > %d sec %ld msec\n",prof_sec,pt.it_value.tv_usec);
    			printf("\n");
    		
    			fflush(stdout);
    		}
    		else if(pid2>0)
    		{
    /************************************************
    * Parent process 
    ************************************************/
    			settimers(&real,&virt,&prof);
    			fibonacci(atoi(argv[1]));
    			gettimers(&rt,&vt,&pt);	
    
    			printf("Parent : Real > %d sec %ld msec\n",real_sec,rt.it_value.tv_usec);
    			printf("Parent : Virt > %d sec %ld msec\n",virt_sec,vt.it_value.tv_usec);
    			printf("Parent : Prof > %d sec %ld msec\n",prof_sec,pt.it_value.tv_usec);
    			printf("\n");
    
    			fflush(stdout);
    			if (waitpid(pid1,NULL,WSTOPPED)<0)
    				perror("waitpid");
    			if (waitpid(pid2,NULL,WSTOPPED)<0)
    				perror("waitpid");
    		} else
    		perror("fork");
    	}
    	else
    	perror("fork");
    
    	return 0;
    }
    
    long unsigned int fibonacci (unsigned int n)
    {
            if (n==0)
                    return 0;
            else if (n==1 || n==2)
                    return 1;
            else
                    return (fibonacci(n-1)+fibonacci(n-2));
    }
    
    static void handler_real(int sig)
    {
            real_sec++;
            if (setitimer(ITIMER_REAL,&real,0)<0)
            {
                    perror("real : setitimer");
                    exit(1);
            }
    }
    
    static void handler_virt(int sig)
    {
            virt_sec++;
            if (setitimer(ITIMER_VIRTUAL,&virt,0)<0)
            {
                    perror("virt : setitimer");
                    exit(1);
            }
    }
    
    static void handler_prof(int sig)
    {
            prof_sec++;
            if (setitimer(ITIMER_PROF,&prof,0)<0)
            {
                    perror("prof : setitimer");
                    exit(1);
            }
    }
    
    void settimers(struct itimerval *r, struct itimerval *v, struct itimerval *p)
    {
            if (setitimer(ITIMER_REAL,r,0)<0)
            {
                    perror("real : setitimer");
                    exit(1);
            }
    
            if (setitimer(ITIMER_VIRTUAL,v,0)<0)
            {
                    perror("virt : setitimer");
                    exit(1);
            }
    
            if (setitimer(ITIMER_PROF,p,0)<0)
            {
                    perror("prof : setitimer");
                    exit(1);
            }
    }
    
    void gettimers(struct itimerval *r, struct itimerval *v, struct itimerval *p)
    {
    
         	if (getitimer(ITIMER_REAL,r)<0)
          	{
           	        perror("getitimer");
                   	exit(1);
            }
    
            if (getitimer(ITIMER_VIRTUAL,v)<0)
           	{
                  	perror("getitimer");
           	       	exit(1);
    	}
    
            if (getitimer(ITIMER_PROF,p)<0)
            {
    	        perror("getitimer");
           		exit(1);
      	}
    }

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    So am I right

    My operating system is windows XP and my compiler is going to be either Turbo C, DEV C++ or Pelles C

    So let me get this clear the following 4 steps should be it. As I already mentioned I never did anything like this before s I could be wrong.

    1. I have to give the address of the buffer to the program.

    2. Then the program is going to read the data inside the adress

    3. Say the data is 56781432 I'am only interested in the first 4 data and it will be treated as 5678.

    4. After a delay say 1 second it will read the data again.

    My problem is that how am I meant to know which buffer adress the data is stored in and how do I read the data when I say read the data I mean the program must know say for the 5th second the data is 5678 how do I do that. Also I would appriciate if someone give me a hint on how to start as I'am still unsure of how to tell the program which buffer adress to use and how to read it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading large complicated data files
    By dodzy in forum C Programming
    Replies: 16
    Last Post: 05-17-2006, 04:57 PM
  2. accessing my com port, writing and reading data
    By shoobsie in forum C Programming
    Replies: 7
    Last Post: 09-16-2005, 03:29 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. reading data format into program
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 10-23-2003, 02:27 PM
  5. Binary data reading
    By jdinger in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2002, 06:56 PM