fread() for file system
-
Hi to All, I have some problem in Logic, while reading a file (which has many objects of structure A,)...and buffering it to a dynamically allocated memory... What I am doing exactly is that....I need to read the file...and if there is any data available, then put it to a new structure (dynamical allocated memory)... Problem is ..<b>where do I declare/allocate memory for the structure</b>, I cannot declare in beginning of the loop, because fread() will overwrite into the same memory location... something like.... A *lpA = new A; while(fread(lpA,sizeof(lpA),1,ptr_myfile) != EOF) { ...do something(Add the pointer into a List) } Similar problem with declaring it inside the loop... fread() needs the first parameter...
----------------------------- I am a beginner
-
Hi to All, I have some problem in Logic, while reading a file (which has many objects of structure A,)...and buffering it to a dynamically allocated memory... What I am doing exactly is that....I need to read the file...and if there is any data available, then put it to a new structure (dynamical allocated memory)... Problem is ..<b>where do I declare/allocate memory for the structure</b>, I cannot declare in beginning of the loop, because fread() will overwrite into the same memory location... something like.... A *lpA = new A; while(fread(lpA,sizeof(lpA),1,ptr_myfile) != EOF) { ...do something(Add the pointer into a List) } Similar problem with declaring it inside the loop... fread() needs the first parameter...
----------------------------- I am a beginner
If you're using
C++
, an option maybe usignSTL
constainers, for instance avector
:std::vector <A> v;
A a;while (fread(&a, sizeof(a), 1, ptr_myfile) != EOF)
{
v.push_back(a);
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]