我刚刚开始编程,但是我有很多关于如何使我的生活更容易解析文件时,通过制作一个程序映射数据的地址,当从一个文件读取到内存
注意:我把墙上的文字剪掉了,这里是一个简单的问题
如何解析没有空结束符的字符数组,但单词都以大写字母开头,因此可以使用大写作为分隔符?
基本上我想解析的文本文件,只是'WordWordWord'和发送每个单词到它自己的单独的字符串变量,然后能够写每个单词到一个文本文件添加换行符。
我想做一些更高级的东西,但我被要求剪掉文本墙,所以现在就可以了:)
//pointers and other values like file opening were declared
int len = (int) strlen( words2 );
cout << "nSize of Words2 is : " << len << " bytesn";
// Loops through array if uppercase then...
for (int i = 0; i < len; i++)
{
if (isupper(words2[i]))
{
// Output the contents of words2
cout << "n Words2 is upper : " << words2[i] << "n";
b1 = &words2[i];
//output the address of b1 and the intvalue of words2[var]
cout << "nChar address is " << &b1 << " word address is " << (int) words2[i] << "n";
cout << "nChar string is " << b1 << " address +1 "<< &b1+1 <<"n and string is " << b1+1 << "n";
}
cout << "nItem I is : i " << i << " and words2 is " << words2[i] << "n";
}
fin.clear();
fin.close();
fout.close();
简单。使用增加。Tokenizer,使用char_separator("", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
。""
为丢弃的分隔符集合,A-Z为保留的分隔符集合。(如果你使用A-Z作为省略分隔符,你会得到ord ord ord
,因为你省略了w)
既然你也
想做一些更高级的东西
我想看看Boost。从一开始就使用正则表达式。
vector<char *> parsedStrings;
char * words = "HelloHelloHello";
int stringStartAddress = 0;
for (int i = 0; i <= strlen(words); i++)
{
/* Parses word if current char is uppercase or
if it's the last char and an uppercase char was previously matched */
if (isupper(words[i]) || ((i == strlen(words)) && (stringStartAddress != 0)))
{
// Current char is first uppercase char matched, so don't parse word
if (stringStartAddress == 0)
{
stringStartAddress = ((int)(words + i));
continue;
}
int newStringLength = ((int)(words + i)) - stringStartAddress;
char * newString = new char[newStringLength + 1];
// Copy each char from previous uppercase char up to current char
for (int j = 0; j < newStringLength; j++)
{
// Cast integer address of char to a char pointer and then get the char by dereferencing the pointer
// Increment address to that of the next char
newString[j] = *((char *)stringStartAddress++);
}
newString[newStringLength] = ' '; // add null-terminator to string
parsedStrings.push_back(newString);
}
}