2013-10-16 25 views
0

例如说我有一个txt文件,看起来像这样的数据的这些行:如何在只读一个int,而忽略C++中的其余

43-.u87t 24days r random sentence 
54pj6fcb5 22things L random sentence 

我需要得到43忽略剩下的那一天,然后得到24个不理会的日子,得到r然后得到字符串,然后再用下一行做。

我最终会得到这个信息到一个输出文件,其中43将是一个ascII字符用作填充字符,24将是输出字符串的宽度,r将等于右对齐,并将字符串放入。所以输出线1是:

+++++++++random sentence 

线2将是:

random sentence6666666 

这不是我的确切分配,但非常相似。现在我有这个:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <iomanip> 


using namespace std; 

int main() 
{ 
string filename; 
int a, b, c; // ASCII values for fill character 
int aw, bw, cw; // width values for setwidth 
char x, y, z; // character for right or left justification 
string s1, s2, s3; // strings 

ifstream inData; 

cout << "please enter the location of the file you wish to input: " << endl; 
getline(cin, filename); 

inData.open(filename.c_str()); 


    inData >> 

任何帮助将不胜感激。

+0

你已经写了一个没有包装的东西! –

+2

你是否熟悉stringstream? – Beta

+0

这是来自[Ivan Neeson](http://www.kumobius.com/2013/08/c-string-to-int/)的一篇很棒的文章,它展示了将'string'转换为'int'的不同方式。其中一些人有你想要的行为。 – wendelbsilva

回答

0

(\d*).*? (\d*).*? (\w) .*

上述正则表达式返回三个匹配组。

“43 .u87t24天R无规句子”(使用正则表达式拉德设计http://www.radsoftware.com.au/regexdesigner/测试)给出“43”,“24”,和“r”

“54pj6fcb5 22things大号随机句子”给出“54” ,“22”和“L”

我没有C++环境,但boost boost正则表达式可以为你提供上述模式。

希望它有帮助。

+0

我试过了,但无法从中得到任何有效的结果。我现在还太新,不清楚这件事发生了什么。 – user2087867

0

尝试阅读每一个字符,并检查与isdigit(),如果它是一个数字。 如果您检测到一个数字,请将其置于一个字符串中,直到isdigit()为false。然后将该字符串转换为数字。

制造商

0

menrfa击败了我。

不过,你有没有考虑过使用正则表达式?

我写了一个快速的程序使用一个的解析你的两个例句:

#include <iostream> 
#include <string> 
#include <regex> 

int main() 
{ 
    std::smatch sm; 
    std::regex r("(\\d*).*? (\\d*).*? (\\w) (.*)"); 
    std::string s[2] = {"43-.u87t 24days r random sentence", 
         "54pj6fcb5 22things L random sentence"}; 

    for (int i = 0; i < 2; ++i) 
    { 
     std::regex_match(s[i], sm, r);  
     std::cout << "The first number matched is : " << sm[1] << std::endl; 
     std::cout << "The second number matched is : " << sm[2] << std::endl; 
     std::cout << "The character matched is  : " << sm[3] << std::endl; 
     std::cout << "The sentence matched is  : " << sm[4] << std::endl 
        << std::endl; 
    } 

    std::cin.get(); 

    return 0; 
} 

这是输出:

The first number matched is : 43 
The second number matched is : 24 
The character matched is  : r 
The sentence matched is  : random sentence 

The first number matched is : 54 
The second number matched is : 22 
The character matched is  : L 
The sentence matched is  : random sentence 
0

你可以得到你输入的每一行与std::getline() 。然后,您可以在该行上使用istringstream将输入的每个组件解析为文本元素,直至您的随机句子的第一个字节。获取这个第一个字节允许istringstream跳过您的句子前面的空格。然后,您可以放回该字节,然后将其余的输入作为随机句子。

然后可以解析前两个字符串(同样用istringstream)以获取所需的整数值。

int main() { 
    std::string line, first, second, sentence; 
    char just, byte; 
    int fill, width; 

    while (std::getline(std::cin, line)) { 
     std::istringstream iss(line); 
     if (iss >> first >> second >> just >> byte) { 
      iss.putback(byte); 
      std::getline(iss, sentence); 
      std::istringstream(first) >> fill; 
      std::istringstream(second) >> width; 
      //... 
     } else { 
      std::cerr << "invalid input: " << line << std::endl; 
     } 
    } 
} 
+0

请按照此[链接](http://ideone.com/fPFAsA)获取上述代码的演示。 – jxh

相关问题