string 객체를 사용하여 c스타일에 함수에서 사용하려면... ㅜㅜ

kkchlove의 이미지

#include
#include
#include

using namespace std;

int main()
{
string testStr;
scanf("%s", testStr);
cout testStr endl;
//printf("%s\n", testStr.c_str());
return 0;
}

cout 에서 수정하였습니다. 하나씩 뺐습니다.
작동 안되네요...

gamdora의 이미지

소스가 보이지는 않지만 std::string::c_str 메소드를 쓰시면 될 겁니다.

std::string test_str;
test_str = "test string T_T";
printf("%s\n", test_str.c_str());

kkchlove의 이미지

c스타일에 입력 함수를 이용하여 넣으려면요?

gamdora의 이미지

에······.

당장 생각 나는 건 C 형식의 문자열(문자 배열)에 먼저 넣은 다음에

C++ 문자열 객체를 만드는 방법과,

char c_str[C_STR_SIZ];
scanf("%s", c_str);
std::string str_obj;
str_obj = c_str;

C 형식의 입력 함수 대신 C++ 형식의 입력 방법을 쓰는 겁니다.

std::string str_obj;
std::cin >> str_obj;

std::string str_obj;
getline(std::cin, str_obj);

kkchlove의 이미지

소켓 함수에서 데이터를 보내고 받는 작업을 string 객체를 이용해서 하려고요...
소켓 함수는... c스타일에 함수밖에 없잖아요... ㅜㅜ

gamdora의 이미지

그렇군요.

소켓은 안 만져봐서 저도 감이 오질 않네요.

도움이 못 되어서 안타깝네요. T_T

kkchlove의 이미지

string testStr;
testStr.reserve(30);
scanf("%s", &testStr[0]);
cout testStr endl;

메모리의 크기를 정하고 사용하면 되는군요..

kyagrd의 이미지

그런 용도라면 vector<char>를 사용하십시오.
string은 vector처럼 연속된 메모리 배열을 사용해야 한다는 강제사항이 없습니다.

대부분의 구현에서 아마 연속된 메모리 배열을 사용할 거라고 미루어 짐작할 수는 있으나, 보장되는 것이 아닙니다.
만일에 어떤 이유로든 string을 연속된 메모리가 아닌 다른 방식으로 구현한 표준라이브러리를 쓰는 환경에서는 위와 같은 코드는 참조해서는 안될 메모리 영역을 건드릴 수 있으므로 위험합니다.

--
There's nothing so practical as a good theory.
- Kurt Lewin

--
There's nothing so practical as a good theory. - Kurt Lewin
"하스켈로 배우는 프로그래밍" http://pl.pusan.ac.kr/~haskell/

voider의 이미지

그렇습니다.
절대 추천할만한 방법이 아닙니다. 에펙티브 STL 책을 권장합니다.
-- 아쉬운 하루 되세요 --

-- 아쉬운 하루 되세요 --

댓글 달기

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