| | |
Move file pointer to read next file value
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 23
Reputation:
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,
C++ Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- C++: ifstream file pointer not getting reset (C++)
- 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)
- file pointer increament (C)
Other Threads in the C++ Forum
- Previous Thread: C++ Hashing functions?
- Next Thread: output of graphics image in turbo c++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






