Solved

Cchar * Concating

Posted on 2015-01-19
7
90 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
ID: 40559152
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
ID: 40559195
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
ID: 40559206
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Expert Comment

by:pepr
ID: 40559609
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
ID: 40561635
Thank You Sir!!!!!!!!
0
 
LVL 28

Expert Comment

by:pepr
ID: 40561744
You are welcome. ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

937 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

7 Experts available now in Live!

Get 1:1 Help Now