c - 用 C 语言编写具有不同子进程的 WC 模拟器

标签 c

这是我迄今为止为wc模拟器得到的最终结果Linux命令。我必须完成这样的输出

This is child process 0, the number of lines is x 
This is child process 1, the number of words is y
This is child process 2, the number of lines is z

我写的代码是这样的:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

char* concat(char *s1, char *s2)
{
char *result = malloc(strlen(s1)+strlen(s2)+1);
strcpy(result, s1);
strcat(result, s2);
return result;
}

int countLines(FILE *f){
int count = 0;
char ch;
while ((ch = fgetc(f)) != EOF){
   if (ch == '\n')
       count++;
}
return count;
}

int countWords(FILE *f){
int countW = 0;
char ch;
while ((ch = fgetc(f)) != EOF){
   if (ch == ' ')
       countW++;
}
return countW;
}

int countChars(FILE *f){
int chars = 0;
char ch;
while ((ch = fgetc(f)) != EOF){
  chars++;
}
return chars;
}

int main(int argc, char** argv)
{
 int lineCount = 0;
 int wordCount = 0;
 int charCount = 0;
 int n = 3;
 int i,status;
 int pids[3];
 char *theprogram = argv[0];
 char *thefile = argv[1];
 if ( argc !=2 )
 {
     printf( "Help: %s filename\n", argv[0]);
 }
 else{
     FILE *file = fopen( argv[1], "r");

   if(file == 0){
         char *sub = concat(theprogram, ": ");
         char *middle = concat(sub, thefile); 
         perror(middle);
   }
   else{
         for (i = 0; i < n; i++) {
                pids[i] = fork();
            if ( pids[i] < 0) { 
                perror("fork"); 
                exit(-1); 
            } else if (pids[i] == 0) { 
                if (i==0){
                        lineCount += countLines(file);
                        printf("This is child proccess %d, and the number of lines is %d\n", i+1, lineCount);
exit(0);
                    }
                    else if (i==1){ 
                        wordCount += countWords(file);
                        printf("This is child proccess %d, and the number of words is %d\n", i+1, wordCount);
                            exit(0);
                    }
                    else {
                        charCount += countChars(file);
                        printf("This is child proccess %d, and the number of characters is %d\n", i+1, charCount);
                            exit(0);
                    }
            } 
          }
            return 0;      
  }
 } 
} 

这是我得到的实际输出:

% ./a.out mywc.c
This is child proccess 2, and the number of words is 0
This is child proccess 1, and the number of lines is 64
This is child proccess 3, and the number of characters is 0
This is child proccess 3, and the number of characters is 0
This is child proccess 2, and the number of words is 0
This is child proccess 3, and the number of characters is 0
This is child proccess 3, and the number of characters is 0

我在这里做错了什么?我对 C 编程非常陌生,所以不确定我缺少什么。

谢谢。

编辑

退出退出代码,但现在无法获取字数和字符数。

最佳答案

解决我的问题的正确代码在这里:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

char* concat(char *s1, char *s2)
{
char *result = malloc(strlen(s1)+strlen(s2)+1);
strcpy(result, s1);
strcat(result, s2);
return result;
}

int countLines(char *f){
 FILE *file = fopen(f, "r");
int count = 0;
char ch;
while ((ch = fgetc(file)) != EOF){
   if (ch == '\n')
       count++;
}
return count;
}

int countWords(char *f){
char buffer[1];
FILE *file = fopen(f, "r");
int countW = 0;
enum states { WSP, WRD };
int state = WSP;
char last = ' '; 
while (read(fileno(file),buffer,1) == 1 )
{
 if ( buffer[0]== ' ' || buffer[0] == '\t' || buffer[0]=='\n' )
 {
    state = WSP;
 }
 else 
 {
    if ( state == WSP )
    {
       countW++;
    }
    state = WRD;
 }
 last = buffer[0];
}
return countW;
}

int countChars(char *f){
 FILE *file = fopen(f, "r");
int chars = 0;
char ch;
while ((ch = fgetc(file))){
     if (ch == EOF) break;
  chars++;
}
return chars;
}

int main(int argc, char** argv)
{
 int lineCount = 0;
 int wordCount = 0;
 int charCount = 0;
 int n = 3;
 int i,status;
 int pids[3];
 char *theprogram = argv[0];
 char *thefile = argv[1];
 if ( argc !=2 )
 {
     printf( "Help: %s filename\n", argv[0]);
 }
 else{
     FILE *file = fopen( argv[1], "r");

   if(file == 0){
         char *sub = concat(theprogram, ": ");
         char *middle = concat(sub, thefile); 
         perror(middle);
   }
   else{
         for (i = 0; i < n; i++) {
             pids[i] = fork();
             if ( pids[i] < 0) { 
                perror("fork"); 
                exit(-1); 
            } else if (pids[i] == 0) { 
                if (i==0){
                        lineCount = countLines(argv[1]);
                        printf("This is child proccess %d, and the number of lines is %d\n", i+1, lineCount);
                        exit(0);
                    }
                    else if (i==1){ 
                        wordCount = countWords(argv[1]);
                        printf("This is child proccess %d, and the number of words is %d\n", i+1, wordCount);
                        exit(0);
                    }
                    else {
                        charCount += countChars(argv[1]);
                        printf("This is child proccess %d, and the number of characters is %d\n", i+1, charCount);
                        exit(0);
                    }
            } 
          }
            return 0;      
  }
 } 
}

关于c - 用 C 语言编写具有不同子进程的 WC 模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20448216/

相关文章:

c - neighbors.c程序调试(多维数组)

c++ - 处理失败的内存分配

c++ - 我可以从 MSVC 编译的 exe 文件中删除 *.exe.manifest 文件吗?

c - 自然对齐的内存地址

c - 使用系统调用的初学者编程 SO

c++ - 设置声明值时调用函数

c - 如何注册和取消注册中断事件

将一个字符串复制或分配给二维指针数组中的另一个字符串

具有与 FFmpeg 相同功能的音频重采样 C# 库

c - 数组本身的名称,它存储在哪里