We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi to all, The posting summery may be simple, but i am not able to fix the error.
Ok lets come to the problem. I am creating an Index File "Test.txt" on Serial Flash using FILESystem which holds 50,000 datas each one is 2 bytes.
Another File is xx.txt which holds same 50,000 datas each size is 4 bytes. Using following function.
//MAX_USERS =50000 int File_Write_Index (unsigned int no_ofData) { unsigned short count[MAX_USERS], i; FILE *fpt; fpt = fopen ("Test.txt","w"); if (fpt == NULL) { printf ("File not found!. Write Failed\n"); return 0; } else { for(i=0; i<MAX_USERS; i++)//MAX_USERS =50000 count[i] = 0x0000 + i; fwrite (&count[0], 2, MAX_USERS, fpt); fclose (fpt); printf("\r\nWrite Succeed"); return 1; } }
and the second function for 4 bytes data same as first one where i change the size as 4.
fwrite (&count[0], 4, MAX_USERS, fpt);
Actually i made this function for 1000 MAX_USERS. But now i want to make it for 5000.
But Creating buffer for 50000 will increase the RAM size. So i Created a line
unsigned long i=0; unsigned char j; for(k=0; k<MAX_USERS; ) { j=(unsigned char *)&k; fwrite (&j, 2, 1, fpt); } unsigned long j; for(k=0x11111111; k<0x11111111+MAX_USERS; k++) { j=(unsigned char *)&k; fwrite (&j, 2, 1, fpt); } But this one getting much time to read and write the files and even to search just for 1000 MAX_USERS. I am not able to go for more than 1000. The code does not print any errors. But its not working. while debugging it goes to Somewhere which i dont know. So i am here for your help and guidelines. Please tell me how to create a line to move my 50000 no of 2 and 4 bytes of datas into my corresponding files? I am using LPC2388.
In some ways, you seem to think you are programming a PC with infinite RAM. In other aspects, it's time for a generic textbook on C programming.
1) Don't have comments mentioning sizes of constants. You will forget to update these comments when you modify the constant. Your source code should just have contained a #define or enum specifying the value of MAX_USERS.
2) Embedded processors normally have limited amount of RAM. Having a local variable unsigned short count[MAX_USERS] would in your case require 50000*2 = 100,000 bytes of stack space. How large stack have you specified?
3) Why do you have null-operations? What did you expect to get from adding 0x0000 to i in the following statement?
count[i] = 0x0000 + i;
4) You posted the following code:
unsigned long i=0; unsigned char j; for(k=0; k<MAX_USERS; ) { j=(unsigned char *)&k; fwrite (&j, 2, 1, fpt); }
k is probably unsigned short (since you write two bytes), but why didn't you post all variable declarations for your sample code?
5) Now is k really two bytes large, since your next code block contains:
unsigned long j; for(k=0x11111111; k<0x11111111+MAX_USERS; k++) { j=(unsigned char *)&k; fwrite (&j, 2, 1, fpt); }
You still write two bytes, but the initial value you assign to k is 0x11111111 which requires a 32-bit variable. If k is a short, how do you expect to be able to increment your 16-bit k until it holds a 32-bit value? And if k really is big enough to hold a 32-bit value, what would then happen when your fwrite only writes two bytes instead of 4?
6) If you did look at the preview, you should have noticed that you forgot to close the last code block. Never put normal text inside a code block, since that will force this forum to display the full text line length without breaking it.
Sorry for my mistakes in my last posting. But thanks for your reply. I didnt look at the preview well. Sorry for that.
What i am trying to do is I am making a index file which contains the locations of the each data in the data file. Each index is 2 bytes and each data is 4 bytes. So each data location is stored in the index file. So i am making 50000 index in the index file and 50000 datas in the data file.
And after i succeed writing them in SD card, i just commend the write the functions and doing binary searching.
unsigned long Data_need_2_Searched = 0x3BA60000;
However i fixed that issue using the following code
int File_Write_Index (unsigned int no_ofData) { unsigned short i; unsigned char *L_UC_Temp; FILE *fpt; fpt = fopen ("Test.txt","w"); if (fpt == NULL) { printf ("File not found!. Write Failed\n"); return 0; } else { for(i=0; i<MAX_USERS; i++) { // count[i] = 0x0000 + i; L_UC_Temp = (unsigned char *)&i; fwrite (L_UC_Temp, 2, 1, fpt); } fclose (fpt); printf("\r\nWrite Succeed"); return 1; } }
and
int File_Write_UserDet (unsigned int no_ofData) { unsigned long int i; FILE *dat; unsigned char *L_UC_Temp; dat = fopen ("UserLocation.txt","w"); if (dat == NULL) { printf ("File not found!. Write Failed\n"); return 0; } else { for(i=0x3BA5B64B; i<(0x3BA5B64B + MAX_USERS); i++) { // count[i] = 0x3BA5B64B + i; L_UC_Temp = (unsigned char *)&i; fwrite (L_UC_Temp, 4, 1, dat); } fclose (dat); printf("\r\nWrite Succeed"); return 1; } }
But still i have a problem that is if i define MAX_USERS value more than 24500, i am not able search my data without writing the 24500 datas again(i.e need to uncomment the write function ). i.e whenever i need to search the data, the code asks that write the whole data again in the file then do the searching. then I need to write the whole 24500 long value again using this function.
File_Write_UserDet(unsigned int no_ofData)
That means once if i succeed to write the index file and data file in the SD card, i am able to commend those line for searching the data in the memory(i.e no need to write it again and again).
So with that if my MAX_USERS value is less than 24000, i am able to search the data again and again without writing them again and again.
But if i define my MAX_USERS value more than 24000 I am not able to Search my data without writing them again and again.
Simply it says data not available.
I did writing the MAX_USERS no of data file in SD card only once. This is enough to search below 24000 datas.
I am not able to continue this with more that 24000 value.
So i need to fix this error. please help. if my posting is not quite clear means please ask me.
How can you print "Write Succeed" in:
for(i=0x3BA5B64B; i<(0x3BA5B64B + MAX_USERS); i++) { //count[i] = 0x3BA5B64B + i; L_UC_Temp = (unsigned char *)&i; fwrite (L_UC_Temp, 4, 1, dat); } fclose (dat); printf("\r\nWrite Succeed"); return 1;
There is no test of the fwrite result...
And you complain about problems searching, but have not posted any search code - only write code. And no code that shows any ability to read what you have written. And no information how you actually write, i.e. what fwrite() really does.
I fixed all the errors. I am not able to post all the codes. Sorry for that.
Instead of making 50000 directly i made a buffer which size is always 250. So then i am able make it works.
L_UI_BufferSize = 0; for(loop = 0; loop < 50000; loop++) { L_US_Test[L_UI_BufferSize] = loop; L_UI_BufferSize++; if(L_UI_BufferSize == 250) { fwrite (&L_US_Test[0], 2, 250, fpt); L_UI_BufferSize = 0; } } fclose (fpt);
i print the printf statements using UART1. serial.c and retarget.c
We can test the fwrite function that whether it has written the whole data or not, using fseek and SEEK_END.
So fseek in the last location gives you the data which you actually written on it.
Does the manual say that fwrite() returns a result code or that it doesn't?
fwrite returns no of data it has been written.
No need to find out the last location.
fseek (fpt, -(1 * 2), SEEK_END);
so
-(1 *2)
gives you the last two bytes of data which is presented in the last location. Whats that result code actually mean?
The result code means that you don't need to wait until you have run the full loop. After each and every call to fwrite() you will know if it has written the expected amount of data or not.
When writing code, you should always consider checking the return values for functions that do return them - they return a value for a reason.
Yes good point per. Thanks for that. I will follow that..... Thanks per.
Hello,
I'm confused with the 'In-Circuit Debugging' and 'Real-Time Debugging'.
I wish to purchase a development system that allows me to change 'variables' as well as continuous display the values of variables during run-time. Which one that I should refer to, the 'In-Circuit Debugging', or the 'Real-Time Debugging'?
Thanks.
Debuggers don't normally show variable values while the application is running. But there are solutions where a background process continuously walks through the RAM and dumps data, allowing you to see variable values with a random time lag.
Why do you "need" to see your variables updated live when the program runs? Most developers manages just fine without this - or maybe implements their own background interrupt to dump critical information (a subset of the RAM) a number of times/s over JTAG, CAN, UART, SPI, ...
You haven't mentioned what processor you have, but NXP has something they call RealMonitor, that is a software module you integrate into your application and that allows real-time viewing of data.
By the way - start new threads for new questions.
And - of course - never hijack someone elses thread.