C语言将“小票”文档内的(数组)数据读出并进行处理

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main (void){
    int fd,len,gd,i,j,p,q,x,y,temp,u,uname;
    int l=0;
    int z=0;
    int t = 0;
    char str[100];
    char newstr[100];
    char price[100];
    char number[100];
    char name[1024];
    int a,b;
    int c[5] = {0,11,22,36};

    fd = open("a.txt", O_RDWR); //以读写方式打开
    len = read(fd, str, 100); /* 读取文件内容 */
    str[len] = '\0';
    printf("%s\n", str);
    
    gd = open("hello.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    
    
for(temp = 0;temp<4;temp++){
    //将单价赋值给price数组 
    for(i = c[temp];i<len;i++){
        if(str[i] == ',') {
            p = i;
            break;
            }
        }

    for(uname = c[temp];uname<p;uname++){
        name[l] = str[uname];
        l++;
    }
    
    for(u = p+1;u<len;u++){    
        if(str[u] == ',') {
            q = u;
            break;
            }
    }
    for(j = p+1;j<q;j++){
        price[z] = str[j];
        z++;
    }
    //将数量赋值给number数组 
        x = q+1;
        number[t] = str[x];
        a = atoi(price);
        b = atoi(number);
        
        sprintf(newstr,"%s的总价格为:%d \n",name,a*b);
        write(gd, newstr,strlen(newstr));
        
        a=0;b=0;
        memset(price,0, sizeof price);
        z=0;
        memset(name,0, sizeof name);
        l=0;
    }
    close(gd);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值