0

I am struggeling now for days, because I'm very new to c++.

I want to write something into a .ini file with WritePrivateProfileString

int i = 0;
char arr[total];
stringstream ssin(configs);
while (ssin.good() && i < total){
    ssin >> arr[i];
    ++i;
};

[...]

WritePrivateProfileString(TEXT("Config"),TEXT("active"),arr[i],path);

(total is a int that equals 6 and configs is a string like "text1 text2 text3")

And now I want to seperate every text (text1, text2, text3) in to a array so I can u easily use them.

But because WritePrivateProfileString uses LPCSTR I cant insert this char array as input.

[Error] invalid conversion from 'char' to 'LPCSTR {aka const char*}' [-fpermissive]

Is there a way to use the char array in the WritePrivateProfileString function or how can I convert it so it can be used in this function?

Thank you in advance.

H.P.
  • 1,181
  • 1
  • 13
  • 26
Shirotaku
  • 3
  • 2
  • LPCSTR is a `const char *`. The error message explains this already. – Panagiotis Kanavos May 31 '17 at 15:10
  • I know. But is there a way to use this array anyway or "convert" this array? - Or how can I use this string (the contet of the array) in the WritePrivateProfielString funtion? – Shirotaku May 31 '17 at 15:12
  • @Shirotaku the parameter should *likely* be simply `arr`, but you had better terminate that string first. – WhozCraig May 31 '17 at 15:13
  • Wow - the error says that you tried to pass a single `char`, not a char array. `arr[i]` is a *single character* – Panagiotis Kanavos May 31 '17 at 15:13
  • @Shirotaku What do you mean _convert_? You can just use `arr` as Parameter already. – πάντα ῥεῖ May 31 '17 at 15:13
  • A char-array decays to a char*, which is directly convertible to a const char* – The Techel May 31 '17 at 15:13
  • The error message doesn't look right, since that parameter is an `LPCTSTR` (note the `T` in the middle) (though it appears that you're building in ANSI mode so it still boils down to `const char *`). It makes me wonder if, in addition to the other problems, you're using some non-standard copies of the Windows headers. – Adrian McCarthy May 31 '17 at 22:21
  • @Adrian, since `LPCTSTR` is a preprocessor macro rather than a typedef, wouldn't the compiler have already converted it to `LPCSTR` (or `LPCWSTR` where appropriate) by the time it noticed the conversion error? – Harry Johnston May 31 '17 at 22:23
  • @Harry Johnston: Perhaps that depends on your compiler. VC++ shows `LPCTSTR` in the error. The message the OP quoted clearly isn't from VC++, but they didn't specify whether it was gcc, or clang, or something else. – Adrian McCarthy May 31 '17 at 22:29
  • I'm not sure it's a dupe. The question _might_ be about splitting the string, but it's not clear. It's certainly not _solely_ about splitting the string. I would have gone with "Unclear what you're asking," but I was holding off hoping the question would be clarified. – Adrian McCarthy May 31 '17 at 22:34
  • @Adrian, turns out my premise was wrong; `LPCTSTR` is a typedef, at least in the latest SDKs. D'oh! (But I believe MinGW and other GCC-based compilers use their own headers, not the Microsoft ones.) The choice of dupe was based on the OPs comment in response to Panagiotis' answer. It seems pretty clear that the OP intended `arr[i]` to contain a string. – Harry Johnston May 31 '17 at 22:37

1 Answers1

1

You are passing arr[i] which is a char, not an array, as the third argument. You probably wanted to pass arr.

The error says:

[Error] invalid conversion from 'char' to 'LPCSTR {aka const char*}' [-fpermissive]

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • But arr does contains the whole content of "configs" (text1, text2, text3, tex4, text4) which I wantet to seperate (or am I wrong), so that I one out of it the like text3. – Shirotaku May 31 '17 at 15:19
  • That's not what you asked in the question. Nor does the code show any splittin. `arr` is a single string, not an array of strings. Check [Split a string in C++](https://stackoverflow.com/questions/236129/split-a-string-in-c) – Panagiotis Kanavos May 31 '17 at 15:40