简体   繁体   中英

Read an ifstream word for word into char*

Just trying a programming test for fun. It stipulates that I should read each word of a file, one by one.

It hints that I might want to use ifstream, but won't let me use std::string , so it looks like I have to use char*

In CI would read line by line & use strok() as I have multiple delimiters (white-space, quotes, brackets, etc).

What the most C++ way to do this - get the words one by one - without using std::string ?

First you must make sure that you have memory allocated for your string (this part would be handled automatically by std::string ). Then just use the normal input operator >> , as it will separate on whitespace.

Don't forget to free the memory you allocate (also handled automatically by std::string ).

Lesson to be learned: Use char pointers for exercises like these, otherwise use std::string .

只需将文件读入std::string ,然后使用std::string::c_str ( )std::string对象中检索以nul终止的C样式字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM