mercredi 29 avril 2015

runtime Error in c++ VS

I have a code for saving file and it works in code blocks but in VS it gives me a run time error. Here is my code:

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    struct st
    {
        int a;
        int b;
    };
    st temp;
    FILE *f;f=fopen("c:\\data1.dat","w");

    while (1)
    {
        cout << "enter a and b";
        cin >> temp.a;
        cin >> temp.b;
        fwrite(&temp, sizeof(st), 1, f);
        if (temp.a == 0)break;
    }
    fclose(f);
    f = fopen("c:\\data1.dat", "r");
    st temp2;
    while (fread(&temp2, sizeof(st), 1, f))
    {
        cout << temp2.a << endl;
        cout << temp2.b << endl;
    }
    fclose(f);
}

Does anybody know what is the problem? Thanks

Aucun commentaire:

Enregistrer un commentaire