Solved

Cchar * Concating

Posted on 2015-01-19
7
103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 29

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 29

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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 29

Expert Comment

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

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 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