C++ 코드 질문입니다. system 함수 관련.

ryan0802의 이미지

안녕하세요. c++ 생초보입니다.
그냥 입력값 하나를 받아서, 그 입력값을 system 함수에서 실행할때 받아주는 일을 하고 싶습니다.

파이썬 처럼 예를 들자면, "APPLE" 이 입력되면,
char lang;
cin>>lang;
system("sort -d %s_list" % lang);
이런식으로 하고 싶습니다.
%s ... % lang 구조는 제가 파이썬만 알아서 이렇게 표현했습니다. ㅠㅠ
c++ 로 이런식의 입력이 가능한가요?
아래 대강의 코드도 올렸습니다.

#include
#include
#include
using namespace std;

int main()
{
char lang;
string input;

cin >> lang;
system("sort -d %s_grapheme_tri | uniq -c > en_uniq" % lang); // 실행이 안됩니다.

return 0;
}

cinsk의 이미지

+ 연산자를 써서 string을 붙이던지, 아니면 snprintf를 쓰는 것이 좋겠네요.

ryan0802의 이미지

말씀하신걸 제가 잘못 이해했는지 모르겠습니다. ㅠㅠ
아래와 같이 하면, 에러가 납니다.

SOURCE: system("sort -d "+lang+"_grapheme_tri | uniq -c > en_uniq");

ERROR: shell.cpp:18: error: invalid operands of types ‘const char*’ and ‘const char [34]’ to binary ‘operator+’

고수님들 조금만 더 힌트를 주셔요 ㅠㅠ

neogeo의 이미지

operator+ 를 구현해보십시오.

Neogeo - Future is Now.

ryan0802의 이미지

operator + 를 구현한다는게..

system("sort -d "+lang+"_grapheme_tri | uniq -c > en_uniq");

이런식으로 하는거 말고 다른걸 말씀하시는 건가요?

neogeo의 이미지

google 님께 operator+ 로 여쭈어보세요 -ㅅ-;;;

Neogeo - Future is Now.

klyx의 이미지

C++공부부터 하셔야 할것 같은데요...
기본적인 문자열 다루기 (char*가 어렵다면 std::string이라도) 부터 입문서를 찾아보심이...
그냥 당장 요거만 해결하면 된다라면, C++의 방식이 아니라 C의 방식대로 하시는게 오히려 간편할 겁니다.
적당한 버퍼 크기잡고 sprintf로 문자열 작성하면되겠네요.
sprintf사용법은 인터넷에 많이 있고, 이쪽이 파이썬에서 쓰던거랑 생긴건 그래도 비슷할 것입니다.
마지막으로 '문자(char)'와 '문자열(char*혹은 std::string)'은 다른겁니다.

ryan0802의 이미지

char n
cin >> lang;
n=sprintf (buffer, "sort -d %s_grapheme_tri", lang);

sprintf 함수를 찾아서 이런식으로 해봤습니다.
그런데 결국 system 함수에 집어넣는 법을 모르겠네요 ㅠㅠ

klyx의 이미지

buffer에 명령어가 들어가있으니까 그걸 system에 넘겨주면 되지요.
system(buffer); 이렇게요.

ryan0802의 이미지

감사합니다!!!!!
정말 감사합니다. ㅠㅠ

댓글 달기

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