Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with a program that does Copy-Paste functionality.

Problem with a program that does Copy-Paste functionality.

Scheduled Pinned Locked Moved C / C++ / MFC
ioshelp
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ritz1234
    wrote on last edited by
    #1

    Hello friends, I've a problem with the following code that does copy an exe and create a new one with changed name.The program however copies the exe but the newly created exe can't be open.

    ifstream fin;
    char buf[1000000];
    try
    {

    	fin.open("C:\\\\Documents and Settings\\\\ritesh.mochi\\\\Desktop\\\\MFCBase64.exe",ios::in|ios::binary);
    
    	if(fin.is\_open())
    	{
    		strcpy(buf,"");
    		while(!fin.eof()) 
    		{
    		       char \*ch=new char(1);
    			fin.read(ch,1);
    			strcat(buf,ch);
    		}
    	}
    	if(1)
    	{
    		unsigned long len;
    		
    		len=strlen(buf);
    
    		ofstream fout;
    		
    		fout.open("C:\\\\Documents and Settings\\\\ritesh.mochi\\\\Desktop\\\\MFCBase64\_1.exe",ios::out|ios::binary);
    		fout.write(buf,len);		 
    		fout.close();
    	}
    }
    catch(...)
    {
    	MessageBox("Exception occured.",\_T("MFCBase64"),MB\_OK);
    }
    

    ritz1234

    C D S 3 Replies Last reply
    0
    • R ritz1234

      Hello friends, I've a problem with the following code that does copy an exe and create a new one with changed name.The program however copies the exe but the newly created exe can't be open.

      ifstream fin;
      char buf[1000000];
      try
      {

      	fin.open("C:\\\\Documents and Settings\\\\ritesh.mochi\\\\Desktop\\\\MFCBase64.exe",ios::in|ios::binary);
      
      	if(fin.is\_open())
      	{
      		strcpy(buf,"");
      		while(!fin.eof()) 
      		{
      		       char \*ch=new char(1);
      			fin.read(ch,1);
      			strcat(buf,ch);
      		}
      	}
      	if(1)
      	{
      		unsigned long len;
      		
      		len=strlen(buf);
      
      		ofstream fout;
      		
      		fout.open("C:\\\\Documents and Settings\\\\ritesh.mochi\\\\Desktop\\\\MFCBase64\_1.exe",ios::out|ios::binary);
      		fout.write(buf,len);		 
      		fout.close();
      	}
      }
      catch(...)
      {
      	MessageBox("Exception occured.",\_T("MFCBase64"),MB\_OK);
      }
      

      ritz1234

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      The bug is you used strlen to get the file size. strlen stops when it reaches the first '\0' character inside the executable file (hence you possibly get a smaller size than the actual one). BTW reading one chracter at time is poor programming in such a context. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      R 1 Reply Last reply
      0
      • C CPallini

        The bug is you used strlen to get the file size. strlen stops when it reaches the first '\0' character inside the executable file (hence you possibly get a smaller size than the actual one). BTW reading one chracter at time is poor programming in such a context. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        R Offline
        R Offline
        ritz1234
        wrote on last edited by
        #3

        Thanks for the reply CPallini, Actually when comparing the size the created new file has more size than the original file. So,now i am maintaining a counter while reading a character from the file. The code is shown below.

        unsigned long len=0;
        while(!fin.eof())
        {
        char *ch=new char(1);
        fin.read(ch,1);
        strcat(buf,ch);
        len++;
        }

        And writing to the file as below

        ofstream fout;

        		fout.open("C:\\\\Documents and Settings\\\\ritesh.mochi\\\\Desktop\\\\MFCBase641.exe",ios::out|ios::binary);
        		fout.write(buf,len-1);
        		//len=strTotalContent.GetLength();
        	 
        		fout.close();
        

        Though the size of the source and destination exe is same in this case.I can't open this destination exe.Please help me.

        ritz1234

        1 Reply Last reply
        0
        • R ritz1234

          Hello friends, I've a problem with the following code that does copy an exe and create a new one with changed name.The program however copies the exe but the newly created exe can't be open.

          ifstream fin;
          char buf[1000000];
          try
          {

          	fin.open("C:\\\\Documents and Settings\\\\ritesh.mochi\\\\Desktop\\\\MFCBase64.exe",ios::in|ios::binary);
          
          	if(fin.is\_open())
          	{
          		strcpy(buf,"");
          		while(!fin.eof()) 
          		{
          		       char \*ch=new char(1);
          			fin.read(ch,1);
          			strcat(buf,ch);
          		}
          	}
          	if(1)
          	{
          		unsigned long len;
          		
          		len=strlen(buf);
          
          		ofstream fout;
          		
          		fout.open("C:\\\\Documents and Settings\\\\ritesh.mochi\\\\Desktop\\\\MFCBase64\_1.exe",ios::out|ios::binary);
          		fout.write(buf,len);		 
          		fout.close();
          	}
          }
          catch(...)
          {
          	MessageBox("Exception occured.",\_T("MFCBase64"),MB\_OK);
          }
          

          ritz1234

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          ritz1234 wrote:

          char *ch=new char(1);

          Why are you doing this? Heap memory is not necessary for what you are doing.

          ritz1234 wrote:

          strcat(buf,ch);

          This will not work when dealing with a single '\0' character.

          "Love people and use things, not love things and use people." - Unknown

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          R 1 Reply Last reply
          0
          • D David Crow

            ritz1234 wrote:

            char *ch=new char(1);

            Why are you doing this? Heap memory is not necessary for what you are doing.

            ritz1234 wrote:

            strcat(buf,ch);

            This will not work when dealing with a single '\0' character.

            "Love people and use things, not love things and use people." - Unknown

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            R Offline
            R Offline
            ritz1234
            wrote on last edited by
            #5

            I am doing this because I want to read file byte by byte.storing this bytes in the char buffer to write these bytes into the files.

            ritz1234

            D 1 Reply Last reply
            0
            • R ritz1234

              I am doing this because I want to read file byte by byte.storing this bytes in the char buffer to write these bytes into the files.

              ritz1234

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              ritz1234 wrote:

              I am doing this because I want to read file byte by byte.

              Understandable, but extremely inefficient.

              ritz1234 wrote:

              storing this bytes in the char buffer to write these bytes into the files.

              Which does not explain why you are using heap memory. You are killing the memory manager for no good reason. Use a stack-based variable instead.

              "Love people and use things, not love things and use people." - Unknown

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              R 1 Reply Last reply
              0
              • D David Crow

                ritz1234 wrote:

                I am doing this because I want to read file byte by byte.

                Understandable, but extremely inefficient.

                ritz1234 wrote:

                storing this bytes in the char buffer to write these bytes into the files.

                Which does not explain why you are using heap memory. You are killing the memory manager for no good reason. Use a stack-based variable instead.

                "Love people and use things, not love things and use people." - Unknown

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                R Offline
                R Offline
                ritz1234
                wrote on last edited by
                #7

                Thanks for the reply David, Right now I am least concern about the efficiency of the code.This is not the final code of the application. Right now I am only concerning about the result of the program which is not correct. Please help me out.A sample code would be appreciated.

                ritz1234

                D 1 Reply Last reply
                0
                • R ritz1234

                  Thanks for the reply David, Right now I am least concern about the efficiency of the code.This is not the final code of the application. Right now I am only concerning about the result of the program which is not correct. Please help me out.A sample code would be appreciated.

                  ritz1234

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  ritz1234 wrote:

                  A sample code would be appreciated.

                  char ch;
                  int x = 0;
                  while (! fin.eof())
                  {
                  fin.read(&ch, 1);
                  buf[x++] = ch;
                  }
                  ...
                  fout.write(buf, x - 1);

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  • R ritz1234

                    Hello friends, I've a problem with the following code that does copy an exe and create a new one with changed name.The program however copies the exe but the newly created exe can't be open.

                    ifstream fin;
                    char buf[1000000];
                    try
                    {

                    	fin.open("C:\\\\Documents and Settings\\\\ritesh.mochi\\\\Desktop\\\\MFCBase64.exe",ios::in|ios::binary);
                    
                    	if(fin.is\_open())
                    	{
                    		strcpy(buf,"");
                    		while(!fin.eof()) 
                    		{
                    		       char \*ch=new char(1);
                    			fin.read(ch,1);
                    			strcat(buf,ch);
                    		}
                    	}
                    	if(1)
                    	{
                    		unsigned long len;
                    		
                    		len=strlen(buf);
                    
                    		ofstream fout;
                    		
                    		fout.open("C:\\\\Documents and Settings\\\\ritesh.mochi\\\\Desktop\\\\MFCBase64\_1.exe",ios::out|ios::binary);
                    		fout.write(buf,len);		 
                    		fout.close();
                    	}
                    }
                    catch(...)
                    {
                    	MessageBox("Exception occured.",\_T("MFCBase64"),MB\_OK);
                    }
                    

                    ritz1234

                    S Offline
                    S Offline
                    Saurabh Garg
                    wrote on last edited by
                    #9

                    There are two problems with this code. 1. strcat: you cannot do a strcat with a single character since it expects both source and destination strings to be NULL terminated. Moreover, executable file is a binary file and this is going to create problems. 2. strlen: computes the length of a NULL terminated string so if a NULL character is encountered in between the buffer it will return incorrect length in case of binary files. Try the following code. Here I have taken correct file size because you cannot have too large arrays on a stack.

                    char* buf = new char[2567672];
                    strcpy(buf, "");
                    
                    int i =0;
                    ifstream fin("C:\\1.exe", ios::binary);
                    if(fin.is_open())
                    {
                    	char ch;
                    	while(!fin.eof())
                    	{
                    		fin.read(&ch, 1);
                    		buf[i] = ch;
                    		i++;
                    	}
                    }
                    
                    ofstream fout("C:\\2.exe", ios::binary);
                    fout.write(buf, i-1);
                    fout.close();
                    

                    If you ask me then I would use following code:

                    size_t FileSize(std::ifstream* fp)
                    {
                    	std::ifstream::pos_type begin = fp->tellg();
                    	fp->seekg (0, std::ios::end);
                    	std::ifstream::pos_type end = fp->tellg();
                    	fp->seekg (0, std::ios::beg);
                    	return end - begin;
                    }
                    
                    void CopyBinaryFile()
                    {
                    	ifstream fin("C:\\1.exe", ios::binary);
                    	
                    	size_t _len = FileSize(&fin);
                    	char* _buffer = new char[_len];
                    	
                    	if(fin)
                    	{
                    		fin.read(_buffer, _len);
                    	}
                    	
                    	ofstream fout("C:\\2.exe", ios::binary);
                    	if(fout)
                    	{
                    		fout.write(_buffer, _len);
                    		fout.close();
                    	}
                    }
                    

                    Hope this solves your problem. -Saurabh

                    R 1 Reply Last reply
                    0
                    • S Saurabh Garg

                      There are two problems with this code. 1. strcat: you cannot do a strcat with a single character since it expects both source and destination strings to be NULL terminated. Moreover, executable file is a binary file and this is going to create problems. 2. strlen: computes the length of a NULL terminated string so if a NULL character is encountered in between the buffer it will return incorrect length in case of binary files. Try the following code. Here I have taken correct file size because you cannot have too large arrays on a stack.

                      char* buf = new char[2567672];
                      strcpy(buf, "");
                      
                      int i =0;
                      ifstream fin("C:\\1.exe", ios::binary);
                      if(fin.is_open())
                      {
                      	char ch;
                      	while(!fin.eof())
                      	{
                      		fin.read(&ch, 1);
                      		buf[i] = ch;
                      		i++;
                      	}
                      }
                      
                      ofstream fout("C:\\2.exe", ios::binary);
                      fout.write(buf, i-1);
                      fout.close();
                      

                      If you ask me then I would use following code:

                      size_t FileSize(std::ifstream* fp)
                      {
                      	std::ifstream::pos_type begin = fp->tellg();
                      	fp->seekg (0, std::ios::end);
                      	std::ifstream::pos_type end = fp->tellg();
                      	fp->seekg (0, std::ios::beg);
                      	return end - begin;
                      }
                      
                      void CopyBinaryFile()
                      {
                      	ifstream fin("C:\\1.exe", ios::binary);
                      	
                      	size_t _len = FileSize(&fin);
                      	char* _buffer = new char[_len];
                      	
                      	if(fin)
                      	{
                      		fin.read(_buffer, _len);
                      	}
                      	
                      	ofstream fout("C:\\2.exe", ios::binary);
                      	if(fout)
                      	{
                      		fout.write(_buffer, _len);
                      		fout.close();
                      	}
                      }
                      

                      Hope this solves your problem. -Saurabh

                      R Offline
                      R Offline
                      ritz1234
                      wrote on last edited by
                      #10

                      Thanks for the help Subhas and David, The problem is now resolved.

                      ritz1234

                      S 1 Reply Last reply
                      0
                      • R ritz1234

                        Thanks for the help Subhas and David, The problem is now resolved.

                        ritz1234

                        S Offline
                        S Offline
                        Saurabh Garg
                        wrote on last edited by
                        #11

                        You are welome. -Saurabh

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups