-1

this is a poorly worded title question and I apologize but I do not know the proper terminology.

I have a variable output formed by.. (it ends up being this way but these 4 lines aren't in order):

char cmd[50]
cmd = "test.txt"
char *output;
*output = cmd;

then I try to call open like:

int outfile;
outfile = open( output, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP );

but that does not work. However this works:

int outfile;
outfile = open( "test.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP );

I am assuming what is being stored in output is just 't' but I need all of it to make this work. Any ideas? The context of this is I am manually trying to do a redirect.

user3037172
  • 577
  • 1
  • 7
  • 25
  • "but that does not work": in what way? What indication(s) do you have that it does not work? – Scott Hunter Mar 11 '15 at 18:45
  • possible duplicate of [Assigning strings to arrays of characters](http://stackoverflow.com/questions/579734/assigning-strings-to-arrays-of-characters) – Peter M Mar 11 '15 at 18:46
  • not a duplicate i already assign output as a char * – user3037172 Mar 11 '15 at 18:49
  • @user3037172 You are trying to initialize the array `cmd` after it has been defined. I expect that there are a bunch of compiler warnings/errors that you are not seeing or ignoring. – Peter M Mar 11 '15 at 18:51
  • @PeterM, so you are saying the line cmd = "test.txt" is the issue? It cmd array of {t, e, s, t, . , t, x, t, '\0'} ? – user3037172 Mar 11 '15 at 18:53
  • @user3037172 Your attempted initialization is wrong, and is the basis of the question I referenced. But also your assignment of `output` is also wrong. – Peter M Mar 11 '15 at 18:56
  • @user3037172 In two separate comments below you have stated that both `test.txt` and `output` as shown above are **not** how they are actually defined in your code. I understand that you are trying to simplify things, but I believe you have gone too far. I also think that you are in the realms of an XY problem (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and that because of your simplifications we are not seeing your actual problem - and hence will have difficulty in actually answering your question. – Peter M Mar 11 '15 at 19:13
  • Think your just trying to be "that guy", but in source code of over 400 lines thats what I have to do. CMD is defined as char cmd [50] and does not get initialized like cmd = "test.txt". Rather, it is created character by character through some function that works and has no purpose in this question. So by the end of all that jazz it ends up with cmd = "test.txt" (IN THIS EXAMPLE). Output is created as char *output and is passed into the function with &output and then the line *output = cmd is used before the function terminates. That is what happens. – user3037172 Mar 11 '15 at 19:24
  • @user3037172 All that is relevant information that is missing from your question. As such people have been forced to make assumptions from the code you presented as to what you are trying to do. And you have now shown those assumptions to be wrong. – Peter M Mar 11 '15 at 19:49

2 Answers2

3

I think that the issue is that output is a pointer to char and *output is a char and you're assigning a char[] to that. Have you tried this?

char cmd[50] = "test.txt";
output = cmd;

EDIT: I didn't see that the initialization was wrong as well at first. Updated code.

Matt Ko
  • 969
  • 7
  • 14
0

I doubt you can assign a string constant like that to a static string, like you're trying to do here in

cmd = "test.txt";

Instead, try to assign upon initialization:

char cmd[50] = "test.txt";

or use strcpy(cmd, "test.txt"); with include <cstring>

  • Also, you don't want to use the `*` when assigning to `output`, just use `output = cmd` – Gabriel Vasconcelos Mar 11 '15 at 19:00
  • The problem is I do not know "test.txt" in my function. That test.txt is built one character a time, cmd is defined as just char cmd [50]; and then built one character a time to in this case "test.txt". I cannot use the cstring library – user3037172 Mar 11 '15 at 19:03
  • How is the string input? The user types one char at a time? In that case you can just loop through your string. `for(int i = 0; i < 50; ++i) cmd[i] = inputChar;` – Gabriel Vasconcelos Mar 11 '15 at 19:24