메인함수 작성시 argv에 대해서질문입니다...^^

오네테르의 이미지

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

  main(int argc, char **argv)
  {
      char *foundString;
      char *outputString = "";

      size_t outputStringSize;

      foundString = strtok(argv[1], "|");

      while(1)
      {
          if(strcmp(foundString, argv[2]) == 0)
          {
              foundString = strtok(NULL, "|");
              continue;
          }

          strcat(outputString, foundString);
          foundString = strtok(NULL, "|");

          if(foundString == NULL)
              break;
          else
              strcat(outputString, "|");
      }

      outputStringSize = strlen(outputString);

      if( outputString[outputStringSize-1] == '|')
      {
          outputString[outputStringSize-1] = '\0';
      }

      printf("%s", outputString);

  }

이 프로그램은 main에서 2개의 argument를 입력받습니다.
하나는 "|"를 구분자로 하는 전체스트링이고
다른하나는 삭제하고싶은 스트링으로
전체 스트링에서 해당스트링을 삭제한 결과를
화면에 출력해 줍니다.

Quote:
$>rmstring "a|b|c|d|e" c
$>a|b|d|e

문제는 전체스트링이 a|b|c|d|e|f|g|h|j
까지는 이상이 없지만 그 이상으로 넘어가면
a|b|c|d|e|f|g|h|j|l|m....
이 되면 앞부분이 잘려서 출력되더군요

Quote:
$>rmstring "a|b|c|d|e|f|g|h|i|j|K|L|m|o|p|q" c
$>m|n|o|p|q

같이 말이죠..
짐작에는 아마 argument 사이즈가 정해져 있어서
그런것 같지만...정확한 원인은 잘 모르겠네요...
도움좀 부탁드립니다...^^
lovewar의 이미지

오네테르 wrote:
  #include <stdio.h>
  #include <string.h>

  main(int argc, char **argv)
  {
      char *foundString;
      char *outputString = "";  /* 메모리 미할당 - "" 상수이기에 값 수정은 정의되지 않는 행위를 유발합니다. */ 

메모리 미할당으로 인한 버그로 추정됩니다.
이럴경우 segment fault를 유발할 소지가 많습니다.

오네테르의 이미지

아하...
malloc으로 메모리 잡아주고
outputString=""
이부분 삭제해주니
제대로 나오는군요...

답변 감사합니다..^^

Want 2 be A good Programmer

doldori의 이미지

while loop의 내용이 좀 복잡해 보입니다. 가독성이 좋도록 고쳐봅니다.

foundString = strtok(argv[1], "|"); 

while (foundString)
{ 
    if (strcmp(foundString, argv[2])) 
    {
        strcat(outputString, foundString); 
        strcat(outputString, "|");
    }
    foundString = strtok(NULL, "|"); 
}

댓글 달기

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