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)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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" );
}
here is the error i get while compiling using gcc
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 **’
declares an array of pointers to
charof unknown size. You need an array ofchartosprintfto, of sufficient known length. Declareor
as a pointer to
charandmallocsufficient memory after you have gotten the name,to avoid unpleasant surprises if
nameturns out longer than expected.