unsigned int j;
for( j = 0; j < 1500; j++ ) {
memcpy(buffTemp, &buffer[j], bytesRead);
memset(buffTemp, buffer[j], 1);
}
edit: bytesRead is a unsigned int and equals 1500
Now this successfully works a little bit and sets buffTemp to the last byte in my buffer. Thats good and all but I want to write all the bytes not just the last one, how would I accomplish this?
Thanks for your time.
- BlueDev
[edited by - BlueDev on September 3, 2003 12:28:46 PM]
Reading one byte at a time...
Hello,
I am trying to read one byte at a time and then write that one byte to a string, then the next byte and append that to the string. My first string is called '*buffer' and second '*buffTemp'. I want buffTemp to read each of buffers bytes and copy them to buffTemp. I cannot do it by just string copying because something has happened to buffer which only wrote two bytes but if I do a loop of displaying the bytes I see all 1500. I dont know why. So I have goten this far:
memcpy( buffTemp, buffer, 1500 );
or
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on September 3, 2003 12:35:41 PM]
or
for( size_t i = 0; i < 1500; ++i ){ buffTemp[i] = buffer[i];}
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on September 3, 2003 12:35:41 PM]
It would depend on the source data, but it seems like you''re just trying to copy a string but the buffer isn''t null terminated. Can you either memset the whole buffer to 0 before populating it or after populating it, set the next byte to 0.
Ok,
Fruny, the first thing I tried was:
memcpy( buffTemp, buffer, 1500 );
Now whats wrong with that is that for some reason my buffer only thinks its two bytes large until I display it in a loop then all the bytes show up. Then when I tried the second loop and displayed buffTemp once again I only saw two bytes worth of data. I dont know why this is happening but if I ever use 'cout <<' and display one byte at a time it all shows up but I just cant read it write. It may also be the way my char is setup, I dont really know but Ill post my code below:
Now for some reason that memcpy and memset only wrote buffTemp the last byte of buffer, which I know thats good in some way cause I can actually write the bytes, now I want to be able to do this with all bytes, how would I?
- BlueDev
[edited by - BlueDev on September 3, 2003 1:05:44 PM]
Fruny, the first thing I tried was:
memcpy( buffTemp, buffer, 1500 );
Now whats wrong with that is that for some reason my buffer only thinks its two bytes large until I display it in a loop then all the bytes show up. Then when I tried the second loop and displayed buffTemp once again I only saw two bytes worth of data. I dont know why this is happening but if I ever use 'cout <<' and display one byte at a time it all shows up but I just cant read it write. It may also be the way my char is setup, I dont really know but Ill post my code below:
#include <iostream.h>#include <stdio.h>#include <stdlib.h>void main() { FILE *file; char *buffer, *buffTemp; unsigned int bytesRead, lSize, MAXSIZE = 1500; file=fopen("C:\\test.txt", "rb"); if(file != NULL) { fseek(file, 0, SEEK_END); lSize=ftell(file); rewind(file); buffer = (char*) malloc (MAXSIZE + 1); if (buffer == NULL) exit (2); bytesRead = fread(buffer,1,MAXSIZE,file); buffer[bytesRead] = '\0'; buffTemp = (char*) malloc (MAXSIZE + 1); unsigned int j; for( j = 1; j < MAXSIZE; ++j ) { cout << buffer[j]; memcpy( buffTemp, &buffer[j], bytesRead ); // bytesRead = 1500 memset( buffTemp,buffer[j],1 ); } cout << endl; fclose(file); free(buffer); free(buffTemp); }}
Now for some reason that memcpy and memset only wrote buffTemp the last byte of buffer, which I know thats good in some way cause I can actually write the bytes, now I want to be able to do this with all bytes, how would I?
- BlueDev
[edited by - BlueDev on September 3, 2003 1:05:44 PM]
I built this with VS.NET 2003 and it worked fine for me. The code is:
The text file I used was:
[edited by - jonstelly on September 3, 2003 2:18:37 PM]
#include <iostream>#include <stdio.h>#include <stdlib.h>using namespace std;void main(){ FILE *file; char *buffer, *buffTemp; unsigned int bytesRead, MAXSIZE = 1500; file=fopen("C:\\test.txt", "rb"); if(file != NULL) { buffer = (char*) malloc (MAXSIZE + 1); if (buffer == NULL) exit (2); bytesRead = fread(buffer,1,MAXSIZE,file); buffer[bytesRead] = '\0'; cout << buffer << endl; fclose(file); free(buffer); }}
The text file I used was:
Hello WorldHow is it going?
[edited by - jonstelly on September 3, 2003 2:18:37 PM]
Strings are not just sequences of characters. A string is sequence of characters with a null terminator. When you write the loop to display the string within the buffer you need to check for a null terminator character which is '\0' as a character, or ascii code zero.
ie-
[edited by - haro on September 3, 2003 3:26:39 PM]
ie-
void PrintString( char* cp ){ int i = 0; while( cp[i] != '\0' ) printf("%c", cp[i++]);}
[edited by - haro on September 3, 2003 3:26:39 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement