Solved

Cchar * Concating

Posted on 2015-01-19
7
88 Views
Last Modified: 2015-01-21
Hello,
 I am wrking on a project in wch I hve to copy some files from one place to another.
 This is wat I do.


DIR *dir;
      struct dirent *ent;
      if ((dir = opendir(path1)) != NULL)
      {
            while ((ent = readdir(dir)) != NULL)
            {
                  a1 = path1;
                  a2 = path2;
                  name = ent->d_name;
                  strcat(a1, name);
                  strcat(a2, name);
                  CopyFileA(a1, a2, 0);
}
}

No error, but it exception is thrown like

An unhandled exception of type 'System.AccessViolationException' occurred in .........

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


Plz help
Thanks in advance
0
Comment
Question by:Mohan Mani
  • 4
  • 3
7 Comments
 
LVL 28

Expert Comment

by:pepr
Comment Utility
In C++, you often allocate memory somehow, and do the things explicitly. It is unlikely you will get the answer if you do not show what is the code around your snippet.
0
 

Author Comment

by:Mohan Mani
Comment Utility
This is the whole code

int Config::fileTranfer(char * source, char * dest)
{
      char * path1;
      char * path2;
      char * a1;
      char * a2;
      char * name;
      path1 = source;
      path2 = dest;
      DIR *dir;
      struct dirent *ent;
      if ((dir = opendir(path1)) != NULL)
      {
            while ((ent = readdir(dir)) != NULL)
            {
                  a1 = path1;
                  a2 = path2;
                  name = ent->d_name;
                  strcat(a1, name);
                  strcat(a2, name);
                  CopyFileA(a1, a2, 0);
                  a1 = "";
                  a2 = "";
            }
            

      }
      return 0;
}
0
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
Comment Utility
The source is the address of a C-string allocated somehow, terminated by null. You are assigning that address to the path1 first and to the a1 later. Anyway, the size of the memory block remains as it was allocated outside. Then you try to write some other characters to the end of that memory. It is likely there is not space for doing that -- hence the exception. The function/method relies on something that may not be true.

When using C++, I suggest to learn strings and streams instead of using the C-libraires. Have a look at "Thinking in C++" by Bruce Eckel (http://mindview.net/Books/TICPP/ThinkingInCPP2e.html -- there is also the free version to be downloaded). The Volume 2 contains the chapter related to strings and streams.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Mohan Mani
Comment Utility
Thank Yu sir.
Can u tell me how to change
char * a;
to String.
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
I do not have a working environment here. Also I did not work in Linux for a while -- just a principle:

#include <string>

using namespace std;

int Config::fileTranfer(const std::string & source, const std::string &  dest)
{
      DIR *dir;
      struct dirent *ent;
      if ((dir = opendir(source.c_str())) != nullptr)
      {
            while ((ent = readdir(dir)) != nullptr)
            {
                  string name = ent->d_name;
                  string a1 = source + "/" + name;
                  string a2 = dest  + "/" + name;
                  CopyFileA(a1.c_str(), a2.c_str(), 0);
            }
      }
      return 0;
} 

Open in new window


The const std::string & source as the argument (I mean the type) ensures, that you can pass also the C-string pointer or C-string literal when calling the function. The std::string constructor will convert it to the string object named source (inside the function) -- that allows you to use std::string methods which may be much easier than fiddling with C-string functions.

The .c_str() returns the const char * address to the C-string from the std::string. The code probably needs some more work -- this is just to illustrate the idea.

For serious work, the future C++ standard will almost certainly accept the Boost Library filesystem module approach to working with directories, and files. However, for simple things, installation/compilation of the Boost Library may be too much work for you. Just keep it in your head for future ;)
0
 

Author Comment

by:Mohan Mani
Comment Utility
Thank You Sir!!!!!!!!
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
You are welcome. ;)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now