Hi,
Iam using filestreams to create binary file (c:\..\abc.bin).I want to put some checksum when i try to open the file through programming ,like if it is previously modified through another application or done manually.At the point of opening the binary file i need to perform some checksum by checking it's size.but how we know the size of the file before corrupted. (or) can any one tell me how we perform checksum like wether the given file is corrupted or not..just give me the logic..
thanks in advance.....

Recommended Answers

All 8 Replies

#include <iostream>
#include <fstream>

int FileSize(std::string Path)
{
    std::ifstream Stream(Path.c_str());
    if (Stream.is_open())
    {
        int Start = Stream.tellg();
        Stream.seekg(0, std::ios::end);
        int End = Stream.tellg();
        Stream.close();
        return (End - Begin);
    }
    return -1;
}

int main()
{
    std::cout<<FileSize("C:/Users/Foo/Desktop/SomeFile.bin")<<std::endl;
}
commented: just provide idea...you are not teaching here by giving code...it's not good.. +0

thanks

but i know about knowing the size of bin file.my dobut is how can we check weather a file is modified or not(for bin file based on size) .we need to compare the file size with prevoiusly stord size and if any changes occur we display that the file is corrupted..

Generate a hash (checksum) value using such algorithms as MD5, SHA1 etc. on the original file.

To later verify whether or not the file has been altered, you must run the file thru the algorithm again. You will get a different hash value if one single byte of the file has been changed. This procedure is often used for file downloads. That is, the website will publish the hash value of a file to be downloaded. The recipient of the downloaded file can run the downloaded file thru the hashing algorithm to generate the hash value. If the recipients hash value is the same as the websites hash value then the file has not been changed thru download corruption or possibly a man in the middle attack.

thank u
but i new to this environment. Let me know how we use such algorithem like MD5 etc.plz explain with simple example.

how i use these algorithem with my file is coded as following
[

#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std;
class Entry 
{
public:

    char name[50];
    int id;
    float sal;
    bool b;
     Entry *next;
     void clear(Entry *head,Entry *tail)
     {
         while(head!=NULL)
              {
                  head=tail->next;
                  delete tail;
                  tail=head;
              }
     }

};

int main ()
{
        ifstream myfile1;
        ofstream myfile2;
        char *buff="";
        int i,length,size;
        Entry *head=NULL,*tail=NULL,*e=new Entry();

  do
  {
      cout<<"Enter ur  option"<<endl;
      cout<<"1.Load data into file"<<endl;
      cout<<"2.Save data into file"<<endl; 
      cout<<"3.Read data from file"<<endl;
      cout<<"4.Close the file"<<"\n";
      cin>>i;
      switch(i)
      {
      case 1:
            if(head==NULL)
            {
              head=tail=new Entry();
            }
            else
            {     
              if(head->next==NULL)
                 head->next=tail->next=new Entry();
              else
                 tail->next=new Entry();
              tail=tail->next;
            }
            if(head==tail)
            {
            head->next=tail->next=NULL;
            }
            cout<<"Enter name\t";
            cin>>tail->name;
            cout<<"Enter Id\t";
            cin>>tail->id;
            cin.clear();
            cout<<"Enter salary\t";
            cin>>tail->sal;
            cin.clear();
            cout<<"Enter Boolean value\t";
            cin>>tail->b;
            cin.clear();
            break;
       case 2:
            myfile2.open ("C:\\sample\\example.bin",ios::app|ios::in|ios::ate);
            tail=head;
            while(head!=NULL)
            {
              myfile2<<head->name<<"\t";
              myfile2<<head->id<<"\t";
              myfile2<<head->sal<<"\t";
                  if((head->b==false)||(head->b==true))
                  {
                      myfile2<<head->b<<"\n";
                      cin.clear();
                  }
              head=head->next;
            }
            myfile2.close();
            head=tail;
            e->clear(head,tail);
            break;
        case 3:
            myfile1.open("C:\\sample\\example.bin",ios::out|ios::ate);

            cout<<"the contents are:\n";

            myfile1.seekg(0,ios::end);
            length=myfile1.tellg();
            if(length>0)
            {
                myfile1.seekg(0,ios::beg);
                buff=new char[length];
                memset(buff,0,length);
                myfile1.read(buff,length);
                cout<<buff<<"\n";
                delete []buff;
            }
            else
            {
                cout<<"file is empty\n";
            }

            myfile1.close();
            myfile2.open("C:\\sample\\examplesize.txt",ios::trunc|ios::out|ios::ate);
            myfile2<<length;
            myfile2.close();
            break;
        case 4:
            if(head==NULL)
                e->clear(head,tail);
            delete e;
            exit(0);
        default:
            if(head==NULL)
                e->clear(head,tail);
            delete e;
            exit(0);
      }
  }while(1);
  return 0;
}

plz help me..

You put it as a function inside that Entry class.

class Entry{
    //clas stuff;
    FileSize(std::string Path)
    {
        std::ifstream Stream(Path.c_str());
        if (Stream.is_open())
        {
            int Start = Stream.tellg();
            Stream.seekg(0, std::ios::end);
            int End = Stream.tellg();
            Stream.close();
            return (End - Begin);
        }
        return -1;
    }
};

and inside the while you call the function:

do{//....
    //while stuff
    int size=FileSize(std::string Path); //your path to the file
    //do stuff with the size...
}while (1);

but i new to this environment. Let me know how we use such algorithem like MD5 etc.plz explain with simple example.

I have attached the following link to an example of creating a MD5 checksum under Windows for your review.

The link

thank u for the example...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.