Detecting the end of the file
-
Hello. These are the codes that I have used to read a bin file and to store in 3 variables. Most of the files read have 1024 columns, hence iI < 1024. If i try to open a file that is less than 1024, the program will give me unexpected file format error. How can detect the end of the file and break from the loop if a file of less than 1024 column is opened? Thanks.
void CVecDoc::Serialize(CArchive& ar) { int iI, iK; unsigned char cDataOne, cDataTwo; short sSwap, as[3]; if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here for (iI = 0; iI < 1024; iI++) { for (iK = 0; iK < 3; iK++) { ar >> cDataOne >> cDataTwo; sSwap = ((short)cDataTwo << 8) + (short)cDataOne; as[iK] = sSwap; m_a1[iI] = as[0]; m_a2[iI] = as[1]; m_a3[iI] = as[2]; } } } }
-
Hello. These are the codes that I have used to read a bin file and to store in 3 variables. Most of the files read have 1024 columns, hence iI < 1024. If i try to open a file that is less than 1024, the program will give me unexpected file format error. How can detect the end of the file and break from the loop if a file of less than 1024 column is opened? Thanks.
void CVecDoc::Serialize(CArchive& ar) { int iI, iK; unsigned char cDataOne, cDataTwo; short sSwap, as[3]; if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here for (iI = 0; iI < 1024; iI++) { for (iK = 0; iK < 3; iK++) { ar >> cDataOne >> cDataTwo; sSwap = ((short)cDataTwo << 8) + (short)cDataOne; as[iK] = sSwap; m_a1[iI] = as[0]; m_a2[iI] = as[1]; m_a3[iI] = as[2]; } } } }
Hi, There is an end of file function eof() which you can use. It is a boolean function which returns a non-zero number when the end of file is reached otherwise it returns zero (meaning false). You will have to use a stream variable to use the function. A very simple article explaining the function is here: http://www.mathbits.org/MathBits/CompSci/Files/End.htm Hope this helps. - Moonis
-
Hello. These are the codes that I have used to read a bin file and to store in 3 variables. Most of the files read have 1024 columns, hence iI < 1024. If i try to open a file that is less than 1024, the program will give me unexpected file format error. How can detect the end of the file and break from the loop if a file of less than 1024 column is opened? Thanks.
void CVecDoc::Serialize(CArchive& ar) { int iI, iK; unsigned char cDataOne, cDataTwo; short sSwap, as[3]; if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here for (iI = 0; iI < 1024; iI++) { for (iK = 0; iK < 3; iK++) { ar >> cDataOne >> cDataTwo; sSwap = ((short)cDataTwo << 8) + (short)cDataOne; as[iK] = sSwap; m_a1[iI] = as[0]; m_a2[iI] = as[1]; m_a3[iI] = as[2]; } } } }
Aint wrote:
Most of the files read have 1024 columns... How can...break from the loop if a file of less than 1024 column is opened?
Which is a concept known only to your code. It's up to you to determine if the file contains 1024 columns or not.
Aint wrote:
If i try to open a file that is less than 1024, the program will give me unexpected file format error.
Have you stepped through the code to find out at what point this error occurs?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Aint wrote:
Most of the files read have 1024 columns... How can...break from the loop if a file of less than 1024 column is opened?
Which is a concept known only to your code. It's up to you to determine if the file contains 1024 columns or not.
Aint wrote:
If i try to open a file that is less than 1024, the program will give me unexpected file format error.
Have you stepped through the code to find out at what point this error occurs?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Yes. It shows that the error occur before the loop end, when iI = 300+. Can I use ar.??? to detect the end of the file?
Aint wrote:
It shows that the error occur before the loop end, when iI = 300+.
How large are
m_a1
,m_a2
, andm_a3
?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne