LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2007, 10:23 AM   #1
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 675

Rep: Reputation: 58
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
 
Old 03-15-2007, 10:35 AM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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);
 
Old 03-15-2007, 10:41 AM   #3
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 675

Original Poster
Rep: Reputation: 58
Im afraid that still errored on me and popped up the bug reporting tool.
 
Old 03-15-2007, 10:45 AM   #4
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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.
 
Old 03-15-2007, 10:49 AM   #5
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 675

Original Poster
Rep: Reputation: 58
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.
 
Old 03-15-2007, 10:56 AM   #6
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 675

Original Poster
Rep: Reputation: 58
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.
 
Old 03-15-2007, 10:59 AM   #7
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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.
 
Old 03-15-2007, 11:15 AM   #8
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 675

Original Poster
Rep: Reputation: 58
I got:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1208915744 (LWP 6259)]
0x45155c95 in strcat () from /lib/libc.so.6
 
Old 03-16-2007, 12:39 AM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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].
 
Old 03-16-2007, 12:25 PM   #10
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C string scanf question????????? skie_knite007 Programming 6 12-12-2005 03:07 PM
C string and printf question exvor Programming 7 12-10-2005 10:53 AM
string question AquamaN Programming 2 10-20-2005 06:17 PM
script string question MadCactus Programming 4 08-14-2004 04:44 PM
simple C string question mvendramini Programming 9 09-30-2003 07:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration