구조체에 배열이 있습니다. 파일에서 문자열로 읽는 중입니다. strtok를 사용하여 처음 몇 개의 문자를 가져오고 나머지 줄을 구조체에 전달하여 결국 스레드로 전달하고 싶습니다. 다음과 같은 오류가 발생합니다.
유형 char[1024]
에서 유형에 할당 할 때 호환되지 않는 유형char *
주석과 함께 아래 표시된 줄을 참조하십시오. 아마도 문자 배열을 복사하려는 방법과 관련이 있지만 더 나은 방법은 확실하지 않습니다.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <linux/input.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
typedef struct
{
int period; //stores the total period of the thread
int priority; // stores the priority
char pline[1024]; // stores entire line of text to be sorted in function.
}PeriodicThreadContents;
int main(int argc, char* argv[])
{
//opening file, and testing for success
//file must be in test folder
FILE *fp;
fp = fopen("../test/Input.txt", "r");
if (fp == NULL)
{
fprintf(stderr, "Can't open input file in.list!\n");
exit(1);
}
char line[1024];
fgets(line, sizeof(line), fp);
//getting first line of text, containing
char *task_count_read = strtok(line," /n");
char *duration_read = strtok(NULL, " /n");
//converting char's to integers
int task_count = atoi(task_count_read);
int i = 0;
PeriodicThreadContents pcontents;
printf("started threads \n");
while (i < task_count)
{
fgets(line, sizeof (line), fp);
strtok(line," ");
if (line[0] == 'P')
{
char *period_read = strtok(NULL, " ");
pcontents.period = atoi(period_read);
printf("%d",pcontents.period);
printf("\n");
char *priority_read = strtok(NULL, " ");
pcontents.priority = atoi(priority_read);
printf("%d",pcontents.priority);
printf("\n");
printf("\n%s",line);
memcpy(&(pcontents.pline[0]),&line,1024);
printf("%s",pcontents.pline);
}
}
return 0;
}
C는 다른 언어처럼 문자열을 처리 할 수 없습니다. C에는 보조 함수를 사용하지 않고 문자열 할당이나 비교가 없습니다.
버퍼에 문자열을 복사하려면 다음을 사용할 수 있습니다.
strcpy(pcontents.pline, line);
또는 (문자열이 1024 바이트를 넘지 않는다는 보증을 받으려면) :
memcpy(pcontents.pline, line, 1024);
pcontents.pline[1023] = '\0';
다른 문자열 작업 확인 : http://www.gnu.org/software/libc/manual/html_node/String-and-Array-Utilities.html#String-and-Array-Utilities
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다