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 10-03-2008, 04:37 PM   #1
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Rep: Reputation: 30
I think that is problem with convertin char to const char*


How can I to convert char* to const char*. I tried with cast but system output error.

My code is:

Quote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/inotify.h>

#define _GNU_SOURCE

void daemonize() {

pid_t pid;

pid = fork();
if (pid < 0) perror("Error in fork()");
if (pid > 0) exit(0);
setsid();

}

int main(int argc, char* argv[]) {

int fd, wd;
size_t lsize = 0;
FILE* conf_file;
char* line = NULL;
char* token;

if (argc == 1) {
printf("\nCommand syntax:\n");
printf("\n\tfsmond [ start | stop ]\n");
printf("\t\tstart: start fsmon daemon\n");
printf("\t\tstop: stop fsmon daemon\n");
}
else if (strcmp(argv[1], "start") == 0) {
daemonize();
fd = inotify_init();
if (fd < 0) perror("Error in inotify_init()");
conf_file = fopen("/etc/fsmon/fsmon.conf", "r");
fseek(conf_file, 0, SEEK_SET);
while (!feof(conf_file))
while (getline(&line, &lsize, conf_file) != -1)
if (strncmp(&line[0], "#", 1) != 0) {
token = strtok(line, "=");
if (strcmp(token, " ADD") == 0) {
token = strtok(NULL, "=");
printf("fd = %d, dir = %s", fd, token);
wd = inotify_add_watch(fd, (const char*)token, IN_CREATE | IN_MODIFY | IN_DELETE);
if (wd < 0) printf("wd = %d Error = %s\n", wd, strerror(errno));

}
}
}

return(0);

}
The output of program is:

Quote:
bash-3.1# gcc fsmond.c -o fsmond
bash-3.1# ./fsmond start
fd = 3, dir = "/tmp"
wd = -1 Error = No such file or directory
fd = 3, dir = "/var/www"
wd = -1 Error = No such file or directory
bash-3.1# gcc fsmond.c -ansi -Wall -pedantic -D_GNU_SOURCE
fsmond.c:8:1: warning: "_GNU_SOURCE" redefined
<command-line>: warning: this is the location of the previous definition
bash-3.1# gcc fsmond.c -o fsmond
bash-3.1# ./fsmond start
fd = 3, dir = "/tmp"
wd = -1 Error = No such file or directory
fd = 3, dir = "/var/www"
wd = -1 Error = No such file or directory
bash-3.1# gcc fsmond.c -ansi -Wall -pedantic -D_GNU_SOURCE
fsmond.c:8:1: warning: "_GNU_SOURCE" redefined
<command-line>: warning: this is the location of the previous definition
bash-3.1#
The inotify_add_watch prototipe is

int inotify_add_watch(int fd, const char *pathname, uint32_t mask);

If I write in inotify_add_watch function directly any directory string:

wd = inotify_add_watch(fd, "/tmp", IN_CREATE | IN_MODIFY | IN_DELETE);

then program run without problems.

I want program in C, then I cannot use c_str() C++ function.
How can I solve this problem?

Thanks,
Savio

Last edited by shifter; 10-03-2008 at 04:42 PM. Reason: correction
 
Old 10-03-2008, 05:37 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe you have to delete the new line character "\n" from dir, just a thought
 
Old 10-03-2008, 05:42 PM   #3
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
How can I split and truncate dir from "\n"?
 
Old 10-03-2008, 05:49 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I don't know, you could replace the newline char with a null:

dir[strlen(dir) -1] = '\0';
 
Old 10-03-2008, 05:53 PM   #5
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by shifter View Post
How can I split and truncate dir from "\n"?
use strrchr or strchr and insert zero at the position of '\n'
 
Old 10-03-2008, 06:03 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I think keefaz is correct you do not add a new line to the printf statements yet there is one there in the output. The cast from char* to char const* is implicit, the warning about _GNU_SOURCE is due to adding it on the command line and also in the code, one of these should be removed. This is a much better request for help

edit: Oops I never refreshed the screen before posting

Last edited by dmail; 10-03-2008 at 06:04 PM.
 
Old 10-03-2008, 06:06 PM   #7
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
I typed:

Quote:
dir = malloc(strlen(token)-1);
strncpy(dir, token, strlen(token) - 1);
printf("fd = %d, dir = %s", fd, token);
wd = inotify_add_watch(fd, dir, IN_CREATE | IN_MODIFY | IN_DELETE);
if (wd < 0) printf("wd = %d Error = %s\n", wd, strerror(errno));
It seems works!
 
Old 10-03-2008, 08:13 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Note however, that with you code, you use non-terminated with '\0' strings, that means weird bugs could happen...
strlen returns the number of chars not including the terminating \0, you have to malloc the number of chars you want + the terminating \0

and man strncpy:
Quote:
char *strncpy(char *dest, const char *src, size_t n);

If there is no null byte among the first n bytes of src, the string placed in dest will not be null terminated.
try:
dir = malloc(strlen(token));
strncpy(dir, token, strlen(token) - 1);
 
Old 10-04-2008, 06:00 AM   #9
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Quote:
try:
dir = malloc(strlen(token));
strncpy(dir, token, strlen(token) - 1);
Ok, thanks, it works
 
  


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
conversion from ‘const char*’ to ‘unsigned char*’ rubadub Programming 2 02-08-2008 05:45 PM
about C++ invalid conversion from 'const char*' to 'char' teoporta Programming 3 07-17-2007 09:24 AM
invalid conversion from `const char*' to `char*' deepinlife Programming 22 08-05-2006 10:49 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM

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

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

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