简体   繁体   中英

C - full char 'string' isn't being read by open function for redirect

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.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM