c++ wc 구현 질문이요.

jamesc1236의 이미지

제가 지금 리눅스 wc -l, -m, -w, -c 를 구현하려고 하는데 -l -m -w 는 문제 없이 했는데 -c 에서 애를 먹고있네요.
-c 가 파일의 byte 사이즈를 프린트 하는데 저는 줄곧 -m 랑 똑같이 생각하고 코딩하다보니 만약 언어가 틀린 파일이면 사이즈도 틀려진단걸 알고 멘붕이 왔네요.
파일 사이즈 구하는건
stat(argv[i], &sb);
printf("%lld", (long long) sb.st_size); <- 이걸 사용해서 쉽게 구하는 법을 알겠는데 문제는 사이즈 구하는 방법을 int wc()에서 해결하고 싶은데
argv[] 는 메인에서만 쓸수있고.. 잘 모르겠네요.. 조언 좀 부탁합니다..

int main(int argc, char *argv[])
{
 
    int i;
    int total_ch, total_word, total_line;
 
    total_ch = total_word = total_line = 0;
 
  int j;    
  j = (argc > 1) ? 1:0;
  int opt;
  bool c_On = false, l_On=false, m_On = false, w_On = false;
 
 
 
 
  for (int i = j; i < argc;i++){
    while ((opt = getopt(argc, argv, "clmw")) != -1) {
               switch (opt) {
               case 'c':
                   c_On=true;
                   break;
               case 'l':
                   l_On = true;
                   break;
	       case 'm':
                   m_On = true;
                   break;
	       case 'w':
                   w_On = true;
                   break;
               default:
			printf("Try `wc --help' for more information.\n");
                 	return EXIT_SUCCESS;
               }
     }
 
   }
if(c_On == false && l_On == false && m_On == false && w_On == false){
	if( argc == 1 ) {
       	 	wc(argv[0], NULL, &total_ch, &total_word, &total_line);
    	}
 
	else {
        	for( i=1 ; i<argc ; i++ ) {
         		wc(argv[0], argv[i], &total_ch, &total_word, &total_line);
        	}
    	}
 
    	if( argc > 2 ) {
        	printf("%7d %8d %8d total\n", total_line, total_word, total_ch);
   	}
}
 
}
int wc(char *cmdname, char *filename, int *total_ch, int *total_word, int *total_line)
{
    int flag;
    struct stat st;
    FILE *fp;
    char ch;
    int word_flag;
    int num_ch, num_word, num_line;
 
    if( filename == NULL ) {
        fp = stdin;
    }
    else {
        flag = lstat(filename, &st);
        if( flag == -1 ) {
            printf( "%s: %s: No such file or directory\n", cmdname, filename);
            return 0;
        }
 
        if( S_ISREG(st.st_mode) ) {
            fp = fopen(filename,  "r");
            if( fp == NULL ) {
                printf("%s: %s: Permission denied\n", cmdname, filename);
                return 0;
            }
        }
        else if( S_ISDIR(st.st_mode) ) {
            printf("%s: %s: Is a directory\n", cmdname, filename);
             return 0;
        }
        else {
            return 0;
        }
    }
 
    num_ch = num_word = num_line = 0;
    word_flag = 0;
    while( (ch = getc(fp)) != EOF ) {
        ++num_ch;
        if( isspace(ch) ) {
            if( word_flag == 1 ) {
                word_flag = 0;
                ++num_word;
            }
        }
        else {
            word_flag = 1;
        }
        if( ch ==  '\n' )
            ++num_line;
    }
 
    fclose(fp);
    printf("%7d %8d %8d %s\n", num_line, num_word, num_ch, filename);
 
    *total_ch += num_ch;
    *total_word += num_word;
    *total_line += num_line;
    return 0;
}
jamesc1236의 이미지

찾았습니다..

댓글 달기

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