•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,612 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1760 | Replies: 1
![]() |
•
•
Join Date: Oct 2007
Posts: 23
Reputation:
Rep Power: 2
Solved Threads: 0
Hi, I have the following structure to read the file data with fixed format of my binary file. Now I want that every time my ReadFromFile function is called with 'counter' variable it should
1. return me frame specified in the 'counter'.
2. The file needs to be closed only when 'stop' has value say 'STOP'
3. The file should not be opened every time and be opened once only.
Thanks,
1. return me frame specified in the 'counter'.
2. The file needs to be closed only when 'stop' has value say 'STOP'
3. The file should not be opened every time and be opened once only.
Thanks,
struct Frame
{
double frame_no;
double dist[3072];
double ampl[3072];
};
struct UShortFrame
{
unsigned frame_no;
unsigned short dist[3072];
unsigned short ampl[3072];
};
void ReadFromFile(int counter, char* stop)
{
int i; static const char filename[] = "data.dat";
UShortFrame* usframe = new UShortFrame;
ifstream filein(filename, ios::in | ios::binary);
if(!filein) {
std::cout << "Cannot open file to read.\n"; }
if(filein.read((char*)usframe, sizeof(UShortFrame)))
{
Frame* frame = new Frame;
frame->frame_no = usframe->frame_no;
for (int i = 0; i < 3072; ++i)
{
frame->dist[i] = usframe->dist[i];
frame->ampl[i] = usframe->ampl[i];
}
delete []usframe;
}
filein.close();
} > UShortFrame* usframe = new UShortFrame;
> delete []usframe;
You didn't allocate an array, so drop the [ ] from the delete.
> 1. return me frame specified in the 'counter'.
> 2. The file needs to be closed only when 'stop' has value say 'STOP'
> 3. The file should not be opened every time and be opened once only.
Sounds like you need a 'FileFrame' class of some sort.
- the constructor opens the file
- the destructor closes the file
- a member function to get the next frame
> delete []usframe;
You didn't allocate an array, so drop the [ ] from the delete.
> 1. return me frame specified in the 'counter'.
> 2. The file needs to be closed only when 'stop' has value say 'STOP'
> 3. The file should not be opened every time and be opened once only.
Sounds like you need a 'FileFrame' class of some sort.
- the constructor opens the file
- the destructor closes the file
- a member function to get the next frame
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- write and read to file (C++)
- Read from a csv file (C)
- File Pointer in C (C)
- Read in a file and store in char array (C)
- C++: ifstream file pointer not getting reset (C++)
- file pointer increament (C)
Other Threads in the C++ Forum
- Previous Thread: C++ Hashing functions?
- Next Thread: output of graphics image in turbo c++



Linear Mode