C언어 관련 질문 드립니다~

khjkag@kldp.org의 이미지

메모리에서 질문 드릴게 있습니다,,

첫번째 프로그램이

----------------------------------------------------

int main()
{
FILE *fp;
char *strz[]={"fprintf()는", "훌륭한","문자열","출력 함수다."};
int x=100;

if((fp=fopen("abc.txt","w"))==NULL)
{
printf("<파일을 열 수 없습니다.>");
return 1;
}
{

fprintf(fp, "%s %d%% %s %s %s", strz[0], x, strz[1], strz[2], strz[3]);

fclose(fp);

}

----------------------------------------------------------------------------
위의 첫번째 프로그램을 실행한 다음에

아래 두번째 프로그램을 실행하면,

----------------------------------------------------------------------------

#include

int main()
{

FILE *fp;
char buf1[256], buf2[256];

if((fp=fopen("abc.txt", "r"))==NULL)
{
printf("파일을 열 수 없습니다.");
return 1;
}

{

printf("파일을 열었습니다.\n");

fscanf(fp, "%s %s", buf1,buf2);
printf("%s %s", buf1, buf2);

fclose(fp);

printf("\n파일을 닫았습니다.");
}

return 0;
}

----------------------------------------

두번째 프로그램 실행 결과는

----------------------------------------
파일을 열었습니다.
fprintf()는 100%
파일을 닫았습니다.

----------------------------------------

이렇게 나오는데, 첫번째 프로그램의

char *strz[]={"fprintf()는", "훌륭한","문자열","출력 함수다."};

이 부분에서 *strz[]가 가지게 되는 char 타입 변수의 주소는

두번째 프로그램의

buf1, buf2 변수의 주소가 되는 것입니까??

그리고 두번째 프로그램이 생성되기 전,

첫번째 프로그램에서 *strz[]가 가지고 있던 메모리의 주소는

나중에 생성되는 두번째 프로그램의

buf1, buf2 변수의 주소와 같은 값은 아니었지요?

초보라서 질문이 요상합니다만..;;

두번째 질문~!
------------------------------- 첫번째 프로그램 -------------------
#include

int main()
{
int account;
char name[ 30 ];
double balance;

FILE *cfPtr;


if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
printf( "Enter the account, name, and balance.\n" );
printf( "Enter EOF to end input.\n" );
printf( "? " );
scanf( "%d%s%lf", &account, name, &balance );


while ( !feof( stdin ) ) {
fprintf( cfPtr, "%d %s %.2f\n", account, name, balance );
printf( "? " );
scanf( "%d%s%lf", &account, name, &balance );
}

fclose( cfPtr );
}

return 0;

}

위의 프로그램을 실행시키고(자료를 입력받는 프로그램)
---------------------------------------------------- 2번째 프로그램 -------

#include

int main()
{
int account;
char name[ 30 ];
double balance;

FILE *cfPtr;


if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" );
fscanf( cfPtr, "%d%s%lf", &account, name, &balance );


while ( !feof( cfPtr ) ) {
printf( "%-10d%-13s%7.2f\n", account, name, balance );
fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
}

fclose( cfPtr );
}

return 0;

}


을 실행시키면 실행 결과가 나옵니다.(입력 받은 자료가 출력되는 프로그램입니다)

그런데 두번째 프로그램에서 이부분,

int account;
char name[ 30 ];
double balance;

변수 선언 부분은 필요없지 않나요?

fscanf는 다른 파일에서 데이터를 읽어오는 것인데 2번째 함수 부분의 변수는

선언하지 않아도 되는 것 아닌가요??

답변 부탁드려요..^^

바라미의 이미지

글의 요지를 파악하기가 힘들어서..
정확한 답이 될지 안될지 모르겠습니다만..

파싱부분에서도 문제가 좀 있는듯 한데요..;
fscanf 사용법을 좀 자세히 알아보셔야 할것 같네요..
%s 옵션은 널문자가 아닌 문자를 한번에 읽어들입니다..

위에서 출력을 %s %s %s .. 이렇게 지정하고 출력 한다음에
읽어들일때 똑같이 %s %s %s 로 출력하는데...

출력할때는 출력이 되지만 읽어들일때는 아마 그줄의 첫글자부터 끝 글자까지를 한개의 %s 로 간주하고 전부 다 읽어비리니 뒤의 %s 들은 무시가 될텐데요..

그리고 scanf 계열 함수들이 왜 변수가 필요하냐면.. 함수 사용 자체도. 변수명을 주지 않는데요. 변수에다가 & 문자를 왜 붙이는지 알아보셔야 할겁니다..

C 책에 이유가 나와있을테니 한번더 자세히 읽어보시고 다시해보세요..

khjkag@kldp.org의 이미지

감사합니다~!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.