Sponsored Content
Top Forums Programming Concatenating Struct Value With Char In C Post 302974356 by Azrael on Sunday 29th of May 2016 02:09:46 AM
Old 05-29-2016
Concatenating Struct Value With Char In C

After googling everything I could, the best I could do with this is get the following warning with -Wall:

Code:
gcc -Wall help.c -o help
help.c: In function ‘findpid':
help.c:29:25: warning: passing argument 2 of ‘strncat' from incompatible pointer type
           strncat( pro, &str, 60);
                         ^
In file included from help.c:3:0:
/usr/include/string.h:140:14: note: expected ‘const char * __restrict__' but argument is of type ‘char (*)[60]'
 extern char *strncat (char *__restrict __dest, const char *__restrict __src,
              ^

Here's what I have for my code:

Code:
#include <stdio.h>
#include <dirent.h>
#include <string.h>

void findpid(char *filename){

    char pro[60] = "/proc/";
    char fd[5] = "/fd/";
    char pid[7];
    char str[60];
    char dir[60];
    FILE *file;
    file = fopen(filename,"r");
    while(fgets(pid, sizeof(pid), file)!=NULL)
    strtok(pid, "\n"); // Strip newline.
    strncat( pro, pid, 10 );
    strncat( pro, fd, 10 );
    strncat( dir, pro, 10);
    fclose(file);

    DIR *dp;
    struct dirent *ep;

    dp = opendir (pro);
    if (dp != NULL)
      {
        while ((ep = readdir (dp)) != NULL)
          memcpy(str, ep->d_name, 60);
          strncat( pro, &str, 60);
          printf("%s\n", pro);
        (void) closedir (dp);
      }
    else
      perror ("Couldn't open the directory");

}

int main(void)
{
    if (fopen("/var/run/apache2/apache2.pid", "r") != NULL) {
        char filename[] = "/var/run/apache2/apache2.pid";
        findpid(filename);
    }

    else if (fopen("/var/run/httpd/httpd.pid", "r") != NULL) {
        char filename[] = "/var/run/httpd/httpd.pid";
        findpid(filename);
    }
    else if (fopen("/var/run/nginx.pid", "r") != NULL) {
        char filename[] = "/var/run/nginx.pid";
        findpid(filename);
    }
    else {
        printf("Nothing found.\n");
    }

    return (0);
}

If Apache or Nginx are running it should grab the pid from /var/run and list its directory in /proc/$pid/fd. Right now, replacing the code in the while loop with "printf("%s%s\n", pro, ep->d_name);" works just fine. However, my current code dies after listing one directory at that location.

I tried casting, dereferncing pointers and a lot of other things I could think of, but kept getting segmentation faults. Any suggestions greatly appreciated.
 

10 More Discussions You Might Find Interesting

1. Programming

save a struct

hi all , can i save a structure in c in a file? how ? help me , thx. :) (2 Replies)
Discussion started by: kall_ANSI
2 Replies

2. Programming

struct tm problem

I receive an integer as argument for a function. within function definition i want it to be of type struct tm. eg.. main() { int a; ...... } function(...,..,a,..) int a; { struct tm tm; if(!a) ^ time(&a); ^ ... (4 Replies)
Discussion started by: bankpro
4 Replies

3. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

4. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

5. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

6. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

7. Programming

help with struct command in C

in C i am using this code to get the c time or a time or m time struct dirent *dir; struct stat my; stat(what, &my); thetime = my.st_ctime; How can i check if i have permission to check the c time of the file? (1 Reply)
Discussion started by: omega666
1 Replies

8. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

9. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

10. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies
Graphics::Primitive::Insets(3pm)			User Contributed Perl Documentation			  Graphics::Primitive::Insets(3pm)

NAME
Graphics::Primitive::Insets - Space between things DESCRIPTION
Graphics::Primitive::Insets represents the amount of space that surrounds something. This object can be used to represent either padding or margins (in the CSS sense, one being inside the bounding box, the other being outside) SYNOPSIS
use Graphics::Primitive::Insets; my $insets = Graphics::Primitive::Insets->new({ top => 5, bottom => 5, left => 5, right => 5 }); METHODS
Constructor new Creates a new Graphics::Primitive::Insets. Instance Methods as_array Return these insets as an array in the form of top, right, bottom and left. bottom Set/Get the inset from the bottom. equal_to Determine if these Insets are equal to another. left Set/Get the inset from the left. right Set/Get the inset from the right. top Set/Get the inset from the top. zero Sets all the insets (top, left, bottom, right) to 0. AUTHOR
Cory Watson, "<gphat@cpan.org>" SEE ALSO
perl(1) COPYRIGHT &; LICENSE Copyright 2008-2010 by Cory G Watson. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.3 2010-08-21 Graphics::Primitive::Insets(3pm)
All times are GMT -4. The time now is 08:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy