2016-12-03 119 views
0

Iam尝试将包含整数的文本文件读取到整数数组中。 如果输入是:1 3 4 5 6 (中间有空格)它工作正常。将逗号分隔的文件读取到整数数组中

但是,如果输入是:1,3,4,5,6(逗号分隔)。它只是打印1.(第一位数字)。如果程序发现1,3,4,5,6作为单个实体那么它应该打印 1,3,4,5,6作为第一个索引ryt? 而且File >> x,这个表达式是否通过检测两者之间的空间来逐个获取值?

#include<iostream> 
#include<fstream> 
#include<string> 
using namespace std; 

int main() 
{  
    int n = 0; //n is the number of the integers in the file ==> 12 
    int num; 
    int arr[100]; 
    int x; 
    int sum = 0; 
    ifstream File; 
    File.open("integer.txt"); 
    if(!File.is_open()) 
    { 
     cout<<"It failed"<<endl; 
     return 0; 
    } 

    while(File>>x) 
    { 
     arr[n] = x; 
     n++; 
    } 

    File.close(); 
    cout<<"n : "<<n<<endl; 
    for(int i=0;i<n;i++) 
    { 
     cout << arr[i] << " "; 
    } 
    return 0; 
} 

回答

0

其最有可能的是,当你输入一个用逗号入文件时,它读取它作为一个整体的字符串。确保这些逗号也是逗号分隔符,我不知道你会在这个代码中放置逗号,但是你仍然需要一个分隔符。

//example function you can use. 
getline(ss, s, ',') 
-1

您是否尝试过使用sscanf和ifstream?下面

#include<iostream> 
    #include<fstream> 
    #include<string> 
    using namespace std; 

    int main() 
    { 
     int n = 0; //n is the number of the integers in the file ==> 12 
     int num; 
     int arr[100]; 
     char line[200]; 
     char *ptr=line; 
     int x=0,count=0; 
     int sum = 0; 
     ifstream File; 
     File.open("integer.txt"); 
     if(!File.is_open()) 
     { 
      cout<<"It failed"<<endl; 
      return 0; 
     } 
     File.getline(line,200); 

     while((count=sscanf(ptr,"%d,",&x))>0) 
     { 
      arr[n] = x; 
      n++; 
      ptr+=count+1; 
     } 
     File.close(); 
     cout<<"n : "<<n<<endl; 
     for(int i=0;i<n;i++) 
     { 
      cout << arr[i] << " "; 
     } 
     cout<<endl; 
     return 0; 
    } 
+0

ptr + = count + 1是跳过分隔符 – KingOfWigs

+0

请更新为什么这是downvoted? – KingOfWigs

+0

因为OP使用的是C++而不是C,而'sscanf'是危险且不安全的。你还添加了另一个魔术数组'char line [200]',不必要地用复杂的指针变量复杂化代码,并使用'getline'成员函数,所有这些都是非常糟糕的编码风格。 –

0

一个简单的例子,这里发生的事情是提取的第一个字母后,你的代码试图提取逗号为整数。因为它不能这样做,它会返回false并结束循环。

有一个非常类似的问题here

while循环应该是这样的:

while(File>>x) 
{ 
    arr[n] = x; 
    n++; 
    if (ss.peek() == ',') 
     ss.ignore(); 
} 
0

但如果输入为:1,3,4,5,6(逗号分隔)。其刚刚打印 (第一位数)。如果程序发现1,3,4,5,6作为一个单独的实体,那么它应该打印1,3,4,5,6作为第一个索引ryt?

由于您试图将“1,3,4,5,6”读入int对象,因此它仅打印“1”。然而,int不能是“1,3,4,5,6”。显然,只要到达第一个“坏”字符即逗号就停止解析,并且最终得到迄今为止建立的整数,即“1”。

输入的其余部分被丢弃。就好像您的线路是“1abcdef”或“1abcdef2345”。

而且File >> x,这个表达式是否通过 一个一个地检测值?

是的,这使得它非常不灵活。

我推荐使用std::getline代替operator>>,而不是用','作为分隔符。虽然函数的名称不再有意义,因为它不再读取(因为它使用默认分隔符'\n'),它将工作得很好。

您将以个人std::string结尾,容易使用std::stoi转换为int s。

当你在它的时候,摆脱原始int arr[100]并使其成为std::vector<int>,以便你不限于100个元素。无论如何,100是一个丑陋的魔法(任意)数字。

下面是一个例子:正如你所看到的,我也采取了提出一些其他优秀的C++的做法,如避免using namespace stdstd::endl机会

#include <iostream> 
#include <sstream> 
#include <string> 
#include <vector> 

int main() 
{ 
    // faking some test file input; behaves like an `std::ifstream`: 
    std::istringstream is("1,2,3,4,5"); 

    std::vector<int> numbers; 

    std::string number_as_string; 
    while (std::getline(is, number_as_string, ',')) 
    { 
     numbers.push_back(std::stoi(number_as_string)); 
    } 

    std::cout << "n : " << numbers.size() << "\n"; 
    for(auto&& number : numbers) 
    { 
     std::cout << number << "\n"; 
    } 
} 

相关问题