이 질문은 상당히 많이 들었지만 특히 포인터가 포함 된 구조체와 관련하여 내 상황을 완전히 도왔습니다. 내가 뭘 하려는지, strtok() 명령 줄 인수는 "|" 캐릭터. 예를 들면 다음과 같습니다 : "ls -l | grep^d | wc -l" 완료되면, 내가 토큰 화 한 항목을 LOGFILE에 쓰려고합니다. 그건 또 다른 문제, 그래서 포인터를 반환 (8) 대신에 너무 문자에 대한 정확한 금액에 대한fwrite()를 사용하여 포인터에서 쓰기
#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() : 여기 내 코드입니다. 그 외에도 로그 파일은 포인터가 가리키는 메모리 주소를 추측하고 있습니다. 토큰 화 된 문자열을 LOGFILE에 기록하려고합니다. 내가 어떻게 그럴 수 있니?