2016-11-11 74 views
1

我有一个整数行文件。我想将每行读入阵列中的一个插槽。我有下面的代码,但它不起作用。我不确定我是否在正确的轨道上。从文件读取数据并将每行存储在数组中?

void Read_Save() { 
    ifstream in; 
    int arr[100]; 
    string line; 
    in.open("file.txt"); 
    while (in.peek() != EOF) 
    { 
     getline(in, line, '\n'); 
     strcpy(arr, line.c_str()); 
    } 
    in.clear(); in.close(); 
} 
+2

你不能输入整数与'strcpy' – Raindrop7

+2

的阵列,同时在'而(!in.peek()= EOF)'会的工作,并在阅读EOF陷阱之前,对你没有陷入EOF测试中,你可能会发现'while(getline(in,line,'\ n'))'更好,因为它为你节省了'peek',并且捕获了比只是EOF。 – user4581301

回答

2

有几种方法可以从字符串中解析出整数值。

首先,让我们来解决你的循环:

int pos = 0; 
while(std::getline(in, line) && pos < 100) 
{ 
    int value = 0; 

    // Insert chosen parsing method here 

    arr[pos++] = value; 
} 

下面是常用选项的不完全名单:

  1. 使用std::strtol

    // Will return 0 on error (indistinguishable from parsing actual 0) 
    value = std::strtol(line.c_str(), nullptr, 10 ); 
    
  2. 使用std::stoi

    // Will throw exception on error 
    value = std::stoi(line); 
    
  3. 建立一个std::istringstream并从中读取:

    std::istringstream iss(line); 
    iss >> value; 
    if(!iss) { 
        // Failed to parse value. 
    } 
    
  4. 使用std::sscanf

    if(1 != std::sscanf(line.c_str(), "%d", &value)) 
    { 
        // Failed to parse value. 
    } 
    

现在,请注意在循环检查pos < 100边界测试。这是因为您的阵列具有存储限制。实际上,你也用Read_Save中的本地地图覆盖了全局地图,因此用一个更小的数组隐藏它,当函数结束时会丢失一个更小的数组。

使用标准库提供的其他容器类型,可以有一个任意大小的“数组”(实际上不是数组)。提供随机访问的有用的是std::vectorstd::deque。让我们用向量和改变Read_Save的定义是有点更加有用:

std::vector<int> Read_Save(std::istream & in) 
{ 
    std::vector<int> values; 
    std::string line; 

    for(int line_number = 1; getline(in, line); line_number++) 
    { 
     try { 
      int value = std::stoi(line); 
      values.push_back(value); 
     } 
     catch(std::bad_alloc & e) 
     { 
      std::cerr << "Error (line " << line_number << "): Out of memory!" << std::endl; 
      throw e; 
     } 
     catch(std::exception & e) 
     { 
      std::cerr << "Error (line " << line_number << "): " << e.what() << std::endl; 
     } 
    } 

    return values; 
} 

最后,该呼叫将变为:

std::ifstream in("file.txt"); 
std::vector<int> values = Read_Save(in); 
0

在你的情况下最好的办法是使用std::vector。代码如下所示:

void Read_Save() 
{ 
    std::ifstream in("file.txt"); 
    int value; 
    std::vector<int> arr; 

    while (in >> value) 
     arr.push_back(value); 

    for(int i(0); i < arr.size(); i++) 
     std::cout << arr[i] << ", "; 

    std::cout << std::endl; 
    in.close(); 
} 
+1

或者,您可以使用'std :: istream_iterator'和'std :: back_inserter'作为迭代器来替换'std :: copy()'手动'while'循环,例如:'std :: copy(std :: istream_iterator (in),std :: istream_iterator(),std :: back_inserter(arr));' –

1

不能使用strcpy()将字符串转换为整数。您可以使用std::strtol()std::stoi(),甚至std::istringstream,如:

int arr[1000]; 

void Read_Save() { 
    ifstream in; 
    string line; 
    in.open("file.txt"); 
    int index = 0; 
    while ((index < 1000) && (getline(in, line))) 
    { 
     if (istringstream(line) >> arr[index]) 
      ++index; 
    } 
} 
+0

我喜欢你做它的方式。我得到这个错误虽然 错误\t C2027 \t使用未定义类型'std :: basic_istringstream ,std :: allocator Jaden

+1

您是否添加了istringstream的include?我的意思是'#包括' – drescherjm

相关问题