Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cchar * Concating

Posted on 2015-01-19
7
Medium Priority
?
113 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

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!

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

618 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