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
PAPS(1) 						      General Commands Manual							   PAPS(1)

NAME
paps - UTF-8 to PostScript converter using Pango SYNOPSIS
paps [options] files... DESCRIPTION
paps reads a UTF-8 encoded file and generates a PostScript language rendering of the file. The rendering is done by creating outline curves through the pango ft2 backend. OPTIONS
These programs follow the usual GNU command line syntax, with long options starting with two dashes (`-'). A summary of options is included below. --landscape Landscape output. Default is portrait. --columns=cl Number of columns output. Default is 1. Please notice this option isn't related to the terminal length as in a "80 culums terminal". --font=desc Set the font description. Default is Monospace 12. --rtl Do right to left (RTL) layout. --paper ps Choose paper size. Known paper sizes are legal, letter and A4. Default is A4. Postscript points Each postscript point equals to 1/72 of an inch. 36 points are 1/2 of an inch. --bottom-margin=bm Set bottom margin. Default is 36 postscript points. --top-margin=tm Set top margin. Default is 36 postscript points. --left-margin=lm Set left margin. Default is 36 postscript points. --right-margin=rm Set right margin. Default is 36 postscript points. --gutter-width=gw Set gutter width. Default is 40 postscript points. --help Show summary of options. --header Draw page header for each page. --markup Interpret the text as pango markup. --lpi Set the lines per inch. This determines the line spacing. --cpi Set the characters per inch. This is an alternative method of specifying the font size. --stretch-chars Indicates that characters should be stretched in the y-direction to fill up their vertical space. This is similar to the texttops behaviour. AUTHOR
paps was written by Dov Grobgeld <dov.grobgeld@gmail.com>. This manual page was written by Lior Kaplan <kaplan@debian.org>, for the Debian project (but may be used by others). April 17, 2006 PAPS(1)
All times are GMT -4. The time now is 04:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy