1

I want to write the PID to a specific file. It echoes the pid in terminal so far. But how to write this PID to a file?

int main()
{
pid_t pid = getpid();
printf("%lu\n", pid);

char *filename = "/.wirebot/pid.txt";
char *home_dir = getenv("HOME");
char *filepath = malloc(strlen(home_dir) + strlen(filename) + 1);
strncpy(filepath, home_dir, strlen(home_dir) + 1);
strncat(filepath, filename, strlen(filename) + 1);

FILE *fp;
fp = fopen(filepath, "w");
 if (fp == NULL) {
} else {
    fputs("%lu\n", pid, fp);
    fclose(fp);
    }

}

Too many arguments at fputs is the error I get.

Sascha7777
  • 89
  • 8
  • 5
    Using `fprintf` instead of `fputs`? Didn't you get compilr errors/warning? Did you include `stdio.h`? – Jabberwocky Jan 10 '23 at 15:01
  • Function [`freopen`](https://www.tutorialspoint.com/c_standard_library/c_function_freopen.htm) can be used to redirect STDIN to a file instead. – abelenky Jan 10 '23 at 15:02
  • @abelenky it's unclear what he actually wants to do, because he already tries to print the pid into a file with `fputs("%lu\n", pid, fp);` which is of course wrong. – Jabberwocky Jan 10 '23 at 15:03
  • BTW: unrelated to your problem: don't use `strncpy` it probably doesn't do what ylou think it does. Read it's documentation carefully. `strncpy` is not the samewhat safer version of `strcpy`. – Jabberwocky Jan 10 '23 at 15:06
  • @abelenky `freopen` was also my first thought. But the question was about STDOUT not the input. – harper Jan 10 '23 at 15:09
  • @harper: Typo on my part: I meant redirecting `stdout`, not `stdin`. But the answer remains: freopen is a likely candidate for what they might be trying to do. – abelenky Jan 10 '23 at 15:12
  • 1
    Sascha7777, Aside: Consider simplifying `strncpy(filepath, home_dir, strlen(home_dir) + 1); strncat(filepath, filename, strlen(filename) + 1);` to `sprintf(filepath, "%s%s", home_dir, filename);`. Let the compiler emit efficient code. – chux - Reinstate Monica Jan 10 '23 at 16:14
  • @chux Thx, I did it and it works so far. Nice. – Sascha7777 Jan 10 '23 at 17:11

3 Answers3

2

How to redirect a printf output to file?

Use freopen(new_filename, "w", stdout)

if (freopen(new_filename, "w", stdout)) {
  fprintf(stderr, "Success\n");
  printf("%lld\n", (long long) pid);
} else {
  fprintf(stderr, "Failed\n"):
}

But how to write this PID to a file?

Could directly use fprintf().

  // fputs("%lu\n", pid, fp);
  fprintf(fp, "%lld\n", (long long) pid);

See also What is the correct printf specifier for printing pid_t.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

You should take a look at fprintf() function. Just remember to check(From you code i guess you are probably trying things with processes and then you will try it with forking), how to handle multiple processes writing to the same file.

0

Here is a sample code to do that. You open a file and then use fprintf to write into it.

// C Program for the above approach  
#include<stdio.h>
int main()
{
    int i, n=2;
    char str[50];
   
    //open file sample.txt in write mode
    FILE *fptr = fopen("sample.txt", "w");
    if (fptr == NULL)
    {
        printf("Could not open file");
        return 0;
    }
   
    for (i = 0; i < n; i++)
    {
        puts("Enter a name");
        scanf("%[^\n]%*c", str);
        fprintf(fptr,"%d.%s\n", i, str);
    }
    fclose(fptr);
   
    return 0;
}
brushtakopo
  • 1,238
  • 1
  • 3
  • 16