【问题标题】:How to obtain the whole integers from file has strings and integers and store them into array in C++?如何从具有字符串和整数的文件中获取整个整数并将它们存储到 C++ 中的数组中?
【发布时间】:2020-02-07 07:20:35
【问题描述】:
我想从也有字符串的文件中获取整数,并将它们存储到数组中以对它们进行一些操作。整数可以是 1 或 12 或 234,所以 3 位。我正在尝试这样做,但是当我运行代码时输出停止
void GetNumFromFile (ifstream &file1, char & contents)
{
int digits[20];
file1.get(contents);
while(!file1.eof())
{
for (int n = 0; n < 10; n++)
{
if(('0' <= contents && contents <= '9') && ('0' >= contents+1 && contents+1 > '9'));
digits[n]=contents;
if(('0' <= contents && contents <= '9') && ('0' <= contents+1 && contents+1 < '9'));
digits[n]=contents;
if(('0' <= contents && contents <= '9') && ('0' <= contents+1 && contents+1 <= '9') && ('0' <= contents+2 && contents+2 < '9'));
digits[n]=contents;
}
continue;
}
for (int i = 0; i <= 20; i++)
{
cout << *(digits + i) << endl;
}
}
【问题讨论】:
-
-
你的代码有很多错误,你最好的下一步可能是自己去do some debugging。一旦你看到更具体的问题,看看它们是否已经在 SO 上得到解决,如果没有,请寻求解决方案。
标签:
c++
arrays
file
stream
fstream
【解决方案1】:
你必须处理找到的数字的位数:
int digits[20];
int i = 0;
short int aux[3]; // to format each digit of the numbers
ifstream file1("filepath");
char contents;
file1.get(contents); //first char
if (!file1.eof()) //test if you have only one char in the file
{
while (!file1.eof() && i < 20) // limit added to read only 20 numbers
{
if (contents <= '9' && contents >= '0') // if character is in number range
{
aux[0] = contents - '0'; // converting the char to the right integer
file1.get(contents);
if (contents <= '9' && contents >= '0') // if contents is number, continue on
{
aux[1] = contents - '0';
if (!file1.eof()) // if has mor char to read, continue on
{
file1.get(contents);
if (contents <= '9' && contents >= '0') // if is integer, continue on
{
aux[2] = contents - '0';
file1.get(contents); // will read same of last char if eof, but will have no effect at all
//aux[0] *= 100; // define houndred
//aux[1] *= 10; // define ten
digits[i++] = (aux[0] * 100) + (aux[1] * 10) + aux[2];
}
else
{
//aux[0] *= 10; // define ten
digits[i++] = (aux[0] * 10) + aux[1];
}
}
else
{
digits[i++] = (aux[0] * 10) + aux[1];
}
}
else
{
digits[i++] = aux[0];
}
}
}
}
else if (contents <= '9' && contents >= '0' && i < 20) // check if the only one char is number
{
digits[i++} = contents - '0';
}
如果要读取未定义的大小数字,则必须分配内存以使用 new (c++) 或 malloc(c/c++) 格式化数字的每个数字。
【解决方案2】:
您首先需要在循环内添加get() 功能,以便到达文件末尾。
一旦发现char 是整数后,再尝试添加一个while 循环以继续请求下一个字符。
例如
int digits[20];
int i = 0;
ifstream file1("filepath");
char contents;
while (!file1.eof())
{
file1.get(contents); // get the next character
if (contents <= '9' && contents >= '0' && i < 20) // if character is in number range
{
digits[i++] = contents - '0'; // converting the chat to the right integer
file1.get(contents);
while (contents <= '9' && contents >= '0' && i < 20) // while is integer continue on
{
digits[i++] = contents - '0';
file1.get(contents);
}
}
}
// do other stuff here
【解决方案3】:
第一个观察:你迭代出数组的边界:
int digits[20];
for (int i = 0; i <= 20; i++)
20 个元素和 21 次迭代。这是一个未定义的行为,所以这里一切皆有可能(如果你的程序最终到达这里)。
接下来,您从文件中读取一次,然后您将进入无限循环,因为表达式!file1.eof() 在程序运行的其余部分中要么为真要么为假。这不是“输出停止”的原因吗?
第三个发现:你的if 语句没有用,因为语句后面有分号:
if(('0' <= contents && contents <= '9') && ('0' >= contents+1 && contents+1 > '9'));
digits[n]=contents;
您只需分配digits[n]=contents;,无需任何检查。
我看不出有任何理由在函数中提供对 char 的引用。为什么不让它成为一个局部变量呢?