Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从文件c+中读取数字+;_C++ - Fatal编程技术网

C++ 从文件c+中读取数字+;

C++ 从文件c+中读取数字+;,c++,C++,我想从文本文件中读取数字。 该文件包含以下数字 35 7 9 20 25 30 40 55 56 57 60 62 1 4 7 11 14 25 44 47 55 57 100 -5 100 1000 1005 -12 1000 1001 空格分隔数字。 我必须编写一些代码来阅读这篇文章,但问题是读取行末尾的数字是不正确的,我不知道如何正确读取它们(CRLF字符的问题); 有人能帮我吗? 这是我的密码 标题 base.h #包括 #包括 #包括 #包括 使用名称空间std; L类 { 公众:

我想从文本文件中读取数字。 该文件包含以下数字

35 7 9 20 25 30 40 55 56 57 60 62
1 4 7 11 14 25 44 47 55 57 100
-5 100 1000 1005
-12 1000 1001

空格分隔数字。 我必须编写一些代码来阅读这篇文章,但问题是读取行末尾的数字是不正确的,我不知道如何正确读取它们(CRLF字符的问题); 有人能帮我吗? 这是我的密码

标题 base.h

#包括
#包括
#包括
#包括
使用名称空间std;
L类
{
公众:
int值;
};
t类
{
公众:
L*线;
t(){
行=新L[10000];
}
};
巴扎级
{
公众:
如果输入;
流出的液体;
弦线;
char-word1[1];
int-wordint;
字符标签[255];
int*tab2;
int x;
int-y;
intz;
INTA;
int b;
INTC;
内部文件;
整数乘法器;
布尔阴性;
int负数;
t*表;
base();
};
下面是base.cpp

#include "base.h"

base::base()
{
    wordint=0;
    a=2000;
    b=0;
    tab2 =new int [a];
    x=0;
    y=0;
    z=0;

    for(int i=0;i<a;i++)
    {
        tab2[i]=0;

    }
    multiplier=1;
    negative =false;
    negative1=-1;
    lines="0";
    ile=0;
    in.open("in.txt");   //open file

    do   
    {
        getline(in,lines);
        if(lines!="0")      //how many lines is in the file
        {
            ile++;
        }
    } while(in.eof()!=true);
    in.close();
    tablica =new t[ile];
    tab5 = new int[ile];
    in.open("in.txt");

    for(int i=0;i<ile;i++)
    {
        do
        {
            in.get(word1[0]);
            if(word1[0]==45)   // check if sign is negative
            {
                word1[0]=0;
                negative=true;
                in.get(word1[0]);
            }
            if(word1[0]!= 10)  //check if  not LF
            {
                if(word1[0]!=' ')  //check if not space
                {
                    tab[x]= word1[0];
                    x++;
                }
            }
            if(word1[0]==' ')  //if space then convert from char to int 
            {
                y=x-1;
                do {
                    word1[0]=tab[y];
                    wordint=atoi(word1);
                    wordint=wordint*multiplier;
                    tab2[z]=tab2[z]+wordint;
                    y--;
                    multiplier=multiplier*10;
                } while(y>=0);
                x=0;
                multiplier=1;
                if(negative==true)  //check if negative
                {
                    tab2[z]=tab2[z]*negative1;
                    negative=false;
                }
                z++;
                c=z;
                tab5[i]=c;
            }
            /**if(word1[0]== '10')  //its not work but same as above only check if LF
            {
                y=x-1;
                do{
                    word1[0]=tab[y];
                    wordint=atoi(word1);
                    wordint=wordint*multiplier;
                    cout<<"dupa";
                    tab2[z]=tab2[z]+wordint;
                    y--;
                    multiplier=multiplier*10;
                } while(y>=0);
                x=0;
                multiplier=1;
                if(negative==true)  //check if negative
                {
                    tab2[z]=tab2[z]*negative1;
                    negative=false;
                }
                z++;
                c=z;
                tab5[i]=c;
            }*/

        } while(word1[0]!=10);
        for(int j=0;j<z;j++)      //write to table
        {
            table[i].line[j].value=tab2[j];
            cout<< table[i].line[j].value<<endl;
            tab2[j]=0;
        }
        z=0;
    }
}
#包括“base.h”
base::base()
{
wordint=0;
a=2000;
b=0;
tab2=新整数[a];
x=0;
y=0;
z=0;

对于(int i=0;i这将把文件中的所有数字读入一个
std::vector

#包括
#包括
#包括
使用名称空间std;
矢量读取编号(字符串文件名)
{
河流充填;
infle.open(文件名);
向量数;
if(infle.is_open())
{
int-num;
而(填充>>num)
{
数字。推回(num);
}
}
返回号码;
}

您应该简单地结合以下示例:

  • 首先,读取文件的所有行(使用)
  • 然后,拆分得到的字符串(使用)
  • 最后,将每个元素转换为一个数字(使用)

您的代码看起来比您发布的代码要聪明得多,应该可以工作。

尝试发布一个简短、自包含、可编译的示例,为什么不直接调用文件上的
istream::operator>
?顺便说一句,在上给出的公认答案似乎由于效率低下和不可伸缩性而在评论部分引起了一些激烈的讨论。
#include "base.h"

base::base()
{
    wordint=0;
    a=2000;
    b=0;
    tab2 =new int [a];
    x=0;
    y=0;
    z=0;

    for(int i=0;i<a;i++)
    {
        tab2[i]=0;

    }
    multiplier=1;
    negative =false;
    negative1=-1;
    lines="0";
    ile=0;
    in.open("in.txt");   //open file

    do   
    {
        getline(in,lines);
        if(lines!="0")      //how many lines is in the file
        {
            ile++;
        }
    } while(in.eof()!=true);
    in.close();
    tablica =new t[ile];
    tab5 = new int[ile];
    in.open("in.txt");

    for(int i=0;i<ile;i++)
    {
        do
        {
            in.get(word1[0]);
            if(word1[0]==45)   // check if sign is negative
            {
                word1[0]=0;
                negative=true;
                in.get(word1[0]);
            }
            if(word1[0]!= 10)  //check if  not LF
            {
                if(word1[0]!=' ')  //check if not space
                {
                    tab[x]= word1[0];
                    x++;
                }
            }
            if(word1[0]==' ')  //if space then convert from char to int 
            {
                y=x-1;
                do {
                    word1[0]=tab[y];
                    wordint=atoi(word1);
                    wordint=wordint*multiplier;
                    tab2[z]=tab2[z]+wordint;
                    y--;
                    multiplier=multiplier*10;
                } while(y>=0);
                x=0;
                multiplier=1;
                if(negative==true)  //check if negative
                {
                    tab2[z]=tab2[z]*negative1;
                    negative=false;
                }
                z++;
                c=z;
                tab5[i]=c;
            }
            /**if(word1[0]== '10')  //its not work but same as above only check if LF
            {
                y=x-1;
                do{
                    word1[0]=tab[y];
                    wordint=atoi(word1);
                    wordint=wordint*multiplier;
                    cout<<"dupa";
                    tab2[z]=tab2[z]+wordint;
                    y--;
                    multiplier=multiplier*10;
                } while(y>=0);
                x=0;
                multiplier=1;
                if(negative==true)  //check if negative
                {
                    tab2[z]=tab2[z]*negative1;
                    negative=false;
                }
                z++;
                c=z;
                tab5[i]=c;
            }*/

        } while(word1[0]!=10);
        for(int j=0;j<z;j++)      //write to table
        {
            table[i].line[j].value=tab2[j];
            cout<< table[i].line[j].value<<endl;
            tab2[j]=0;
        }
        z=0;
    }
}
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

vector<int> read_numbers(string file_name)
{
    ifstream infile;
    infile.open(file_name);
    vector<int> numbers;

    if (infile.is_open())
    {
        int num; 
        while(infile >> num)
        {
            numbers.push_back(num);
        }
    }

    return numbers;
}