---
# System prepended metadata

title: 實做 - 檔案讀取函數

---

# 實做 - 檔案讀取函數
```
#include <iostream>
#include <fstream>
#include <cstdio>
#include <windows.h>
using namespace std;
int data();
int main()
{
    int a = data();
    cout << a << endl;
    return 0;
}

int data()
{
    fstream fin;
    fin.open("func.txt");
    if(!fin)
        return false;
    int n, i = -1;
    fin >> n;
    cout << n << endl;
    int *a = (int*) malloc(n * sizeof(int));
    double b;
    while(!fin.eof())
        {
            i++;
            if(i + 1 > n)
                return false;
            fin >> b;
            if( b >= 2147483647 || b <= -21474836477)
                return false;
            a[i] = b;
            cout << a[i] << endl;
        }
    if(i + 1 < n)
        return 0;
    return true;
}
```