1

I'm trying to make a char array in "C" that would have an integer value at the end of the array. but I'm trying to change the integer value while going through a loop.

system("mv " + file_list[loca] + " /media/MyBook1TB/" + folderint);

file_list[n][200] // is a char array of file locations
folderint //  folder increment variable example "/media/MyBook1TB/0" then "/media/MyBook1TB/1"

I'm not sure if c.str() would work for what I put in system() up above.

I couldn't find anything on the cplusplus website. any help?

I'm trying to not use the string library..

5
  • why are you trying not to use the string library? and are you using c or are you using c++ Commented Aug 16, 2013 at 3:21
  • fixed the tag for you then Commented Aug 16, 2013 at 3:26
  • If it's just a single character [0-9] then just put the ascii value [48-57 decimal] in the proper location. Is that what you mean? Commented Aug 16, 2013 at 3:26
  • This is really not a good way to do things. What if file_list[loca] is . .; rm -rf /* ; echo? Commented Aug 16, 2013 at 3:36
  • @DavidSchwartz the array contents come from a text file. so im not too worried about that. plus this is a very simple program. just some parts isnt for me right now, lol. Commented Aug 16, 2013 at 4:02

2 Answers 2

3

In C you can't just add together strings to concatenate them. You need to do something like:

char command[MAX_LENGTH];
sprintf(command, "mv %s /media/MyBook1TB/%d", file_list[loca], folderint);
system(command);

You don't specify what file_list is so you'll need to define your own MAX_SIZE. Also note that %s assumes file_list is a string and %d assumes that folderInt is an int.

Side note, the nature of the command you've posted seems like it would be better suited for a shell script, but if you must use C, look up the exec functions. system is a dangerous (and inefficient) function to call (especially in this case if you're not sanitizing your inputs).

Sign up to request clarification or add additional context in comments.

2 Comments

I dont know how to make a shell script. i have only learned C/C++, Python, VB, and Java in college. i have never seen anyone make a script.
@Alex Shell scripts are very easy (especially compared to C!). They are essentially just a file with a list of shell commands to run. You can still create variables and use loops and conditional statements too. Look up bash scripting on Google.
2
char system_string[LARGE_COMFORTABLE_NUM];

sprintf(system_string,"mv %s /media/MyBook1TB/%d",file_list[loca],folderint);

system(system_string);

1 Comment

I wasn't sure what the file_list[loca] was, but shanet and I are in the same ballpark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.