Sinisterly
Char query - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: Char query (/Thread-Char-query)



Char query - Ex094 - 02-16-2014

I have this function,

Code:
void load_dict(char filename[15]) { std::ifstream dict; //Load Dictionary dict.open(filename); char test[25]; //Character Limit for(int i = 0; i < count_file(filename); i++) { dict >> test; std::cout << test; } }

Now If the function is like:

Code:
void load_dict(std::string filename) { std::ifstream dict; //Load Dictionary dict.open(filename); char test[25]; //Character Limit for(int i = 0; i < count_file(filename); i++) { dict >> test; std::cout << test; } }

I can't compile, I get a debug error, When I use std:Confusedtring instead of char, to be precise! Why is that so? Why I have to use char, can't I use string to achieve this? Can any one shed some light on that..


Char query - Ex094 - 02-16-2014

I have this function,

Code:
void load_dict(char filename[15]) { std::ifstream dict; //Load Dictionary dict.open(filename); char test[25]; //Character Limit for(int i = 0; i < count_file(filename); i++) { dict >> test; std::cout << test; } }

Now If the function is like:

Code:
void load_dict(std::string filename) { std::ifstream dict; //Load Dictionary dict.open(filename); char test[25]; //Character Limit for(int i = 0; i < count_file(filename); i++) { dict >> test; std::cout << test; } }

I can't compile, I get a debug error, When I use std:Confusedtring instead of char, to be precise! Why is that so? Why I have to use char, can't I use string to achieve this? Can any one shed some light on that..


RE: Char query - mrpassword - 02-16-2014

you can use std:Confusedtring by using the function .c_str() which returns the string as a const * char.
so
Code:
filename.c_str()



RE: Char query - mrpassword - 02-16-2014

you can use std:Confusedtring by using the function .c_str() which returns the string as a const * char.
so
Code:
filename.c_str()



RE: Char query - Ligeti - 02-16-2014

(02-16-2014, 05:27 PM)mrpassword Wrote: you can use std:Confusedtring by using the function .c_str() which returns the string as a const * char.
so
Code:
filename.c_str()

Yes, the standard streams doesn't accept a standard string, so use c-string as @mrpassword said!

Thanks


RE: Char query - Ligeti - 02-16-2014

(02-16-2014, 05:27 PM)mrpassword Wrote: you can use std:Confusedtring by using the function .c_str() which returns the string as a const * char.
so
Code:
filename.c_str()

Yes, the standard streams doesn't accept a standard string, so use c-string as @mrpassword said!

Thanks