从C++中的文件中读取IP地址

use*_*947 0 c++ file-io

我正在尝试从文本文件中读取几千个IPv4地址并将它们放入C++字符串数组中.这是我到目前为止所拥有的

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

int main()
{
    string fileName1,fileName2;
    fileName1="file1Large.txt";
    ifstream inData;
    inData.open(fileName1);
    string file1[10000];
    int i=0;
    while(inData)
    {
        inData>>file1[i];
        i++;
    }
    cout<<"File1: "<<endl;
    for(int z=0; z <= 10000; z++)
    {
        cout<<file1[z];
    }
    inData.close();
    system("pause");
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我得到一个未处理的异常,我不确定是什么问题.我主要使用Java而不是很多C++,所以如果我遗漏了一些明显的东西,我会道歉.

Abh*_*sal 5

你的数组可能超出范围.

尝试:

while(i < 10000 && inData >> file1[i])
    {
        i++;
    }
Run Code Online (Sandbox Code Playgroud)

此外,这肯定是一个问题:

for(int z=0; z < i; z++) // remove = and change 10000 to i
    {
        cout<<file1[z];
    }
Run Code Online (Sandbox Code Playgroud)

编辑:正如Dave Rager所指出的,迭代的最大值应该是i而不是10000.