2011-09-05 62 views
0

我刚刚开始编程,但我有很多关于如何通过制作从文件读入内存时映射数据地址的程序解析文件时,使我的生活更轻松的想法。C++存储地址列表以解析原始非终止文本数组?

注:我砍倒墙上的文字,这里的问题简而言之

一个人如何解析字符数组没有空终止,但话全部用大写字母开头这样资本可以作为分隔符?

基本上我想解析只是'WordWordWord'的文本文件,并将每个单词发送给它自己的单独字符串变量,然后就可以将每个单词写入到添加了新行的文本文件中。

我想做一些更高级的东西,但我被要求削减文本的墙,这样会为现在做:)

//pointers and other values like file opening were declared 
int len = (int) strlen(words2); 

cout << "\nSize of Words2 is : " << len << " bytes\n"; 

// 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(); 
+0

缩短你的文字。我不想浪费时间阅读这一切:)和因为没有人回答我想我不是唯一一个......写得简短明了;并举例说明.. – duedl0r

回答

0

既然你也

想做一些更先进的东西

我会看看Boost.Regex从开始。这是进行文本操作的好库。

1

简单。使用Boost.Tokenizer,与char_separator("", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")""是放置分隔符的集合,A-Z是保留分隔符的集合。 (如果你使用AZ的下降分隔符,你会得到ord ord ord,因为你会放下W.)

0
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] = '\0'; // add null-terminator to string 
     parsedStrings.push_back(newString); 
    } 
}