txt파일로 문자열을 입력받을때...

ljs0705의 이미지

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

int main( void ){
	char b[100];
	FILE *f;
	if( ( f = fopen( "test.txt", "w" ) ) == NULL )
		return 1;
	scanf("%s", b);			
                //만약 "우리나라는 삼면이 바다이다."를 입력하면 test.txt파일에 
                //"우리나라는"만 출력된다.
	fprintf( f, "%s", b );
	fclose( f );

	return 0;
}

입력을 받은후 "우리나라는 삼면이 바다이다."를 모두 파일에 출력하려면 어찌해야하나요...
xyhan의 이미지

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
char c;
int in,out;
out=open("file.out",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);

while(scanf("%c",&c))
write(out,&c,1);

exit(0);
}
~

그냥 끝낼때는 Ctr+

============================================================

선한 인간이냐 악한 인간이냐는 그사람의 의지에 달렸다. -에픽테토스-
의지 노력 기다림은 성공의 주춧돌이다. -파스퇴르-

============================================================

sozu의 이미지

ljs0705 wrote:

scanf("%s", b);
//만약 "우리나라는 삼면이 바다이다."를 입력하면 test.txt파일에
//"우리나라는"만 출력된다.

scanf는 스트링의 구분자를 white space와 new line으로 합니다.

그러므로 당근 하나의 스트링만을 입력받았으므로 '우리나라는'밖에 입력되지 않는 것입니다.

gets(b); <- new line만을 구분자로 삼죠

로 하신다면 모두 입력받으실수 있습니다.

-----------
청하가 제안하는 소프트웨어 엔지니어로써 재미있게 사는 법
http://sozu.tistory.com

new5244의 이미지

윗분 답변대로 scanf ( "%s" ) 는 공백문자가 나오기 전까지의 string 만 입력받을 수 있습니다.

stdin 으로 입력을 받으시려면 gets() 대힌 fgets() 를 사용하시는게 좋을 것입니다. man page 확인해 보세요.

아참 파일에 기록할때 fgets 에서 "\n" 까지 입력되므로 만약 "\n" 을 제거하시려면 fprintf 대신 fputs() 를 사용하세요.

from saibi

dingdong77의 이미지

%s와 같은 대신 white space에서는 안끊기고 newline 에서만 끊기게 하려면

scanf ( "%[^\n]" ) ;

(scanf를 우습게 보지 마십시다. ^^;;)

charsyam의 이미지

dingdong77 wrote:
%s와 같은 대신 white space에서는 안끊기고 newline 에서만 끊기게 하려면

scanf ( "%[^\n]" ) ;

(scanf를 우습게 보지 마십시다. ^^;;)

오옷... 이런게 있었네요. 지금까지 항상 scanf는 좀 우습게 보는 경향이

있었는데 멋집니다. ^^ 덕분에 좋은걸 알았습니다. 고운 하루되세요.

=========================
CharSyam ^^ --- 고운 하루
=========================

cedar의 이미지

dingdong77 wrote:
%s와 같은 대신 white space에서는 안끊기고 newline 에서만 끊기게 하려면

scanf ( "%[^\n]" ) ;

(scanf를 우습게 보지 마십시다. ^^;;)


근데 이건 ANSI C 호환이 아닙니다.
다른 플랫폼에서는 사용 불가능합니다.

댓글 달기

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