473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to store a value in a buffer in C?

Hi,

I would like to know how to store a value in a buffer in C.
One function counts the time and store it in a special place in buffer for a project. For ex project 2 save time in buffer position 2. This thing works fine, but then i would like to add a new time to project 2, but this override the old value.

Question: How can I save the old value, and then write again to the same position in the buffer. I would like to add this times in total.

I have seen some ex with using malloc(), or is there some easier way to do this.
I couldn't find any good example how to do this.
Thanks for any help.

Expand|Select|Wrap|Line Numbers
  1. void main(void){
  2.     unsigned int TimerBuffer[40];
  3.  
  4.     unsigned int project; 
  5.     project=1;
  6.     //double value;
  7.  
  8.     while (project!=0)
  9.     {
  10.     printf("Project number \n", project);
  11.     scanf_s("%d",&project);
  12.  
  13.  
  14. Berakna_Tid(TimerBuffer, project);   // Count time
  15. Sortering(TimerBuffer, project);    // Sort
  16.  
  17.     }
  18.  
  19. }
  20.  
  21. void Berakna_Tid(unsigned int TimerBuffer[], unsigned int project)
  22. {
  23.  
  24. time_t start_time;
  25. time_t stop_time;
  26. double value;
  27. int e;
  28.  
  29. printf( "Type the Enter key to START timing.\n");
  30. while(getchar()!='\n');
  31. start_time=time(NULL);
  32. printf( "Type the Enter key to STOP timing.\n");
  33. while(getchar()!='\n');
  34. stop_time=time(NULL) ;
  35. e=difftime( stop_time,start_time);   // /60 for minutes
  36. printf( "Elapsed time: %d minutes.\n" ,e) ;
  37. value=e;
  38.  
  39.  
  40.         switch (project)
  41.         {
  42.     case 1:
  43.         TimerBuffer[1] = value;
  44.         break;
  45.     case 2:
  46.         TimerBuffer[2] = value;
  47.         break;
  48.     case 3:
  49.         TimerBuffer[3] = value;
  50.         break;
  51.         }
  52.  
  53.         //project=0;
  54. }
  55.  
  56.  
  57. unsigned int Sortering(unsigned int TimerBuffer[], unsigned int project)
  58. {
  59.  
  60.             int sum=0;
  61.             unsigned int temp;
  62.         switch (project)
  63.         {
  64.     case 1:
  65.         //TimerBuffer[1] = value;
  66.         break;
  67.     case 2:
  68.         //TimerBuffer[2] = value;
  69.          sum=TimerBuffer[2] ;
  70.          temp=TimerBuffer[2];
  71.             return temp;
  72.             break;
  73.  
  74.     case 3:
  75.         //TimerBuffer[3] = value;
  76.         break;
  77.         }
  78.         project=0;    
  79.  
  80. }
  81.  
Mar 2 '11 #1
4 3892
Banfa
9,065 Expert Mod 8TB
Do you mean how can you store multiple times for a project or how can you overwrite the time for a project with a new time or how can you store the total time for a project adding to it on successive occasions?
Mar 2 '11 #2
Hi,
Yes i mean how to store multiple times for the project. For ex add 2 different times for project 2 and then store total time for project 2. The same thing should be for project one also.
I want to save the first time and when i got the new time i will add this together.
I have looked for example save to file, but maybe it is an another solution.
Thanks for help
Mar 3 '11 #3
Banfa
9,065 Expert Mod 8TB
So if you can't be sure how many times you will need to store you will need to be able to take account of the possibility of needing to change the amount of storage so that means recording how much storage you already have.

So rather than TimerBuffer being an array of unsigned int I would make it an array of structures. The structure would have a pointer to the allocated memory to store the times, a member recording how much data is allocated in the memory pointed to and a member recording how much of that memory is used or how many of the available array entries are used.

When you add a time you will need to check you have enough memory allocated, if not reallocate more memory (look up realloc), and then store the new time updating the other members of the structure.

I would make a function that adds a time entry to a structure because then it will be easier to test and keep the rest of your code clean.
Mar 3 '11 #4
Why dont you use a linklist kind of storage, adding a new node to the head with the updated values. This will maintain your past data as well as current too.
Mar 17 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: TG | last post by:
Form inputs question: How can I store the inputs a user has put into a form if they leave my website without calling the post command? I store them now after the post command is called when a...
2
by: mahesh.anjani | last post by:
hi i have stored my connction string in web.config as we do usually. code of web.confing is given below. check the password field contain "&" character. now when i run my application it gives...
1
by: Ronald S. Cook | last post by:
In my Windows app, I want to keep track of previous "screens" visited (like a browser does in a web app). On some screens (e.g. Employee Detail), there would be an EmployeeID to retain to retrieve...
6
by: sattu | last post by:
i want to store value of querystring("id") when page postback
2
by: sattu | last post by:
i have this command on page load event when page first time loaded value of querystring is 1 for Ex. but second time it becomes null i am doing paging in repeater cmd.CommandText = "select *...
1
by: Scott | last post by:
In my web app I'm creating a datatable to show my out in a grid. I have a row that has totals within my datatable. How can I store that total to use in another section of code? my datatable...
2
by: rajeshjava | last post by:
I am working with xsl. i want to store a value in the variable. xml Source looks like the below one <?xml version="1.0" encoding="UTF-8" ?> <getTradingInstallmentHistoryResponse...
5
by: pratimapaudel | last post by:
I have one text box and submit button in one asp.net page. When users enter their email address in textbox and click submit button, i want to store that email address which comes from text box in sql...
2
by: sweetneel | last post by:
hi all, can any one give me an idea how could i store a Asp.net Application variable by javascript.
2
by: lokcard | last post by:
i am a newbie with Access. I like to create a form to store user configuration. How to transfer and store those user-select value? should i store them in a table? and, during operation, how to...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.