如何逐字读取文本文件(分隔空格和新行) 我正在做C++。如果我有类似“逐字\n这是测试”的字符串,则每次ifstream从文本文件中获取值时,都可能有空格、单词和新行字符。像 string msg = "word by word\n this is test"; string eachWord; while( // something here ) { // do some code here to get word by word // but it should get white space and new line when it hits them }
因此,当字符串“eachWord”通过while循环时,它将具有如下特性 第一个单词:“单词” 第二个单词:“\0” 第三个单词:“by” . . . . . 注意:您不知道您将拥有什么字符串,因为我将从随机文本文件中获取字符串如何逐字读取文本文件(分隔空格和新行) 我正在做C++。如果我有类似“逐字\n这是测试”的字符串,则每次ifstream从文本文件中获取值时,都可能有空格、单词和新行字符。像 string msg = "word by word\n this is test"; string eachWord; while( // something here ) { // do some code here to get word by word // but it should get white space and new line when it hits them },c++,C++,因此,当字符串“eachWord”通过while循环时,它将具有如下特性 第一个单词:“单词” 第二个单词:“\0” 第三个单词:“by” . . . . . 注意:您不知道您将拥有什么字符串,因为我将从随机文本文件中获取字符串 我可以用ifstream.get()和一堆条件语句来完成,但我只是想知道是否有更好的方法。您可以使用substr或find函数来查找所需的空格或其他字符 例如: string str="We think in generalities, but we live in
我可以用ifstream.get()和一堆条件语句来完成,但我只是想知道是否有更好的方法。您可以使用substr或find函数来查找所需的空格或其他字符 例如:
string str="We think in generalities, but we live in details.";
string str2 = str.substr (3,5); // output --> "think"
请阅读以下页面,它将帮助您:
您可以使用substr或find函数查找所需的空格或其他字符 例如:
string str="We think in generalities, but we live in details.";
string str2 = str.substr (3,5); // output --> "think"
请阅读以下页面,它将帮助您:
使用strtok。在我看来,没有必要围绕标记化构建一个类,除非strtok没有提供您需要的东西。这里有一个例子
char myString[] = "Word1 Word2";
char *p = strtok(myString, " ");
while (p) {
printf ("Token: %s\n", p);
p=strtok(NULL, " ");
}
一些注意事项(可能不适合您的需要)。字符串在此过程中被“销毁”,这意味着EOS字符被内联放置在delimter点中。正确的用法可能需要创建字符串的非常量版本。您还可以在解析过程中更改分隔符列表。使用strtok。在我看来,没有必要围绕标记化构建一个类,除非strtok没有提供您需要的东西。这里有一个例子
char myString[] = "Word1 Word2";
char *p = strtok(myString, " ");
while (p) {
printf ("Token: %s\n", p);
p=strtok(NULL, " ");
}
一些注意事项(可能不适合您的需要)。字符串在此过程中被“销毁”,这意味着EOS字符被内联放置在delimter点中。正确的用法可能需要创建字符串的非常量版本。您还可以在解析过程中更改分隔符列表。您可以在输入流上使用
操作符>
功能,并禁用跳过空白字符
示例代码
您可以对输入流使用
操作符>
功能,并禁用跳过空白字符
示例代码
< /P> < P>基于牛仔建议的完全C++解决方案。< / P>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
int main()
{
std::vector<std::string> words;
std::ifstream ifs("words.txt");
if (ifs.is_open() == false)
{ std::cerr << "Couldn't open file..." << std::endl; return -1; }
std::string string_to_split(
(std::istreambuf_iterator<char>(ifs))
, std::istreambuf_iterator<char>());
char * cstr = new char [string_to_split.length()+1];
std::strcpy (cstr, string_to_split.c_str());
const char delimiters[]=" \t\r\n\v\f";
char *p = strtok(cstr, delimiters);
while (p) {
words.push_back(p);
words.push_back("");
p = strtok(NULL, delimiters);
}
for (auto word : words)
{ std::cout << "word: " << word << std::endl; }
}
#包括
#包括
#包括
#包括
#包括
int main()
{
向量词;
std::ifstream ifs(“words.txt”);
if(if.is_open()==false)
{STD::CURR < P>基于OrbBoBOBOL建议的完全C++解决方案。
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
int main()
{
std::vector<std::string> words;
std::ifstream ifs("words.txt");
if (ifs.is_open() == false)
{ std::cerr << "Couldn't open file..." << std::endl; return -1; }
std::string string_to_split(
(std::istreambuf_iterator<char>(ifs))
, std::istreambuf_iterator<char>());
char * cstr = new char [string_to_split.length()+1];
std::strcpy (cstr, string_to_split.c_str());
const char delimiters[]=" \t\r\n\v\f";
char *p = strtok(cstr, delimiters);
while (p) {
words.push_back(p);
words.push_back("");
p = strtok(NULL, delimiters);
}
for (auto word : words)
{ std::cout << "word: " << word << std::endl; }
}
#包括
#包括
#包括
#包括
#包括
int main()
{
向量词;
std::ifstream ifs(“words.txt”);
if(if.is_open()==false)
{ STR::CURR:请把你尝试过的内容张贴出来,指出你的代码中有哪些问题/疑问。@ RSAHHU,也许我不应该给出你的评论的答案,但我很好奇,想解决一个问题。我应该删除我的答案吗?如果它是文本文件,你一行一行地看它怎么会出现在中间?”很好。无需删除您的答案。请发布您尝试过的内容,指出代码中存在问题/疑问的地方。@RSahu鉴于您的评论,也许我本不应该提供答案,但我很想尝试解决问题。我应该删除我的答案吗?如果它是文本文件,并且您逐行阅读,那么\n如何在中间出现dle?@JamesAdkison,没关系。不需要删除你的答案。