2015-06-27 73 views
1

我想通过阅读文件来打印整数值。无法打印所有整数值

代码:

int temp; 
    char* trainname; 
    trainname="dfg.txt"; 
    ifstream trainfile; 
    trainfile.open(trainname); 
    if(!trainfile){ 
     cout<<"Cannot open file!"<<'\n'; 
     exit(1); 
    } 
    while(trainfile >> temp) 
     cout << temp << " "; 
    trainfile.close(); 

dfg.txt:1 2 we er rf 5

输出:1 2

的问题是,它不打印5

+1

你为什么不阅读你正在尝试使用的函数的文档? –

回答

4

阅读到一个临时字符串,然后再使用std::stoi尝试从中解析一个整数,如果成功,它输出:

std::string temp; 

while(trainfile >> temp) { 
    try { 
     std::cout << std::stoi(temp) << " "; 
    } 
    catch(const std::invalid_argument&) { 
     // not a valid number 
    } 
} 
+1

仍然使用输出运算符<<而不是运算符>>读取。 ;) – Pixelchemist

+0

谢谢。可能这种解决方案是正确的。但是,函数'stoi'无法解析。虽然我加了'#include '和国旗'-std = C++ 11'。 – srcn

+1

@srcn - 适用于我,铿锵++和g ++。你使用什么编译器?然而,这个解决方案给出一个包含'6.1','7'的单词,给出一个包含'7abc890'的单词。这是否是一种有效的解释取决于你。如果'7abc890'无效,那么0x7abc890呢?那么'077'(以及哪个整数)呢? “0xdzdz”或“088”怎么样? –

0
string temp; 

if('0' <= temp[0] && temp[0]<='9') 

     cout << temp << " "; 

它会工作,我想。

1
while(trainfile >> temp) 
    cout << temp << " "; 

上述集上trainfilefailbit在遇到不是空白或数字的任何字符。这终止了循环。这是我倾向于不使用可能在输入流上失败的格式化I/O的原因之一。我发现最好是将文本读取为文本(而不是数字),然后处理刚才读取的字符串。例如,请参阅zenith的答案。

如果你坚持要从输入流中做所有事情,你需要一个外部循环来清除流的failbit。例如,

while (! std::cin.eof()) 
{ 
    while (std::cin >> temp) 
    { 
     std::cout << temp << " "; 
    } 
    std::cin.clear(); 
    std::cin.ignore(); 
} 

鉴于含有1 2 we er rf 5的输入文件,上述将打印1 2 5。如果输入文件包含1 2 abc345def 6,以上将打印1 2 345 6。请注意,天顶的方法将打印1 2 6abcdef夹在345之间是否以整数计为您自己。


我推荐使用zenith的解决方案。


更新:
上面解释abc345def作为表示整数345。 Zenith的解决方案和上面的解释345def代表整数345。对我而言,abc345def345def应该被拒绝为代表整数。所以应该6.1,但0x abc345def没有错。 C标准库中有很好的工具,strtol,很好地解析整数。它还表明是什么使解析停止。对于一个有效的整数,它应该停止在输入字符串的末尾。就这样,

#include <iostream> 
#include < fstream> 
#include <string> 
#include <cstdlib> 

int main() 
{ 
    std::ifstream trainfile("dfg.txt"); 
    if (!trainfile) 
    { 
     std::cerr << "Cannot open file!\n"; 
     exit(1); 
    } 

    std::string s; 
    while(trainfile >> s) 
    { 
     char* end; 
     long num = std::strtol (s.data(), &end, 0); 
     if (!*end) 
     { 
      std::cout << num << " "; 
     } 
    } 
    trainfile.close(); 
    std::cout << "\n"; 
} 
0

这里是你可以考虑─

#include <iostream> 
#include <sstream> 
#include <fstream> 

using namespace std; 

int main() 
{ 
    ifstream trainname("dfg.txt",ios::in); 
    string temp; 

    getline(trainname,temp); 
    stringstream str; 
    str<<temp; 

    int extract_int; 

    while(getline(str, temp,' ')) 
    { 
     if(stringstream(temp)>>extract_int) 
      cout<<extract_int<<" "; 
    } 

    return 0; 
} 

或者根据大卫Hammen的回答,你可以解决它的另一种方式如下way-

#include <iostream> 
#include <sstream> 
#include <fstream> 

using namespace std; 

int main() 
{ 
    int temp; 
    char* trainname; 
    trainname="dfg.txt"; 
    ifstream trainfile; 
    trainfile.open(trainname); 

    if(!trainfile){ 
     cout<<"Cannot open file!"<<'\n'; 
     exit(1); 
    } 

    while (!trainfile.eof()) 
    { 
     while (trainfile>>temp) 
      cout<<temp<< " "; 

     trainfile.clear(); 
     trainfile.ignore(); 
    } 

    return 0; 
}