Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can i use output from sprintf as the filename for fopen?
    primarykey
    data
    text
    <p>I am working on a log parsing program that retrieves the file to open by combining an environment variable and a preset string in order to provide the full path to the file, but i am having trouble getting fopen to take out the output from sprintf which i am using to combine the environment variable and the preset string,so i was wondering if anyone could offer advice on what i should do to get this to work properly? thanks! (i have just begun teaching myself C over the last few weeks, so im open to any tips no matter how obvious they should be to me)</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #define _GNU_SOURCE void main(int argc, char *argv[], char *envp[]) { FILE *fd; // File pointer char *name; char *filename[]; name = getenv("MCEXEC_PLAYERNAME"); sprintf(filename,"/home/minecraft/freedonia/playerdata/deathlog-%s.txt",name); char buff[1024]; if ((fd = fopen(filename, "r")) != NULL) // open file { fseek(fd, 0, SEEK_SET); // make sure start from 0 while(!feof(fd)) { memset(buff, 0x00, 1024); // clean buffer fscanf(fd, "%[^\n]\n", buff); // read file *prefer using fscanf } printf("Last Line :: %s\n", buff); } else printf( "fail" ); } </code></pre> <p>here is the error i get while compiling using gcc</p> <pre><code>lastline.c: In function ‘main’: lastline.c:9: error: array size missing in ‘filename’ lastline.c:11: warning: passing argument 1 of ‘sprintf’ from incompatible pointer type /usr/include/stdio.h:341: note: expected ‘char * __restrict__’ but argument is of type ‘char **’ lastline.c:13: warning: passing argument 1 of ‘fopen’ from incompatible pointer type /usr/include/stdio.h:249: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload