Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Working with and outputting Strings
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
deville75




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: 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.
abcdefghijklmnopqrstuvwxy




PostPosted: 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:

code:

char c_string[255] = "hello my name is: ";
std::string cpp_string = c_string;
std::ifstream in("hello.txt");

cpp_string >> in;

deville75




PostPosted: 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:

code:

char c_string[255] = "hello my name is: ";
std::string cpp_string = c_string;
std::ifstream in("hello.txt");

cpp_string >> in;



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 Confused
md




PostPosted: 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 Wink

also
code:
in << c1 << "," << c2 << "," << c3 << "," << c4 << std::endl;


using std::string is a much better idea then c strings, so I'd recommend looking into them.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: