Hi mates :)

recently I did this code:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main()
{
	int check = 1;
	string dir;
	string dir2;
	string name;

	cout << "Which directory is the file located?\n";
	getline(cin, dir);

	cout << "\nWhere you wanna move it to which directory?\n";
	getline(cin, dir2);

	while( check == 1 )
	{
		cout << "\nEnter the name of the file: ";
		getline(cin, name);

		CopyFile(dir + name , dir2 + name, false);

		cout << "Do you want to copy a file again? 1 if yes, otherwise, no: ";
		cin >> check;

	}

	return 0;

}

How to fix the problem, please?

Thanks :)

Recommended Answers

All 17 Replies

>>How to fix the problem, please?
what problem?? CopyFile() parameters are const char*, not std::string. Call string's c_str() method to make the conversion.

Umm so any solution? I want to specify the directory and name of file using the code.

Any ideas?

Umm so any solution? I want to specify the directory and name of file using the code.

Any ideas?

I already told you the solution. Pay attention.

you may also have to add a '\' in between the dir and the file name unless the person enters the dir with a slash at the end

Thanks guys.. I tried but it shows problems :(

this is my new code, kindly help

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main()
{
	int check = 1;
	string dir;
	string dir2;
	string name;
	char *cstr, *cstr2;

	cout << "Which directory is the file located?\n";
	getline(cin, dir);

	cout << "\nWhere you wanna move it to which directory?\n";
	getline(cin, dir2);

	while( check == 1 )
	{
		cout << "\nEnter the name of the file: ";
		getline(cin, name);

		dir = dir + name;
		dir2 = dir2 + name;

		cstr = new char [dir.size()+1];
		cstr2 = new char [dir2.size()+1];

		CopyFile(cstr, cstr2, false);

		cout << "Do you want to copy a file again? 1 if yes, otherwise, no: ";
		cin >> check;

	}

	return 0;

}

Thanks

do you know about the c_str() member function of string? if not it returns a const char * that contains the string. this can be used to pass into functions that only take a char * as a parameter. if you put this together with what Ancient Dragon said you should be able to figure it out.

std::string has a very useful member function called .c_str ().
It returns the NULL terminated string equivalent of the STL string.

std::String strMyString ("BLAHBLAHBLAH");
  char *myChar = const_cast <char*> strMyString.c_str ();

P.S.
Please notice that c_str () returns a const char* not a char*!

char *myChar = const_cast <char*> strMyString.c_str ();

In general this is a bad idea, c_str returns const because you should not be changing the data pointed to by its return value. Randomly casting away the constness for no good reason is asking for trouble and undefined behaviour.

cstr = new char [dir.size()+1];
		cstr2 = new char [dir2.size()+1];

		CopyFile(cstr, cstr2, false);

This just creates 2 buffers of the right size without putting anything in them and then passes the unitialised buffers to CopyFile.

You need to see everyone elses comments of string::c_str you may just want to consider looking the method up in your reference material.

commented: Solid advice :) +8

I'm gonna try it out and get back to u, thanks for the explanation.

Still i'm getting errors after editing the code and I did what u said.

here is my code:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main()
{
	int check = 1;
	string dir;
	string dir2;
	string name;

	cout << "Which directory is the file located?\n";
	getline(cin, dir);

	cout << "\nWhere you wanna move it to which directory?\n";
	getline(cin, dir2);

	while( check == 1 )
	{
		cout << "\nEnter the name of the file: ";
		getline(cin, name);

		dir = dir + name;
		dir2 = dir2 + name;
		
		char *myChar1 = const_cast <char*> dir.c_str ();
		char *myChar2 = const_cast <char*> dir2.c_str ();

		CopyFile(*myChar1, *myChar2, false);

		cout << "Do you want to copy a file again? 1 if yes, otherwise, no: ";
		cin >> check;

	}

	return 0;

}

lines 24 and 25: you may have to put '\' between them if the first string doesn't already end in '\'

line 30. This two parameters are wrong. First you don't even need those two pointers. CopyFile(dir1.c_str(), dir2.c_str()); No are not paying attention to what other people are telling you or not comprehending the importance of their comments.

that is not true my friend, if what u r saying is true, then u shouldn't see me trying and writing code!!!!

Anyway, thanks for the help. If you want to be so mean or u r feeling bad because u r helping or giving advices then I'd rather be "untaught" instead of taking advices or help from someone who just act like giving some!
Sorry for being rude, but this how your comments/or replies sound to me.


I tried what u said and I ended up that my file isn't copied and i have infinite loop :D

my code:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main()
{
	int check = 1;
	string dir;
	string dir2;
	string name;

	cout << "Which directory is the file located?\n";
	getline(cin, dir);

	cout << "\nWhere you wanna move it to which directory?\n";
	getline(cin, dir2);

	while( check == 1 )
	{
		cout << "\nEnter the name of the file: ";
		getline(cin, name);

		dir = dir + name;
		dir2 = dir2 + name;

		CopyFile(dir.c_str(), dir2.c_str(), false);

		cout << "Do you want to copy a file again? 1 if yes, otherwise, no: ";
		cin >> check;

	}

	return 0;

}

Kindly, if anyone can help would be appreciated.

put print statements on line 26 to show the value of dir1 and dir2. Most likely one or both are not formed correctly.

>>and i have infinite loop
Because the cin >> check; leaves the '\n' in the keyboard buffer which you need to remove

cin >> check;
cin.ignore();
// the value of check wlll be '1', not 1.

The above may or may not flush all the keyboard input buffer, expecially if you type a lot of other characters besides the integer.

Hi my friend thanks for explaining things, much appreciated :)

I did what you said, when I put "cout" in line 26 for dir, I got
E:\
Which I wrote,

But when I put "cout" after both dir and dir2, for dir
it gave me
E:\D:\

and still I'm getting "infinite loop" after using your solution.!!

You probably didn't put a space or '\n' after displaying dir1 so they were shown together

cout << dir1 << '\n' << dir2 << '\n';

and still I'm getting "infinite loop" after using your solution.!!

Post your revised program that contains the change I suggested

Still, I made the suggested solutions but not benefits!

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main()
{
	int check = 1;
	string dir;
	string dir2;
	string name;

	cout << "Which directory is the file located?\n";
	getline(cin, dir);

	cout << "\nWhere you wanna move it to which directory?\n";
	getline(cin, dir2);

	/*while( check == 1 )
	{*/
		cout << "\nEnter the name of the file: ";
		getline(cin, name);

		dir = dir + name;
		dir2 = dir2 + name;

		cout << dir;
		cout << endl;

		/*CopyFile(dir.c_str(), dir2.c_str(), false);

		cout << "Do you want to copy a file again? 1 if yes, otherwise, no: ";
		cin >> check;
		cin.ignore();

	}*/

	return 0;

}

I repeat:

>>How to fix the problem, please?
what problem??

You must tell us the problem. Do NOT just post code and ask us for a solution .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.