C의 구조체 문제

user3287789

구조체에 배열이 있습니다. 파일에서 문자열로 읽는 중입니다. 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;   
}
Fernando

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

구조체의 문제 (C ++)

분류에서Dev

C의 구조체와 배열 문제

분류에서Dev

C 구조체 구문 문제

분류에서Dev

구조체 간의 @binding 문제

분류에서Dev

구조체의 포인터 문제

분류에서Dev

Objective C 구조체 구문

분류에서Dev

C의 구조체에서 char 배열의 strcpy 문제

분류에서Dev

C의 구조 내부 구조 문제

분류에서Dev

C ++의 공용체 내부 구조 액세스 문제

분류에서Dev

C의 구조체 배열로 파일 읽기 문제

분류에서Dev

C ++ 구조 문제

분류에서Dev

구조체의 문자열

분류에서Dev

C의 객체-구조체 구문 관련

분류에서Dev

구조 문제의 정의

분류에서Dev

c의 중첩 된 구조체, 포인터 주소 지정 문제

분류에서Dev

매크로를 사용할 때 구조체 초기화 문제의 C 배열

분류에서Dev

C ++-구조체의 순서가 지정되지 않은 맵 메모리 문제

분류에서Dev

pthread를 사용할 때 구조체의 배열 전달 문제

분류에서Dev

구조체 배열 초기화 문제 C ++

분류에서Dev

C에서 조건문 내부의 구조체 평가

분류에서Dev

Objective-C 구조 문제

분류에서Dev

C 구조 패딩 문제

분류에서Dev

전체 구조체의 문자열 구문 분석

분류에서Dev

구조체의 int를 C의 문자열로 변환

분류에서Dev

구조체의 메모리 제한

분류에서Dev

C에서 구조체 배열을 포함하는 구조체를 만드는 데 문제가 있습니다.

분류에서Dev

구조의 구문 [c]

분류에서Dev

Scala Eclipse의 구문 강조 문제

분류에서Dev

구조체 C ++의 메모리 할당 및 할당 해제