写作和叉子之内阅读 - Ç [英] writing and reading within a fork - C

查看:169
本文介绍了写作和叉子之内阅读 - Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这次演习是简单的。父亲的过程让我在文件中写我的名字和姓氏。子处理等待5秒钟,然后读取并显示它。我不能使用 #WAIT()功能。

The exercise is simple. The father process lets me write my name and surname in a file. The son process waits 5 seconds, then reads and displays it. I can't use the #wait() function.

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

int main(int argc, char** argv) {
    char name[20];
    char surname[20];
    char outp[50];

    // The file pointer   
    FILE *file;
    // Open the file in write mode
    file = fopen("dati.txt", "w+");

    pid_t pid;
    pid=fork();
    if(pid>0){
        printf("Father");
        printf("Insert name: ");
        scanf("%s",name);

        for (int i=0; i<strlen(name); i++) {
            if (i==0) {
                if (name[i]>='a' && name[i]<='z')
                    name[i]-=32;
            } else {
                if (name[i]>='A' && name[i]<='Z')
                    name[i]+=32;
            }
        }

        printf("Insert surname: ");
        scanf("%s", surname); 

        for (int i=0; i<strlen(name); i++){
            if (i==0){
                if (surname[i]>='a' && surname[i]<='z')
                    surname[i]-=32;
            } else {
                if (surname[i]>='A' && surname[i]<='Z')
                    surname[i]+=32;
            }
        }
        fputs(name, file);
        fputs(" ",file);
        fputs(surname, file);
        printf("Father exits");
        fclose(file);
        exit(0);
    }else{
        sleep(5);
        // position to the start of the file
        rewind(file);    
        fgets(outp, 50, file);
        printf("Read string: %s", outp);        
        fclose(file);
        exit(0);
    }
    return 0;
}

如果我在第5秒前插入姓名,程序写他们,但不显示它们。否则,如果我写的名字和不小心等待5日第二次,它会显示文件内容(这基本上是随机字符)。

If I insert name and surname before the 5th second, the program writes them, but doesn't display them. Otherwise, if I write the name and "accidentally" wait for the 5th second, it displays the file content (which are basically random characters).

推荐答案

您正在关闭该文件中的主要过程,那么你要重用子进程相同的指针。这不可能。您需要重新打开子进程的文件。

You are closing the file in the main process, then you want to reuse the same pointer in the child process. This is not possible. You need to reopen the file in the child process.

所以,你需要在后写这个睡眠()电话:

file = fopen("dati.txt", "r");

您也不会需要 #rewind()函数调用,因为你会回来的文件的开头。

You also won't need the #rewind() function call, as you will be back at the start of the file.

更新:的问题是, W + 办法

写入/更新时间:创建一个空文件并打开它进行更新

write/update: Create an empty file and open it for update

所以,你的文件被删除。随着研究模式下,它工作得很好。

So, you the file was erased. With r mode, it works just fine.

PS:顺便说一句,你不应该在C程序中使用 cstdio ,只使用标准 stdio.h中头文件。还有一个编译错误,因为你重新声明循环变量 I

PS: By the way, you shouldn't use cstdio in a C program, use only the standard stdio.h header file. There was also a compilation error, because you are redeclaring the loop variable i.

这篇关于写作和叉子之内阅读 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆