Computer Science Canada Working with and outputting Strings |
Author: | deville75 [ Thu Feb 01, 2007 1:16 pm ] |
Post subject: | Working with and outputting Strings |
I'm using the fwrite function and I'm having a problem. size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream ); my ptr is a character of size 256. size is set to 1 ( sizeof(ptr[0]) ). my problem is with count. If i set count to sizeof(buffer) it will be set to 256. how do i make it so that it only gets the exact size of only the text. stream is just a FILE object. What I did is I tried entereing a template text. So if my text is "12:32:58:01" then I let count be equal to sizeof("99:99:99:99"); This works, but the problem is my text does not follow an exact template. Any ideas? Or maybe I should use a different output function. |
Author: | md [ Thu Feb 01, 2007 4:55 pm ] |
Post subject: | RE:Working with and outputting Strings |
try strlen, which will interpret your buffer as a string. If your not writing an actual string, but some binary data then you'll have to figure out the size on your own. |
Author: | abcdefghijklmnopqrstuvwxy [ Thu Feb 01, 2007 8:16 pm ] | ||
Post subject: | RE:Working with and outputting Strings | ||
Are you just trying to write some characters to a text file? How come the extraction operator (>>) doesn't work? Also I'm having trouble understanding what you are saying. All that templated text jazz confused the hell out of me. But are you saying you want to take a c-string like "hello my name is" and have it output "hello my name is" to a text file? In that case I'd do this:
|
Author: | deville75 [ Fri Feb 02, 2007 8:51 am ] | ||
Post subject: | Re: RE:Working with and outputting Strings | ||
abcdefghijklmnopqrstuvwxy @ Thu Feb 01, 2007 8:16 pm wrote: Are you just trying to write some characters to a text file? How come the extraction operator (>>) doesn't work?
Also I'm having trouble understanding what you are saying. All that templated text jazz confused the hell out of me. But are you saying you want to take a c-string like "hello my name is" and have it output "hello my name is" to a text file? In that case I'd do this:
lol, ya i didn't think that template 'jazz' was very well explained by me.. my mistake. Yes you are right I want to output text to a file, but the text is actual data. There are four 'channels', each channel is read and the data needs to be output to a .csv (comma seperated excel file). So I'd output: (pseudocode) chan1 + "," + chan2 + "," + chan3 + "," + chan4 + "\n"; chan1-4 is a char that will look like this: 23.2423 for example, or it will say "Disconnected" if the channel is disconnected. But, either way Md's suggestion worked! I can't believe I forgot about strlen function ![]() |
Author: | md [ Fri Feb 02, 2007 1:28 pm ] | ||
Post subject: | RE:Working with and outputting Strings | ||
try [codein << "stuff" [/code] Given that that's how you write ![]() also
using std::string is a much better idea then c strings, so I'd recommend looking into them. |