c언어로 텍스트 파일을 읽어오는 프로그램을 만들고 있는데.....

moonrepeat의 이미지

c언어 초짜입니다..... 계속 삽질 중입니다. 도와주시면 정말 감사.....
(환경은 window xp이고 컴파일러는 dev c++입니다.)

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

#define BUILDING_NO 5
#define BUSSTOP_NO 5

int building_bus[BUILDING_NO][BUSSTOP_NO];

int bus_time();
int human_time();

int main(){
	int i;
	int j;
	int k;
	int len;
	char c;
	char temp_c[5];
    
	FILE *fp;
	fp=fopen("data.txt","r");
	
    memset(temp_c,0,sizeof(temp_c));
	
	for(i=0;i<BUILDING_NO;i++){
		for(j=0;i<BUSSTOP_NO;i++){
 			while((c=fgetc(fp))!=','){
 				len=strlen(temp_c);
 				temp_c[len]=c;
     		}
     		if(c=='\n')
	                           exit;
  		    building_bus[i][j]=atoi(temp_c);
		    memset(temp_c,0,sizeof(temp_c));
		}
	}
    
	for(i=0;i<BUILDING_NO;i++){
		for(j=0;i<BUSSTOP_NO;i++){
			printf("%d ",building_bus[i][j]);
		}
		
		printf("\n");
	}

	getchar();
	return 0;
}

 data.txt 파일 내용
216,2,95,68,89222,
77,17,70,76,43,
35,73,4558,4,41,
268,75,21,49,11147,
84,13,93,31,70,

제가 원하는 건 파일을 읽어서 n X m 배열로 저장을 하는겁니다.....
(나중에 계산을 하기위해 int형으로 변환)

그런데 실행시켜 보면

216 2 95 68 89222

라고 한줄만 나옵니다.
아마도 제가 줄 바꾸는 걸 할줄 몰라서 그러는것 같은데
조언 주시면 정말 감사하겠습니다.

marten의 이미지

          if(c=='\n') 
                              exit; 

한줄 읽고 나서, '\n'이 나오면 그냥 exit 해버리네요...
이게 문제일듯..

etermory의 이미지

exit; 부분을 break; 로 바꿔주시고
루프를 보면 변수가 모두 i(아이)로 되어있네요..
잘 보시고 j(제이)로 바꿔주세요.

moonrepeat의 이미지

컥........... i...... j 괜히 삽질하고 있었군요.........

삽질은 계속되어야 한다....... 쭉.........

etermory의 이미지

한 번 실수하면 자신은 아무리 들여다 봐도 쉽게 알기 어려운 버그 입니다. ^^;;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.