fstream >> int 에서 type이 맞지 않을때?

lacovnk의 이미지

int a;
fstream fs("bla~");
fs >> a;

이랬는데, bla~의 내용에 정수가 아닌 캐릭터가 있다고 합시다 ㅎ

그러면 a의 값은 아무 변화가 없더군요;

이럴 경우 a에 특정 값을 넣어주려고 하는데, 그럼 결과에 대한 flag가 있어야 할 것 같은데.. (그리고 있을 것 같은데..)

못찾겠네요 :oops:

이런 표준 클래스에 대한 man은 없나요? 우웅..

lacovnk의 이미지

try-catch로 잡을 수도 있나요? 으음..

in >> a;
if(1 == in.fail()){a=bla;}

이 경우 failbit말고, badbit가 설정될 때에도 a가 bla로 설정되니.. 각각 예외를 catch하고 싶어졌어요 ㅎㅎ

doldori의 이미지

if (!(fs >> a))
    // input failed

이런 식으로 씁니다. fs >> a는 fs의 참조를 반환하고 이것을 boolean 식에 쓰면
입력이 성공했을 때 true가 됩니다. 입력이 실패했을 때 예외를 던지는 것은 좀 과하지
않을까 하는 생각이 드네요. 뭐, 상황에 따라 달라지겠지만요.
lacovnk의 이미지

에러가 발생한 이후, 스트림의 상태가 어떻게 되었는지 모르겠네요;;

int형으로 받는 것 실패하고, string으로 이어서 받으려하니, 아무것도 못받네요;

if(!(fs >>a))
{
string temp;
fs >> temp;
cout << temp;
}

temp가 비어있어요~ 흑.. 그리고 저것이 루프로 돌아가는데, 실패가 발생한 이후 부터 스트림에서 못 읽어오는 것 같습니다;

어떻게 처리해야하나요? 실패한 부분(token?)만 넘어가면 되는데..

doldori의 이미지

입출력 중 에러가 발생하면 스트림의 failbit 또는 badbit가 켜지고 그 이후로는
모든 입출력이 중단됩니다. basic_ios::clear()를 써서 스트림의 상태를 복구하면
다시 입출력을 재개할 수 있습니다. (badbit가 켜진 경우에는 정상적인 동작을
기대할 수 없음)

if (!(fs >>a)) 
{
    fs.clear(); 
    string temp; 
    fs >> temp; 
    cout << temp; 
}

댓글 달기

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