Size of File

I need to read two files with same name. but they have different contents.

first file has 60 bytes
the second file has 244 bytes

I can read the files no problem but i would want some error checking

I would appreciate if somebody could give me an example of how to write the if
statement to throw an catch the error if file opened is not equal to the bytes inside the file.

thanks for the help.


closed account (o3hC5Di1)
Hi there,

Could you please give a little more information on this:

rafae11 wrote:
if file opened is not equal to the bytes inside the file.


I'm not sure what you mean by that.

Thanks!

All the best,
NwN
If you simply want to get the file length to check which of the two files you've opened take a look at:

http://www.cplusplus.com/reference/iostream/istream/read/

The example shows how to get the length of a file by moving the file pointer to the end of the file and reading the position.
sorry should have read.
if file length is not equal to the bytes inside the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int main () {
  int length;
  char * buffer;

  ifstream is;
  is.open ("c:\\temp\\abc.bin", ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  length = is.tellg();

    if (length == 244)
    {
    // open file
    }
    else if(length ==60)
    {
   // open file
    }
    else
    {
    // display error dialog box and retry to open file
    }

  is.seekg (0, ios::beg);

  // allocate memory:
  buffer = new char [length];

  // read data as a block:
  is.read (buffer,length);
  is.close();

  cout.write (buffer,length);

  delete[] buffer;
  return 0;
}


this what i would want to do except i would want a dialog box to show up and then start again if i loaded a wrong file.

thanks for the help guys.
It's impossible to have two files of the same name in the same folder.
ok thanks.
Topic archived. No new replies allowed.