⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read.c

📁 linux中演示如何产生子进程
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <error.h>
#include <sys/types.h>

#define BUFFER_LEN	256

int main(int argc, char **argv)
{
    printf("In child process...\n");
    
    //对参数进行校验
    if(argc < 2)
    {
        printf("Child process start failure!\n");
        printf("\n\tformat:./read inputfile\n");
        printf("\n\texample:./read hello.txt\n");
        return EXIT_FAILURE;
    }
    
    //打开指定的文件,并从中读取数据
    char szErrorText[BUFFER_LEN] = {0};
    FILE *pFile = fopen(argv[1],"r");
    if(!pFile)
    {
        snprintf(szErrorText,BUFFER_LEN,"Child process open inputfile \"%s\" failure.",argv[1]);
        perror(szErrorText);
    }
    else
    {   
        char szDataBuffer[BUFFER_LEN] = {0};
        int iReadBytes = fread(szDataBuffer,BUFFER_LEN,1,pFile);
        if(iReadBytes == EOF)
        {
            snprintf(szErrorText,BUFFER_LEN,"Child process read data from \"%s\"",argv[1]);
            perror(szErrorText);
        }
        else
        {
            printf("Child process read data from \"%s\" success, data is:\"%s\", data length:%d.\n",argv[1],szDataBuffer,strlen(szDataBuffer));
        }

        fclose(pFile);
        pFile = NULL;
    }

    printf("Child process exit...\n");
    return EXIT_SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -