[완료] C++ 스트링 관련

bluesolip의 이미지

안녕하세요.

text 파일에서 문자열을 읽어와 특정 문자열을 찾으려고 합니다.

- test.txt 내용
aaaaa
bbbbb
ccccc

- 코드 일부
char buffer[128];
ifstream testtxt ("test.txt");
while (! testtxt.eof() )
{
testtxt.getline (buffer,128);
strcmp(buffer, "bbbbb")
{
printf("find");
}
}

파일에서 정상적으로 읽어오는데 "bbbbb" 문자열을 못찾습니다.
그래서 길이를 비교해보니 파일에서 읽은 값은 길이가 6이고 "bbbbb"는 5 라 다르다고 판단한 것이었습니다.
파일에서 한라인을 읽어들일때 NULL값이나 줄바꿈 없이 스트링 "bbbbb"만 읽어오려면 어떻게 해야 하나요?
도움 부탁드립니다.

익명 사용자의 이미지

strstr or strncmp

bluesolip의 이미지

댓글 감사합니다.

text에서 한라인을 읽어와서 카피하면 \n 이 붙잖아요?
이걸 없애고 복사하는 방법을 원해요. for loof로 끝에 있는 0x0d를 없애도 되겠지만 깔끔하게 코딩하는 방법이 있나 해서 여쭤봐요.

익명 사용자의 이미지

*strstr(s,"\n") = \0;

bluesolip의 이미지

ㅎㅎ 간단한 방법이 있었네요.

감사합니다.

qiiiiiiiip의 이미지

std::fstream::getline 함수는 별도로 구분자가 주어지지 않은 경우

\n을 구분자로 간주해서 라인을 읽습니다.

즉 \n이 읽히지 않습니다.

두번째로 fgets 처럼 \n을 읽는다고 하더라도, 이 코드는

예를 들어 한 라인이 128 byte보다 긴 경우 등,

strstr이 NULL을 반환하는 경우 런타임 오류가 날 수도 있겠습니다..

좀 길어지더라도 안전하게 코딩하는게 좋아보이네요..

아래분 말씀처럼 std::string을 쓰는 것 추천.

jick의 이미지

이왕 C++ stream을 쓰실 거면 C의 문자열보다 C++에서 제공하는 std::string을 쓰는 게 훨씬 간편하고 메모리 관리도 쉽고 좋습니다.

댓글 달기

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