我有这种格式12345678-A数字加一些随机的数字和文本之间一个txt文件。我需要读取这个文件,并且只保存8位数字的整数。我该怎么做?C++ ifstream的只读整数
当前代码我有,作品,如果只是有一些数字:输入文本的
const int MAX = 1000;
ifstream file("file.txt");
int data;
int index = 0;
int bigdata[MAX];
while (!file.eof() && index < MAX)
{
file >> data;
if (data > 20000000 && data < 90000000)
{
bigdata[index] = data;
index++;
}
}
样品:
48251182-D 6,5 6
49315945-F 7 3
45647536-I 3,5 3
45652122-H 7 6,5
77751157-L 2 2,5
75106729-S 2 5
77789857-B 4 3 3,5 3
59932967-V 4 8,5
39533235-Q 8 8,5
45013275-A 5 2
48053435-Y 6 8
48015522-N 3,5 4
48015515-T
48118362-B 7,5 3,5
39931759-Q 5,5 3
39941188-D 3,5 1,5
39143874-I 3,5 4
48281181-O 6,5 6
其实你的代码部分反正打破。哪个资源教你编写'while(!file.eof())'? [我们需要纠正它](http://stackoverflow.com/q/5605125/560648)。 –
大概[此酮](http://stackoverflow.com/questions/2084265/reading-integers-from-a-text-file-with-words)上SO。 – 2016-04-14 12:42:08
@RawN:不太可能。这是SO展示该问题的数百万个晦涩难懂的答案之一,下面有一条评论说“不要这样做”。我期待被告知哪本书或教科书是错误的。 –