Hi, I'm making a small program that sends data to a program that is constantly moving around. (to practice my c++)and it needs to be able to read file paths (C:\\Dev-Cpp\\...etc) from a file and store it in a char array (this is all I could figure out worked for storing file paths without an error) So far I have all the code down except the part where I send it to the function request(). I store the path name in a char array, and it generates an error "Invalid conversion from char* to char"

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

int request(char);
int main()//Run.test() is the class to test the file.
{
    int check;//this used to store 1 if request worked, 0 if it didn't
    char FilePath[] = "C:\\Dev-Cpp\\keylogger.exe";//this is the file path the data is sent to.
    check=(request(FilePath));
    if (check==1)
    {
                 cout << "It worked, check for yourself!";
}
return 0;
}

int request()
{
    int temporary=0;
    ofstream Asker("Liason.def",ios::trunc);
    if(Asker.is_open())
    {
                     Asker << FilePath;
                     MoveFile("Liason.def",FilePath);
                     temporary=1;
                     }
    else
    {   temporary=0;   }
    return temporary;
}

I'm sure this is a very simple problem, but I really can't figure it out (i'm still a c++ noob)
Anyone know what my problem is?

Two things:

1) char is not the same as char*. The first is a single character. The second is an address to one or more characters (like an array). Your function should take the second as argument.

2) The function prototype lists an argument, but the function itself never names it. Hence it cannot be used (and I would imagine that your compiler would complain about the mismatched prototypes anyway.)

int request( const char* );

int main()
  {
  ...
  }

int request( const char* filename )
  {
  ofstream Asker( filename, ios::trunc );
  ...
  }

Hope this helps. :)

char FilePath[] = "C:\\Dev-Cpp\\keylogger.exe";

I think your problem is here, where you use \\ instead of // for your file path checking.

Try this--

char FilePath[] = "C:/Dev-Cpp/keylogger.exe";

Although I'm not too sure why you have two slashes instead of one.

also when creating an array like that, you may want to initialize it in this manner--

char FilePath[] = {'C',':','/','D','e','v','-','C','p','p','/','k','e','y','l','o','g','g','e','r','.','e','x','e','\0'};

I think I did that right, hopefully I didnt miss a char-symbol...

either that or just do it the symple way...

char *FilePath = "C:/Dev-Cpp/keylogger.exe"

whew..

char FilePath[] = "C:\\Dev-Cpp\\keylogger.exe";

I think your problem is here, where you use \\ instead of // for your file path checking.

Nope. That's perfectly fine. The first backslash is used as an escape-character to indicate that the second backslash isn't an escape-character :)
Link

In C++

char FilePath[]="...

is equivalent to :-

char *FilePath="..

so when you are passing the variable "FilePath" to a function, the function needs to accept a "char* "(or char variable_name[]) as an argument and not just "char".

In C++

char FilePath[]="...

is equivalent to :-

char *FilePath="..

Those declarations are only equivalent as function parameters. The buggiest difference is that initializing the pointer points it to a read-only string literal while the array gets a writable copy of it.

Those declarations are only equivalent as function parameters. The buggiest difference is that initializing the pointer points it to a read-only string literal while the array gets a writable copy of it.

Cool tried it out !!! Never knew about this !!!

Thank you guys so much, this was a big help, i just compiled it without an error!

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.