Guest_imported
New member
- Jan 1, 1970
- 0
Hi,
ok, here is what I am trying to accomplish here.
I heard that the function Fread(), can automatically read a data block in the size of the structure from the Input file and assigns the data to the appropriate element of the structure. Well that really got me excited, it seem like it can make my work easier.
So Im trying it, but I can't seem to get this piece of code to work. Can you experts take a look at it for me please to see what I am doing wrong? Orbetter yet, if you know another why to do what I wish to accomplish here, please share it with me. And include sample too. Thankx in advance.
#include<iostream>
#include<fstream>
#include<string>
#include<stdio.h>
using namespace std;
struct Book
{ string Isbn;
string Title;
float Price;
int StockAmnt;
};
void main()
{
Book Dat[1];
FILE *Infile; \\This is like ifstream Infile
if((Infile=fopen("program4.txt","rb")==NULL)
{cout<<"Cant open file"; exit(1);}
if(fread(&Dat,sizeof(Book),4,Infile)!=4)
{ cout<<"Cant read data!";
}
}
ok, here is what I am trying to accomplish here.
I heard that the function Fread(), can automatically read a data block in the size of the structure from the Input file and assigns the data to the appropriate element of the structure. Well that really got me excited, it seem like it can make my work easier.
So Im trying it, but I can't seem to get this piece of code to work. Can you experts take a look at it for me please to see what I am doing wrong? Orbetter yet, if you know another why to do what I wish to accomplish here, please share it with me. And include sample too. Thankx in advance.
#include<iostream>
#include<fstream>
#include<string>
#include<stdio.h>
using namespace std;
struct Book
{ string Isbn;
string Title;
float Price;
int StockAmnt;
};
void main()
{
Book Dat[1];
FILE *Infile; \\This is like ifstream Infile
if((Infile=fopen("program4.txt","rb")==NULL)
{cout<<"Cant open file"; exit(1);}
if(fread(&Dat,sizeof(Book),4,Infile)!=4)
{ cout<<"Cant read data!";
}
}