从文件

时间:2015-05-08 15:32:02

标签: c++ file file-handling filehandle

 在

4
5
6
7

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

int main()
{
    int data[4], a, b, c, d, e, f;
    ifstream myfile;
    myfile.open("tera.txt");
    for (int i = 0; i < 4; i++)
    {
        myfile >> data[i];
    }
    myfile.close();
    a = data[0];
    b = data[1];
    c = data[2];
    d = data[3];
    cout << a << "\t" << b << "\t" << c << "\t" << d << "\n";
    return 0;
}

它也需要AT并给出垃圾值。我应该如何以及在哪里使用ignore函数来忽略AT值。 如果有另一个数组给BT包含一些像这样的值,那么还有一件事: 在BT 如何将BT的所有值存储在数组中?

1 个答案:

答案 0 :(得分:0)

你只需要跳过第一行。您还可以添加可选的错误处理,否则读取可能会失败。

if (!myfile)
{
    cout << "can't open\n";
    return 0;
}

string temp;
myfile >> temp;
cout << "first line: " << temp << endl;
for (int i = 0; i < 4; i++)
{
    myfile >> data[i];
    if (myfile.fail())
    {
        cout << "error\n";
        myfile.clear();
        myfile.ignore(1000000, '\n');
    }
}