<div>본삭금 걸어서 본문 수정은 안되는군요;; </div> <div>답변 주신분들 다시 한번 감사드리며 다음과 같이 해결했습니다.</div> <div><br></div> <div>----------------------------------------------------------</div> <div>#define _CRT_SECURE_NO_WARNINGS</div> <div>#include </div> <div>#include </div> <div>#include </div> <div><br></div> <div>//일단 파일의 줄을 세는 함수를 정의했습니다.</div> <div>int Countlines(FILE *fp)</div> <div>{</div> <div><span style="white-space:pre;"> </span>int ch = 0;</div> <div><span style="white-space:pre;"> </span>int lines = 0;</div> <div><span style="white-space:pre;"> </span>int currPos = ftell(fp);</div> <div><br></div> <div><span style="white-space:pre;"> </span>fseek(fp, 0, SEEK_SET);</div> <div><br></div> <div><span style="white-space:pre;"> </span>if (fp == NULL)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>return 0;</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>lines++;</div> <div><span style="white-space:pre;"> </span>while (feof(fp)==0)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>ch = fgetc(fp);</div> <div><span style="white-space:pre;"> </span>if (ch == '\n')</div> <div><span style="white-space:pre;"> </span>lines++;</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>fseek(fp, currPos, SEEK_SET);</div> <div><span style="white-space:pre;"> </span>return lines;</div> <div>}</div> <div>//위 함수를 이용해 파일을 라인마다 읽어 들이는 함수를 정의했습니다. 이중포인터를 결과값으로 반환합니다.</div> <div>char **getData(FILE *fp)</div> <div>{</div> <div><span style="white-space:pre;"> </span>char **getdata = malloc(sizeof(char *)*Countlines(fp));</div> <div><span style="white-space:pre;"> </span>int currPos = ftell(fp);</div> <div><br></div> <div><span style="white-space:pre;"> </span>fseek(fp, 0, SEEK_SET);</div> <div><span style="white-space:pre;"> </span></div> <div><span style="white-space:pre;"> </span></div> <div><span style="white-space:pre;"> </span>for (int i = 0; i < Countlines(fp); i++)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>getdata[i] = malloc(sizeof(char *)*255);</div> <div><span style="white-space:pre;"> </span>memset(getdata[i], '\0', sizeof(char *)*255);</div> <div><span style="white-space:pre;"> </span>fgets(getdata[i], sizeof(char *)*255, fp);</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span></div> <div><br></div> <div><span style="white-space:pre;"> </span>fseek(fp, currPos, SEEK_SET);</div> <div><br></div> <div><span style="white-space:pre;"> </span>return getdata;</div> <div><br></div> <div><span style="white-space:pre;"> </span>for (int i = 0; i < Countlines(fp); i++)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>free(getdata[i]);</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>free(getdata);</div> <div><br></div> <div>}</div> <div>//길이가 제일 긴 줄의 크기를 구하는 함수인데, 정확히 하려니 생각보다 피곤해서;; 2자리수(문자) + 공백1자리 </div> <div>//strlen 길이를 쟨후 3으로 나누고, 손실이 생길걸 대비해 그냥 2더했습니다;;ㅎㅎ</div> <div>int Max_Line(FILE *fp)</div> <div>{</div> <div><span style="white-space:pre;"> </span>int size = 0;</div> <div><span style="white-space:pre;"> </span>int ch = 0;</div> <div><span style="white-space:pre;"> </span>int lines = 0;</div> <div><span style="white-space:pre;"> </span>int currPos = ftell(fp);</div> <div><br></div> <div><span style="white-space:pre;"> </span>fseek(fp, 0, SEEK_SET);</div> <div><br></div> <div><span style="white-space:pre;"> </span>if (fp == NULL)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>return 0;</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span></div> <div><span style="white-space:pre;"> </span>while (feof(fp) == 0)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>ch = fgetc(fp);</div> <div><span style="white-space:pre;"> </span>if (ch != '\n')</div> <div><span style="white-space:pre;"> </span>lines++;</div> <div><span style="white-space:pre;"> </span>else if (size < lines)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>size = lines;</div> <div><span style="white-space:pre;"> </span>lines = 0;</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>fseek(fp, currPos, SEEK_SET);</div> <div><span style="white-space:pre;"> </span>return (size/3 +2);</div> <div>}</div> <div><br></div> <div>//아래에서 파일 포인터를 정의하고, 위 함수들을 이용해</div> <div>//문자 이중포인터에 파일을 줄마다 저장하고, 그것을 strtol을 이용해 두번째 정수 이중포인터에 정수로 치환하여 대입해서 출력했습니다. ㅎ</div> <div>int main()</div> <div>{</div> <div><span style="white-space:pre;"> </span>FILE *fp = fopen("Problem18.txt", "r");</div> <div><span style="white-space:pre;"> </span>char **charmatrix;</div> <div><span style="white-space:pre;"> </span>int **intmatrix = malloc(sizeof(int *)*Countlines(fp));</div> <div><span style="white-space:pre;"> </span>char *end;</div> <div><span style="white-space:pre;"> </span>charmatrix = getData(fp);</div> <div><span style="white-space:pre;"> </span></div> <div><span style="white-space:pre;"> </span>printf("%d\n", Max_Line(fp));</div> <div><span style="white-space:pre;"> </span></div> <div><span style="white-space:pre;"> </span>for (int i = 0; i < Countlines(fp); i++)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>intmatrix[i] = malloc(sizeof(int)* Max_Line(fp));</div> <div><span style="white-space:pre;"> </span>memset(intmatrix[i],'\0',sizeof(int)* Max_Line(fp));</div> <div><span style="white-space:pre;"> </span>}</div> <div><br></div> <div><br></div> <div><span style="white-space:pre;"> </span>for (int i = 0; i < Countlines(fp); i++)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>printf("%s\n", charmatrix[i]);</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span></div> <div><span style="white-space:pre;"> </span></div> <div><span style="white-space:pre;"> </span>for (int i = 0; i< Countlines(fp); i++)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>end = NULL;</div> <div><span style="white-space:pre;"> </span>intmatrix[i][0] = (int)strtol(charmatrix[i], &end, 10);</div> <div><span style="white-space:pre;"> </span>for (int j = 1; j < Max_Line(fp); j++)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>intmatrix[i][j] = (int)strtol(end, &end, 10);</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span></div> <div><span style="white-space:pre;"> </span>for (int i = 0; i < Countlines(fp); i++)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>for (int j = 0; j < Max_Line(fp); j++)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>printf("%d ", intmatrix[i][j]);</div> <div><span style="white-space:pre;"> </span>}</div> <div><span style="white-space:pre;"> </span>printf("\n");</div> <div><span style="white-space:pre;"> </span>}</div> <div><br></div> <div><br></div> <div><span style="white-space:pre;"> </span></div> <div><span style="white-space:pre;"> </span>for (int i = 0; i < Countlines(fp); i++)</div> <div><span style="white-space:pre;"> </span>{</div> <div><span style="white-space:pre;"> </span>free(intmatrix[i]);</div> <div><span style="white-space:pre;"> </span>}</div> <div><br></div> <div><span style="white-space:pre;"> </span>free(intmatrix);</div> <div><span style="white-space:pre;"> </span>fclose(fp);</div> <div><span style="white-space:pre;"> </span>return 0;</div> <div><br></div> <div>}</div> <div>------------------------------------------------------------------------------------------------------</div> <div>이제 문제를 풀어야겠습니다 ㅎㅎㅎ 다들 수고하세요ㅎㅎ</div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.