c语言防重复运行

//lockfile.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>


int main()
{
    int fd,i;
    char path[]="/home/taoyong/test.txt";
    extern int errno;
    fd=open(path,O_WRONLY|O_CREAT);
    if(fd!=-1)
        {
        printf("open file %s .\n",path);
        printf("please input a number to lock the file.\n");
        scanf("%d",&i);
        if(flock(fd,LOCK_EX)==0)
            {
            printf("the file was locked.\n");
            }
        else
            {
            printf("the file was not locked.\n");
            }
        printf("please input a number to unlock the file.\n");
        scanf("%d",&i);
        if(flock(fd,LOCK_UN)==0)
            {
            printf("the file was unlocked.\n");
            }
        else
            {
            printf("the file was not unlocked.\n");
            }
        close(fd);


        }
    else
        {
        printf("cannot open file %s\n",path);
        printf("errno:%d\n",errno);
        printf("errMsg:%s",strerror(errno));
        }
}


//testprocess.c


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/file.h>


int main()
{
    int fd,i;
    char path[]="/home/taoyong/test.txt";
    char s[]="writing...\nwriting......\n";
    extern int errno;
    fd=open(path,O_WRONLY|O_CREAT|O_APPEND);
    if(fd!=-1)
            {
        printf("open file %s .\n",path);


            if(flock(fd,LOCK_EX|LOCK_NB)==0)
            {
            printf("the file was locked by the process.\n");    
                if(-1!=write(fd,s,sizeof(s)))
                    {
                printf("write %s to the file %s\n",s,path);
                        }
                else
                       {
                printf("cannot write the file %s\n",path);
                printf("errno:%d\n",errno);
                printf("errMsg:%s\n",strerror(errno));
                    }        
                
            }
        else
            {
            printf("the file was locked by other process.Can't write...\n");
                printf("errno:%d:",errno);
            }
        
        close(fd);




            }
        else
           {
        printf("cannot open file %s\n",path);
        printf("errno:%d\n",errno);
        printf("errMsg:%s",strerror(errno));
            }
}
### C语言重复输出问题的解决方案 在C语言编程过程中,可能会遇到因逻辑错误或代码结构不当而导致的重复输出问题。以下是几种常见的原因以及对应的解决方案: #### 1. 循环条件设置不合理 如果循环中的终止条件未正确设定,可能导致程序进入无限循环或者多次执行不必要的迭代。 ```c for (int i = 0; i < limit; i++) { if (condition) { printf("Output\n"); } } ``` 为了避免这种情况,应仔细检查循环变量的变化范围和退出条件[^1]。确保每次迭代都能更新必要的状态,并且只有满足特定条件下才进行输出。 #### 2. 函数调用嵌套过深 当函数内部存在递归或其他形式的多重调用时,如果没有适当控制返回路径也可能引发冗余打印行为。 例如,在解决约瑟夫环问题时,如果不小心让`printf()`语句位于每轮遍历之外而非仅针对目标节点触发,则会造成多余的信息展示[^2]。 修正后的版本如下所示: ```c void joseph(int n, int s, int m){ ... while(p->next != p){ // 修改为直到剩下最后一个元素为止 for(i=0;i<m-1;i++) p=p->next; q=p->next; printf("%d ",q->data); p->next=q->next; free(q); // 添加释放内存操作以泄露 } printf("\nThe survivor is:%d\n",p->data); free(p); } ``` #### 3. 文件读写过程异常 对于涉及I/O流的应用场景来说,比如从标准输入获取字符串后立即显示出来,假如缓冲区尚未刷新完全就再次尝试访问同一位置的数据源的话,同样会出现类似的状况——即连续两次甚至更多次呈现出相同的内容片段。 此时可以考虑采用清空缓存队列的方式加以规避[^4]: ```c fflush(stdin); // 清除键盘输入缓冲区 scanf_s("%s", buffer, sizeof(buffer)); puts(buffer); ``` 另外值得注意的是,在跨平台开发环境下还需关注编码格式的一致性问题以免引起误解译现象发生。 --- ### 总结 综上所述,要有效应对C语言项目里的重复输出难题需做到以下几点:一是精准把控各类控制结构内的流转机制;二是合理规划子模块间的交互关系减少意外干扰因素的影响程度;三是重视实际运行环境特性差异带来的潜在隐患排查工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值