problem with size_t filesize

Thread Solved

Join Date: Apr 2008
Posts: 5
Reputation: komany is an unknown quantity at this point 
Solved Threads: 0
komany komany is offline Offline
Newbie Poster

problem with size_t filesize

 
0
  #1
Apr 23rd, 2008
Hello
I have little problem with a code.
I want to send information about a size on a file and uses “size_t filesize”.
I will send this information thru a network called ”controller area Network” and uses the code
stat = canWrite(hnd, 1234, size, 8, canMSG_EXT);
in order to do this.
The problem is that ”size” in the code only may last 8 byte. Is there somewhat better way to type the code below?
  1. void send_messege(void)
  2. {
  3. int hnd,stat;
  4. unsigned char data[8];
  5. unsigned int size, i=0;
  6. unsigned char* buf = 0;
  7. FILE* fp;
  8.  
  9. //***************************************************
  10. //********* Read in the entire file to one buffer **********
  11. //***************************************************
  12.  
  13. size_t filesize = 0;
  14. // Open the sound file
  15. fp = fopen("test.mp3","rb");
  16.  
  17. // Find the size and allocate the buffer
  18. fseek(fp,0,SEEK_END);
  19.  
  20. filesize = ftell(fp);
  21.  
  22. // Go to beginning of the file
  23. fseek(fp,0,SEEK_SET);
  24.  
  25. // Allocate buffer memory
  26. buf = malloc(filesize);
  27.  
  28. // Read in to buffer
  29. fread(buf,1,filesize,fp);
  30. // close the file
  31. fclose(fp);
  32.  
  33. // Open up the bus on controller are network
  34. hnd = InitCtrl(second);
  35.  
  36.  
  37. /****************************************************
  38.  *****************************************************
  39.  ********* Send away INFO about the file's size **********
  40.  *****************************************************
  41.  ****************************************************/
  42.  
  43.  
  44. size=filesize;
  45.  
  46. // send message
  47. stat = canWrite(hnd, 1234, size, 8, canMSG_EXT);
  48. if (stat < 0)
  49. {
  50. printf("Failed, status == %d\n", stat);
  51. }
  52. stat=canWriteSync(hnd, 500);
  53.  
  54. /****************************************************
  55.  *****************************************************
  56.  ******* Divide up the file about 8 byte and send it *******
  57.  *****************************************************
  58.  ****************************************************/
  59. while (i < filesize)
  60. {
  61. data[0]=buf[i];
  62. data[1]=buf[i++];
  63. data[2]=buf[i++];
  64. data[3]=buf[i++];
  65. data[4]=buf[i++];
  66. data[5]=buf[i++];
  67. data[6]=buf[i++];
  68. data[7]=buf[i++];
  69. //send data
  70. stat = canWrite(hnd, 1234, data, 8, canMSG_EXT);
  71. if (stat < 0)
  72. {
  73. printf("Failed, status == %d\n", stat);
  74. }
  75. stat=canWriteSync(hnd, 500);
  76. i++;
  77. }
  78. //close bus
  79. canBusOff(hnd);
  80. canClose(hnd);
  81.  
  82.  
  83. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: problem with size_t filesize

 
0
  #2
Apr 23rd, 2008
>>stat = canWrite(hnd, 1234, size, 8, canMSG_EXT);
what is the 1234 and 8 for? sizeof(size_t), which is an unsigned int, if 4 on most 32-bit computers, not 8.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,624
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem with size_t filesize

 
0
  #3
Apr 23rd, 2008
>Is there somewhat better way to type the code below?
Are you simply asking for advice on working code, or are you having problems with it? I can see areas where mixing and matching types (both ranges and signedness) may get you in trouble.

If all you want is ways to improve the code then to simplify matters, I would get rid of data and just use a slice of buf:
  1. for ( i = 0; i < filesize; i += 8 ) {
  2. stat = canWrite ( hnd, 1234, buf + i, 8, canMSG_EXT );
  3.  
  4. if ( stat < 0 )
  5. printf ( "Failed, status == %d\n", stat );
  6.  
  7. stat = canWriteSync ( hnd, 500 );
  8. }
And you can refactor that while writing piece because everything is the same except for the pointer to your eight bytes:
  1. int networkWrite ( unsigned char *start )
  2. {
  3. if ( canWrite ( hnd, 1234, start, 8, canMSG_EXT ) < 0 )
  4. printf ( "Failed, status == %d\n", stat );
  5.  
  6. return canWriteSync ( hnd, 500 );
  7. }
Then the two blocks of code become:
  1. /* Send away INFO about the file's size */
  2. size = filesize;
  3. stat = networkWrite ( size );
  4.  
  5. /* Divide up the file about 8 byte and send it */
  6. for ( i = 0; i < filesize; i += 8 )
  7. stat = networkWrite ( buf + i );
However, since size is an unsigned int, you have an issue if int isn't an 8-byte quantity. If that's the case, you need to pad it. The same goes for if the file size isn't evenly divisible by eight. You can bring data back into the picture for this:
  1. /* Send away INFO about the file's size */
  2. memset ( data, 0, 8 );
  3. memcpy ( data, size, sizeof size );
  4. stat = networkWrite ( data );
  5.  
  6. /* Divide up the file about 8 byte and send it */
  7. for ( i = 0; i < filesize; i += 8 ) {
  8. size_t remaining = filesize - i;
  9. size_t n = 8;
  10.  
  11. if ( remaining < 8 )
  12. n = remaining;
  13.  
  14. memset ( data, 0, 8 );
  15. memcpy ( data, buf + i, n );
  16. stat = networkWrite ( data );
  17. }
Beyond that you'll have to specify what you need help with.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 5
Reputation: komany is an unknown quantity at this point 
Solved Threads: 0
komany komany is offline Offline
Newbie Poster

Re: problem with size_t filesize

 
0
  #4
Apr 24th, 2008
Hello again.
Thanks a lot for these codes. They have helped a lot.

I have typed 2 codes, one in order to send a file via controller area Network and a code in order to receive the file.

However, there are 2 problems with the code in order to receive the file.
1: Stack around the variable buf was corrupted
2: I believe that the file that to be sent is ok, but when I receive the file and then saved it on the hard disk. It has same size that the original file. But it is corrupted.

Some proposals on how to solve this?

The code to send the file
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <memory.h>
  4. #include <windows.h>
  5. #include <canlib.h>
  6.  
  7.  
  8. //global value
  9. int bitrate = BAUD_125K;
  10. int first = 0; // channel 1
  11. int second = 1; // channel 2
  12.  
  13. void Check(char* id, canStatus stat)
  14. {
  15. char buf[50];
  16. if (stat != canOK)
  17. {
  18. buf[0] = '\0';
  19. canGetErrorText(stat, buf, sizeof(buf));
  20. printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
  21. }
  22. }
  23.  
  24. int InitCtrl(int ctrl)
  25. {
  26. int stat;
  27. int hnd;
  28.  
  29. //open the channel
  30. printf("canOpenChannel, channel %d... ", ctrl);
  31. hnd = canOpenChannel(ctrl, canOPEN_REQUIRE_EXTENDED);
  32. if (hnd < 0)
  33. {
  34. Check("canOpenChannel", (canStatus)hnd);
  35. exit(1);
  36. }
  37. printf("OK.\n");
  38.  
  39.  
  40. printf("Setting the bus speed...");
  41. stat = canSetBusParams(hnd, bitrate, 0, 0, 0, 0, 0);
  42. if (stat < 0)
  43. {
  44. printf("canSetBusParams failed, stat=%d\n", stat);
  45. }
  46. printf("OK.\n");
  47.  
  48. //Go online on the bus
  49. printf("Go bus-on...");
  50. stat = canBusOn(hnd);
  51. if (stat < 0)
  52. {
  53. printf("canBusOn failed, stat=%d\n", stat);
  54. }
  55. printf("OK.\n");
  56.  
  57. return hnd;
  58. }
  59.  
  60. void send_messege(void)
  61. {
  62. int hnd,stat;
  63. unsigned char data[8];
  64. signed int *size, i,k=0;
  65. unsigned char* buf = 0;
  66. FILE* fp;
  67.  
  68.  
  69. /*********Read the file to buffer**********/
  70.  
  71. size_t filesize = 0;
  72. // Open the file
  73. fp = fopen("mat.rar","rb");
  74.  
  75. // get the size of the file and allocate input buffer space
  76. fseek(fp,0,SEEK_END);
  77.  
  78. filesize = ftell(fp);
  79.  
  80. // go back to beginning of file
  81. fseek(fp,0,SEEK_SET);
  82.  
  83. // allocate input buffer
  84. buf = malloc(filesize);
  85.  
  86. // read into buffer
  87. fread(buf,filesize,1,fp);
  88. //close the file
  89. fclose(fp);
  90.  
  91. //Öppna upp busen
  92. hnd = InitCtrl(second);
  93.  
  94.  
  95. /* Send away INFO about the file's size */
  96. size=filesize;
  97. //Send messege
  98. stat = canWrite(hnd, 1234, &size, 4, canMSG_EXT);
  99. if (stat < 0)
  100. {
  101. printf("Failed, status == %d\n", stat);
  102. }
  103. stat=canWriteSync(hnd, 500);
  104.  
  105. /* Divide up the file about 8 byte and send it
  106. 1234 = ID, buf +n=data that is send, n=how many byte to send(max=8),
  107. */
  108. for ( i = 0; i <= filesize; i += 8 )
  109. {
  110. size_t remaining = filesize - i;
  111. size_t n = 8;
  112.  
  113. if ( remaining < 8 )
  114. {
  115. n = remaining;
  116. k = n+1;
  117. stat = canWrite ( hnd, 1234, buf +k, n, canMSG_EXT );
  118. }
  119. else
  120. stat = canWrite ( hnd, 1234, buf + i, n, canMSG_EXT );
  121.  
  122. }
  123. //Close the bus
  124. canBusOff(hnd);
  125. canClose(hnd);
  126. }
  127.  
  128. void main()
  129. {
  130. canStatus stat;
  131. canInitializeLibrary();
  132. send_messege();
  133.  
  134. }

And the code to receive
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <memory.h>
  4. #include <windows.h>
  5. #include <canlib.h>
  6.  
  7.  
  8. //global value
  9. int bitrate = BAUD_125K;
  10. int first = 0; // channel 1
  11. int second = 1; // channel 2
  12. void Check(char* id, canStatus stat)
  13. {
  14. char buf[50];
  15. if (stat != canOK)
  16. {
  17. buf[0] = '\0';
  18. canGetErrorText(stat, buf, sizeof(buf));
  19. printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
  20. }
  21. }
  22.  
  23. int InitCtrl(int ctrl)
  24. {
  25. int stat;
  26. int hnd;
  27.  
  28.  
  29. //open the channel
  30. printf("canOpenChannel, channel %d... ", ctrl);
  31. hnd = canOpenChannel(ctrl, canOPEN_REQUIRE_EXTENDED);
  32. if (hnd < 0)
  33. {
  34. Check("canOpenChannel", (canStatus)hnd);
  35. exit(1);
  36. }
  37. printf("OK.\n");
  38.  
  39.  
  40. printf("Setting the bus speed...");
  41. stat = canSetBusParams(hnd, bitrate, 0, 0, 0, 0, 0);
  42. if (stat < 0)
  43. {
  44. printf("canSetBusParams failed, stat=%d\n", stat);
  45. }
  46. printf("OK.\n");
  47.  
  48.  
  49. //Go online on the bus
  50. printf("Go bus-on...");
  51. stat = canBusOn(hnd);
  52. if (stat < 0)
  53. {
  54. printf("canBusOn failed, stat=%d\n", stat);
  55. }
  56. printf("OK.\n");
  57.  
  58. return hnd;
  59. }
  60.  
  61.  
  62. void read_messege(void)
  63. {
  64. long id;
  65. unsigned int dlc,flags;
  66. unsigned long timestamp,timeout;
  67. int hnd;
  68. int stat,i=0;
  69.  
  70. char *buf ;
  71. signed int *size;
  72. size_t filesize;
  73. FILE* fp;
  74.  
  75.  
  76. hnd = InitCtrl(first);
  77.  
  78. /* Read INFO about the file's size
  79. hnd=handshake, &size=data that i resived, &dlc=how many byte that is resived
  80. */
  81.  
  82.  
  83. stat=canReadWait(hnd, &id, &size, &dlc, &flags, &timestamp, &timeout);
  84. stat=canReadSync(hnd, 500);
  85.  
  86.  
  87. filesize = size;
  88.  
  89. /*Store data to the buffer*/
  90. for ( i = 0; i < filesize; i += 8 )
  91. {
  92. size_t remaining = filesize - i;
  93. size_t n = 8;
  94.  
  95. if ( remaining < 8 )
  96. {
  97. n = remaining+i;
  98. stat=canReadWait(hnd, &id, &buf + n, &dlc, &flags, &timestamp, &timeout);
  99. stat=canReadSync(hnd, 500);
  100. }
  101. else
  102. {
  103. stat=canReadWait(hnd, &id, &buf + i, &dlc, &flags, &timestamp, &timeout);
  104. stat=canReadSync(hnd, 500);
  105. }
  106. }
  107.  
  108. //Close the bus
  109. canBusOff(hnd);
  110. canClose(hnd);
  111.  
  112. /*write data to file*/
  113. fp = fopen("matt.rar","wb");
  114. fwrite(&buf,filesize,1,fp);
  115. fclose(fp);
  116. }
  117.  
  118. void main()
  119. {
  120.  
  121. canStatus stat;
  122. canInitializeLibrary();
  123. read_messege();
  124. return 0;
  125.  
  126. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: problem with size_t filesize

 
0
  #5
Apr 24th, 2008
>> Stack around the variable buf was corrupted
That means the program has trashed the stack somewhere. That's a tough one to find. Check for buffer overruns, writing beyond the end of a buffer or array, using uninitialized pointers, and indexing into non-existant array elements. If you still can't find it then start commenting out blocks of code until the problem disappears. When that happens you have probably found the deamon.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC