파일 입출력 에러 원인을 모르겠네요 도와주세요 ㅠㅠ
Hotpants
질문 제목 :파일 입출력 에러 원인을 모르겠네요 도와주세요 ㅠㅠ텍스트 파일로 만들어진 희소행렬을 입력받아 화면에 출력하고 희소행렬간의 덧셈을 하는 소스 입니다. 근데 파일 입력 부분에서 자꾸 문제가 생기네요
아래 소스 부분에서 빨간색으로 된 부분이 문제에 부분입니다.
분명 텍스트 파일이 a가
7 5 4
6 4 79
1 2 51
3 3 57
2 2 44
이런식으로 저장 되있는데 출력을 하면
이런식으로 뜨네요
문제가 무엇인지 알려주시면 감사하겠습니다 ㅠㅠ질문 내용 :
#include stdio.h
#include stdlib.h
#include malloc.h
#include week4_3.h
void main( )
{
sparsematrix m1;
sparsematrix m2;
sparsematrix m3; printf( a\n);
sparse_matrix_init(a.txt, &m1);
sparse_matrix_print(m1); printf( b\n);
sparse_matrix_init(b.txt, &m2);
sparse_matrix_print(m2); printf( a + b = c\n);
m3 = sparse_matrix_add2(m1, m2);
sparse_matrix_print(m3);
}void sparse_matrix_init(const char* file_name, sparsematrix* sparse_matrix)
{
file *fp;
int term=0,n=0;
fp = fopen(file_name,r);
fscanf(fp, %d %d %d\n, &sparse_matrix-rows, &sparse_matrix-cols, &sparse_matrix-terms); while(termsparse_matrix-terms){
fscanf(fp, {%d %d %d}\n, &sparse_matrix-data[n].row, &sparse_matrix-data[n].col, sparse_matrix-data[n].value);
n++;
term++;
}
fclose(fp);
}void sparse_matrix_print(sparsematrix sparse_matrix)
{
int i;
printf(%d %d %d\n,sparse_matrix.rows, sparse_matrix.cols, sparse_matrix.terms);
for(i=0;isparse_matrix.terms;i++) //행 바꾸기 반복문
{
printf(%d %d %d\n,spar\n,sparse_matrix.data[i].row, sparse_matrix.data[i].col, sparse_matrix.data[i].value);
}
printf(\n);
}
sparsematrix sparse_matrix_add2(sparsematrix a, sparsematrix b) //희소 행렬 덧셈 함수
{
sparsematrix c;
int ca=0, cb=0, cc=0; //각 배열의 항목을 가리키는 인덱스
if(a.rows != b.rows || a.cols != b.cols){
fprintf(stderr, 희소행렬 크기에러\n);
exit(1);
}
c.rows = a.rows;
c.cols = a.cols;
c.terms = 0; while(ca a.terms && cb b.terms){ //각 항목의 순차적인 번호를 계산한다.
int inda = a.data[ca].row * a.cols + a.data[ca].col;
int indb = b.data[cb].row * b.cols + b.data[cb].col;
if(inda indb){ //a 배열 항목이 앞에 있으면
c.data[cc++] = a.data[ca++];
}
else if(inda == indb){ //a와 b가 같은 위치
if((a.data[ca].value + b.data[cb].value) != 0){
c.data[cc].row = a.data[ca].row;
c.data[cc].col = a.data[ca].col;
c.data[cc++].value = a.data[ca++].value + b.data[cb++].value;
}
else {
ca++;
cb++;
}
}
else //b 배열 항목이 앞에 있음
c.data[cc++] = b.data[cb++];
}
for(;ca a.terms;)
c.data[cc++] = a.data[ca++];
for(;cb b.terms;)
c.data[cc++] = b.data[cb++];
c.terms = cc;
return c;
}week4_3.h
#define max_terms 10typedef struct {
int row;
int col;
int value;
} element;typedef struct sparsematrix {
element data[max_terms];
int rows; // 행의 개수
int cols; // 열의 개수
int terms; //항의 개수
} sparsematrix;void sparse_matrix_init(const char* file_name, sparsematrix* sparse_matrix);
void sparse_matrix_print(sparsematrix sparse_matrix);
sparsematrix sparse_matrix_add2(sparsematrix a, sparsematrix b);
-
난길 2026-02-13
{}\\n 을 scanf 서식에서 지우세요.
| 번호 | 제 목 | 글쓴이 | 날짜 |
|---|---|---|---|
| 2752085 | 문자열을 랜덤하게 출력해서 대체,삽입,삭제 횟수를 각가 표시하고 그걸 표 형태로 출력 | 슬아s | 2026-10-20 |
| 2752056 | 포인터 && 배열 질문요~ (2) | 해찬나래 | 2026-10-20 |
| 2752031 | x^n 구하는 코드 짜봤는데 값이 틀려요 ㅠㅠ | 세실 | 2026-10-20 |
| 2752000 | 나 정말 미치겠어요..ㅠㅜ (4) | 다이 | 2026-10-20 |
| 2751978 | 문자열 질문이요.. | 초시계 | 2026-10-19 |
| 2751945 | 숫자 맞추는 프로그램이요 ㅠㅠ (1) | 동은 | 2026-10-19 |
| 2751917 | C언어 이중 연결 관련 질문합니다. | 유우 | 2026-10-19 |
| 2751888 | return의 정확한 의미좀 알려주세요 (3) | 올해1살 | 2026-10-19 |
| 2751857 | 포인터 배열 (2) | 터큰 | 2026-10-18 |
| 2751831 | 연산자? 질문입니다 (2) | 진주 | 2026-10-18 |
| 2751800 | C언어 기초질문입니다.. (3) | 핫블랙 | 2026-10-18 |
| 2751739 | 파일 출력을 구조체로 받을 때, 파일의 끝을 판별하는 방법을 알려주세요. (2) | 얼 | 2026-10-17 |
| 2751715 | unresolved external symbol 에러... | 이룩 | 2026-10-17 |
| 2751686 | 변수 설정에 관한 질문입니다. | 꽃달 | 2026-10-17 |
| 2751655 | 자료구조 연습문제 | 한무릎 | 2026-10-17 |
| 2751631 | gets 함수 질문이요. (4) | 해늘 | 2026-10-16 |
| 2751553 | 퀵정렬 소스인데 주석좀 달아주세요 (1) | 싴흐한세여니 | 2026-10-16 |
| 2751528 | 저기 이거하는법좀 질문.. (2) | 가람슬기 | 2026-10-15 |
| 2751445 | 도저히 풀 수가 없네요.... (2) | 신당 | 2026-10-15 |
| 2751412 | 어제부터 계속 해도.........안되는.... (1) | 족장 | 2026-10-14 |
