?
Solved

Cchar * Concating

Posted on 2015-01-19
7
Medium Priority
?
109 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

Technology Partners: 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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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