从C 中的文本提取数字---分割故障

extract numbers from text in C++ --- segmentation fault

本文关键字:数字 分割 故障 提取 文本      更新时间:2023-10-16

我有一个包含数字的文本:148,147,148,146,135,22,....几乎有11000个数字。我想将每个奇数的数字(第一个,第三个...)放在阵列(148,148,135 ...)中,而每个偶数均匀的数字(第二,第四...,即147,146,22)...)。运行以下代码我会遇到细分故障错误,我不确定数字是否正确在数组中。你能帮我吗?

 #include <stdio.h>
 #include <stdlib.h>
 #include <fstream>
 #include <iostream>
 #include <sstream>
 #include <string>
 using namespace std;

 int main(){
 ifstream infile;
 int arraya[11000];
 int arrayb[11000];
 int i=0;
 int noum=0;
 char cNum[10];
            infile.open ("pairs.txt", ifstream::in);
            if (infile.is_open())
            {
                    while (infile.good())
                    {
                        infile.getline(cNum, 256, ',');
                        if ( i % 2== 0 )
                        arraya[i]= atoi(cNum); 
                        else
                        arrayb[i]= atoi(cNum) ;                 
                        i++ ;
                    }
                    infile.close();
            }
            else
            {
                    cout << "Error opening file";
            }
 for (i=0; i<10;i++){
 cout<<arraya[i];
 cout<<",";
 cout<<arrayb[i];}
 return 0;
}

修改

while (infile.good())
{
    infile.getline(cNum, 256, ',');

进入

while (infile.getline(cNum, 10, ',')) {

因为cNum的大小仅为10。

另外,正如@synapse提到的那样,您在数组边界上写下。您需要

if ( noum % 2== 0 )
    arraya[i/2]= atoi(cNum); 
else
    arrayb[i/2]= atoi(cNum);                 
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
    ifstream infile;
    int arraya[6000];
    int arrayb[6000];
    int iA = 0;
    int iB = 0;
    int noum = 0;
    char cNum[10];
    infile.open("pairs.txt", ifstream::in);
    if (infile.is_open()) {
        while (infile.getline(cNum, 256, ',')) {
            noum = atoi(cNum);
            if (noum % 2 == 0)
                arraya[iA++] = noum;
            else
                arrayb[iB++] = noum;
        }
        infile.close();
    } else {
        cout << "Error opening file";
    }
    cout << "Even: " << endl;
    for (int i = 0; i < iA; i++) {
        cout << arraya[i];
        cout << ",";
    }
    cout << std::endl;
    cout << "Odd: " << endl;
    for (int i = 0; i < iB; i++) {
        cout << arrayb[i];
        cout << ",";
    }
    return 0;
}

您需要拥有一个布尔变量,该变量会声明该数字是偶数或奇数,它也可以通过迭代器来解决,并检查它是否是%2 == 0,这只是其中一个应用程序:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
    ifstream text;
    text.open("yourfile", ifstream::in);
    if (!text.is_open()) {
        cerr << "Error opening file.";
        return 1;
    }
    int arrayA[1000];
    int arrayB[1000];
    int iA = 0;
    int noum = 0;
    int iB = 0;
    bool odd = true;
    string cNum;
    while(text >> cNum) {
        noum = atoi(cNum.c_str());
        if (!odd) {
            arrayA[iA++] = noum;
            odd = true;
        } else {
            arrayB[iB++] = noum;
            odd = false;
        }
    }
    text.close();
    if (!odd)
        for (int i = 0; i < iA; i++)
            cout << arrayA[i] << ", " << arrayB[i] << endl;
    else
        for (int i = 0; i < iB; i++)
            cout << arrayA[i] << ", " << arrayB[i] << endl;
    return 0;
}