LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   newbie c string question (https://www.linuxquestions.org/questions/programming-9/newbie-c-string-question-537728/)

nomb 03-15-2007 10:23 AM

newbie c string question
 
Hey guys,

I'm very stuck. I am trying to put together some strings to make a command to run with system. Here is what I have:

Declarations:

gchar *method[0]

GtkWidget *dmcal_new_txt_Path = NULL;
GtkWidget *dmcal_new_txt_Size = NULL;
gchar *file_size = NULL;
gchar *file_path = NULL;
gchar *command = NULL;
gchar *compart = NULL;


Code:

g_print("dd if=%s of=%s bs=1024 count=%s\n",method[0],file_path,file_size);
compart = "dd if=";
command = strcat (compart, method[0]);
g_print(command)

Bisically I want to build what is in the first g_print into the variable command. The first g_print prints everything out great with no problems. Its just when it gets to the strcat it errors and pops up the bug buddy.

Thanks a lot for your help.

nomb

weibullguy 03-15-2007 10:35 AM

See if this works
Code:

g_print("dd if=%s of=%s bs=1024 count=%s\n",method[0],file_path,file_size);
compart = "dd if=";
command = (char *)calloc(strlen(compart) + strlen(method[0]) + 1, sizeof(char));
command = strcat(compart,method[0]);
g_print(command);


nomb 03-15-2007 10:41 AM

Im afraid that still errored on me and popped up the bug reporting tool.

weibullguy 03-15-2007 10:45 AM

Are you running the program from the command line or using a launcher? Try running it from the command line. Try using gdb to trace the program.

nomb 03-15-2007 10:49 AM

I am running it from the command line. Im not sure how to use gdb. I have it but not sure how to use it.

nomb 03-15-2007 10:56 AM

Oh wait this is what it says on the error:

Startng program: /home/nomb/Projects/DMCaL/src/dmcal
[Thread debugging using libthread_db enabled]
[New Thread -1208641312 (LWP 5993)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1208641312 (LWP 5993)]
0x45155c95 in strcat () from /lib/libc.so.6

I'm not sure what that means tho.

weibullguy 03-15-2007 10:59 AM

Try a slightly different approach
Code:

g_print("dd if=%s of=%s bs=1024 count=%s\n",method[0],file_path,file_size);
command = (char *)calloc(strlen("dd if=") + strlen(method[0]) + 1, sizeof(char));
command = "dd if=";
strcat(command,method[0]);
g_print(command);

To run gdb, just execute
Code:

gdb ./foo-bar
. This starts gdb and loads the foo-bar program. Then run the program in gdb by executing run. Here's the on-line manual http://sourceware.org/gdb/current/on...s/gdb_toc.html for more in-depth help with gdb.

nomb 03-15-2007 11:15 AM

I got:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1208915744 (LWP 6259)]
0x45155c95 in strcat () from /lib/libc.so.6

graemef 03-16-2007 12:39 AM

The strcat is expecting a valid memory address with which to place the string data. So you have declared compart and command as pointers to a character (correct) Next you assign some memory into command with the calloc function (correct), then comes the curious line
Code:

compart = "dd if=";
compart is a pointer so this assignment will not work, compart is expecting an address to memory not actual data.

Next check out the details of strcat (man strcat) and you will see that strcat changes the details of the first argument. So build up the details of command in two steps, both using strcat the first adding the literal "dd if=" the next call appending the value of method[0].

weibullguy 03-16-2007 12:25 PM

Code:

gchar *method[0]

GtkWidget *dmcal_new_txt_Path = NULL;
GtkWidget *dmcal_new_txt_Size = NULL;
gchar *file_size = NULL;
gchar *file_path = NULL;
gchar *command;
gchar *compart="dd if=";

g_print("dd if=%s of=%s bs=1024 count=%s\n",method[0],file_path,file_size);
command = (char *)calloc(strlen("dd if=") + strlen(method[0]) + 1, sizeof(char));
strcat(command, compart);
strcat(command, method[0]);
g_print(command)



All times are GMT -5. The time now is 02:22 AM.