This question was asked quite a lot, but specifically in regards to structs containing pointers, and never helped my situation fully. What I'm trying to do, is strtok() the first and only command line argument based on the "|" character. For example, it will be something like: "ls -l | grep ^d | wc -l." After that is complete, I want to write to a LOGFILE the items I tokenized. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
void main(void)
{
FILE *logfp = fopen("LOGFILE", "w");
char * commands;
char * container[4];
char commandLine[] = ("test|test2|test3|test4\n");
int i = 0;
commands = strtok(commandLine, "|");
while(commands != NULL)
{
container[i] = commands;
printf("Being stored in container: %s\n", container[i]);
i++;
commands = strtok(NULL, "|");
}
printf("This is the size of the container: %d\n", (int)sizeof(container));
fwrite(container,1,sizeof(container),logfp);
fclose(logfp);
}
sizeof() on a pointer returns 8 instead of the correct amount for a char too, so that's another problem. In addition to that, the logfile is full of what I'm guessing is memory addresses to where the pointers are pointing. I want to write the tokenized strings into the LOGFILE. How would I do that?