2017-03-03 13 views
0

C를 사용하여 P3 PPM 파일을 저장하려고 시도하고 있지만 벽돌 벽에 부딪혔습니다. 누군가 내 일을 완수하는 데 나를 도울 수 있는지 궁금해하고있었습니다. 고맙습니다.PPM P3 파일의 모든 값을 저장합니다.

struct PPM 및 struct PPM * getPPM (File * fd)으로 유지하고 싶지만 내용을 변경할 수 있습니다.

또한 의견을 저장하는 방법을 알지 못하여이를 수행하고 싶습니다.

는 기본적으로 나는 다음과 같이 데이터를 저장하는 것을 시도하고있다 :

P3 
#comment.1 
. . . 
#comment.n 
width height 
max 
r1 g1 b1 
r2 g2 b2 
r3 g3 b3 
. . . 

편집 : 내가 제대로 컴파일하고 지금은 너무 ARGV을 통과하려고에 대한 변경 사항을 수행 한

그 파일은 파일 이름을 얻기 위해 argv [1]를 읽을 수 있습니다. 그렇게하면 분할 오류가 발생합니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

//Holds the data 
struct data {int r,g,b;}; 
struct PPM {char code[4]; char comments; int width; int height; int max; struct data *Data;}; 

//Gets the PPM data 
struct PPM* GetPPM(FILE * fd, argv) 
{ 
    char readChars[256] = {0}; 
    //Allocates memory for PPM 
    struct PPM *image = (struct PPM *)calloc(1, sizeof(struct PPM)); 
    int i; 
    fgets(image->code, sizeof(image->code), fd); 
    fd = fopen(argv[1], "r"); 
    //Checks if file is type P3 
    if((image->code[0] != 'P') && (image->code[0] != '3')) 
    { 
      return NULL; 
    } 
    image->code[2] = '\0'; 
    //Checks for comments then continues around the loop until there's no more 
    fgets(readChars, sizeof(readChars), fd); 
    while(readChars[0] == '#') 
    { 
      fgets(readChars, sizeof(readChars), fd); 
    } 
    //Checks for PPM width, height and max 
    sscanf(readChars, "%d %d", &image->width, &image->height); 

    fgets(readChars, sizeof(readChars), fd); 
    sscanf(readChars, "%d", &image->max); 

    image->Data = (struct data*)malloc(image->width * image->height * sizeof(struct data)); 
    i = 0; 
    while(fgets(readChars, sizeof(readChars), fd)); 
    { 
      sscanf(readChars, "%d %d %d", &(image->Data[i].r), &(image->Data[i].g), &(image->Data[i].b)); 
      ++i; 
    } 
    fclose(fd); 
    return image; 
} 


//Deallocates memory 
void freePPM(struct PPM *image) 
{ 
    free(image->Data); 
    free(image); 
} 

//Displays PPM 
void showPPM(struct PPM *image) 
{ 
    int i = 0; 
    int totalpixels = image->width * image->height; 
    printf("%s\n", image->code); 
    printf("%d %d\n", image->width, image->height); 
    printf("%d\n", image->max); 

    for(i = 0; i < totalpixels; ++i) 
    { 
      printf("%d %d %d\n", image->Data[i].r, image->Data[i].g, image->Data[i].b); 
    } 
} 



int main(int argc, char ** argv) 
{ 
    struct PPM *image = GetPPM(argv); 
    showPPM(image); 
    freePPM(image); 

    return 0; 
} 

답변

0

구조체를 선언 할 때 오류가 발생하며 데이터를 읽는 방법에 차이가 있습니다. 다음 코드에서 데이터를 읽는 방법을 확인하고 더 명확하게 보려면 여기로 돌아와주십시오.

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct data {int r,g,b;}; 
struct PPM {char code[4]; char comments; int width; int height; int max; struct data *Data;}; 

struct PPM* GetPPM(FILE * fd) 
{ 
    char readChars[256] = {0}; 
    struct PPM *image = (struct PPM *)calloc(1, sizeof(struct PPM)); 
    int i; 

    fgets(image->code, sizeof(image->code), stdin); 

    if((image->code[0] != 'P') && (image->code[0] != '3')) 
    { 
     return NULL; 
    } 
    image->code[2] = '\0'; 

    fgets(readChars, sizeof(readChars), stdin); 
    while(readChars[0] == '#') 
    { 
     fgets(readChars, sizeof(readChars), stdin); 
    } 

    sscanf(readChars, "%d %d", &image->width, &image->height); 

    fgets(readChars, sizeof(readChars), stdin); 
    sscanf(readChars, "%d", &image->max); 

    image->Data = (struct data*)malloc(image->width * image->height * sizeof(struct data)); 
    i = 0; 
    while(fgets(readChars, sizeof(readChars), stdin)) 
    { 
     sscanf(readChars, "%d %d %d", &(image->Data[i].r), &(image->Data[i].g), &(image->Data[i].b)); 
     ++i; 
    } 

    return image; 
} 

void FreePPM(struct PPM *image) 
{ 
    free(image->Data); 
    free(image); 
} 

void PrintPPM(struct PPM *image) 
{ 
    int i = 0; 
    int totalpixels = image->width * image->height; 
    printf("%s\n", image->code);  
    printf("%d %d\n", image->width, image->height); 
    printf("%d\n", image->max); 

    for(i = 0; i < totalpixels; ++i) 
    { 
     printf("%d %d %d\n", image->Data[i].r, image->Data[i].g, image->Data[i].b); 
    } 
} 

int main() 
{  
    struct PPM *image = GetPPM(stdin); 
    PrintPPM(image); 
    FreePPM(image); 

    return 0; 
} 
+0

저는 왜 코드에서 fd를 사용한 적이 없는지 궁금합니다. PPM 파일을 읽을 수 없으면 PPM 파일의 데이터를 어떻게 볼 수 있습니까? 아니면 stdin의 모든 인스턴스를 fd로 변경하면됩니까? 그 외는 대부분 좋아 보이는데 대부분 I/O가 더 잘 작동하는 방식을 이해합니다 – Jason

+0

@Jason :'GetPPM (FILE * fd)'함수는'FILE *'을 매개 변수로받습니다. 따라서 필요한 이미지 파일을 열어 유효한 핸들을'GetPPM' 함수에 전달할 수 있습니다. – sameerkn