Linux c编程 文件操作作业

a.txt文件内容如下:
32
45
65
32
67
454
89
54
24
75
3
67
890
32
1

1、编写一个程序读取a.txt文件,将文件内容数字从小到大排序,并将排序结果写入b.txt

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
void sort(int *a, int n)
{
    int i, j, temp;
    for (j = 0; j < n - 1; j++)
    {
        for (i = 0; i < n - 1 - j; i++)
        {
            if (a[i] > a[i + 1]) //数组元素大小按升序排列
            {
                temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
            }
        }
    }
}
int main()
{
    //读取a.txt
    FILE *fpa = NULL;
    fpa = fopen("a.txt", "r");
    if (fpa == NULL)
    {
        printf("fpa open error:%s", strerror(errno));
        return -1;
    }
    int buf[100];
    memset(buf, 0, sizeof(buf));
    char val[100];
    int iLen = 0;
    while (1)
    {
        memset(val, 0, sizeof(val));
        if (fgets(val, sizeof(val), fpa) == NULL)
        {
            break;
        }
        buf[iLen] = atoi(val);
        //printf("buf[%d]=%d\n", iLen, buf[iLen]);
        iLen++;
    }
    fclose(fpa);
    //排序
    sort(buf, iLen);
    //写入b.txt
    FILE *fpb = NULL;
    fpb = fopen("b.txt", "w");
    if (fpb == NULL)
    {
        printf("fpb open error:%s", strerror(errno));
        return -1;
    }
    int loop = 0;
    for (loop = 0; loop < iLen; loop++)
    {
        memset(val, 0, sizeof(val));
        sprintf(val, "%d\n", buf[loop]);
        fputs(val, fpb);
    }
    fclose(fpb);
    return 0;
}

2、通过读取/proc下的相关文件,得到系统CPU信息,内存信息。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 字符串解析*/
/*打开的文件,文件中要查找的字符串,返回的数据地址*/
//char *start;
char *parse_string(char *file, char *string)
{
    static char resultstr[100];
    memset(resultstr, 0, sizeof(resultstr));
    int fd;
    char pbuf[1024*10];
    int nread;
    char *ptmp;
    char *start;
    char *end;
    if (!file || !string)
    {
        return NULL;
    }
    /* 打开文件*/
    fd = open(file, O_RDONLY);
    if (fd < 0)
    {
        printf("open %s file error!\n", file);
        return NULL;
    }
    /*将文件读入缓冲区中*/
    nread = read(fd, pbuf, sizeof(pbuf));//将文件中的内容,存放到pbuf中
    if (nread < 0)
    {
        printf("read %s file error!", file);
        return NULL;
    }
    /* 查找文件中的字符串*/
    ptmp = strstr(pbuf, string);
    ptmp += strlen(string);//跳过查找到的字符串位置
    while((*ptmp == ' ') || (*ptmp == ':') || (*ptmp == '\t'))
        ptmp++;
    start = ptmp;
    end = strchr(ptmp, ' ');//查找到所需的数据
    *end = '\0';
    memcpy(resultstr, start, end-start);
    return resultstr;
}
int main()
{
    printf("%s\n", parse_string("/proc/meminfo", "MemFree"));
    return 0;
}

c.txt文件内容如下
index1 = 45
index2 = 36
index3 = 231
index4 = 43
index5 = 100
index6 = 123
index7 = 51

3、通过读取读取c.txt文件内容中等号右值,并将右值最大值,最小值和平均值打印到屏幕。


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
char *TrimStrR(char *SrcStr) //去掉字符串右面的空格
{
    if (SrcStr == NULL)
        return NULL;
    int iLen = strlen(SrcStr);
    int i;
    for (i = (iLen - 1); i >= 0; i--)
    {
        if (SrcStr[i] == ' ')
            SrcStr[i] = 0;
        else
            break;
    }
    return SrcStr;
}
char *TrimStrL(char *SrcStr) //去掉字符串左面的空格
{
    if (SrcStr == NULL)
        return NULL;
    if (SrcStr[0] != ' ')
        return SrcStr;
    int iLen = strlen(SrcStr);
    if (iLen == 0)
        return SrcStr;
    char *sTmp = (char *) malloc(iLen + 1);
    memset(sTmp, 0, iLen + 1);
    memcpy(sTmp, SrcStr, iLen);
    int i;
    for (i = 0; i < iLen; i++)
    {
        if (SrcStr[i] != ' ')
        {
            strcpy(sTmp, SrcStr + i);
            break;
        }
    }
    strcpy(SrcStr, sTmp);
    free(sTmp);
    return SrcStr;
}
void ParseValueStr(char *DestStr, char *SrcStr) //解析带等号的字符串并去掉字符传尾部空格
{
    int iLen = strlen(SrcStr);
    if (iLen == 0)
        return;
    if ((SrcStr[iLen - 1] == '\n') || (SrcStr[iLen - 1] == '\r'))//为兼容windows格式文本文件,所以解析了\r字符
        SrcStr[iLen - 1] = '\0';
    if (iLen > 1)
    {
        if ((SrcStr[iLen - 2] == '\n') || (SrcStr[iLen - 2] == '\r'))//为兼容windows格式文本文件,所以解析了\r字符
            SrcStr[iLen - 2] = '\0';
    }
    TrimStrR(SrcStr); //去掉尾部空格
    int i;
    for (i = 0; i < iLen; i++)
    {
        if ((*SrcStr) == '=')
        {
            strcpy(DestStr, ++SrcStr);
            break;
        }
        ++SrcStr;
    }
    TrimStrL(DestStr); //去掉首部空格
}
int max(const int *buf, const int bufsize) //计算数组buf中的最大值,参数bufsize为数组buf的元素数量
{
    int tmp = buf[0];
    int i = 0;
    for (; i < bufsize; i++)
    {
        if (tmp <= buf[i])
            tmp = buf[i];
    }
    return tmp;
}
int min(const int *buf, const int bufsize) //计算数组buf中的最小值,参数bufsize为数组buf的元素数量
{
    int tmp = buf[0];
    int i = 0;
    for (; i < bufsize; i++)
    {
        if (tmp >= buf[i])
            tmp = buf[i];
    }
    return tmp;
}
float avg(const int *buf, const int bufsize) //计算数组buf中的平均值,参数bufsize为数组buf的元素数量
{
    float sum = 0;
    int i = 0;
    for (; i < bufsize; i++)
    {
        sum += buf[i];
    }
    return sum / bufsize;
}
int main(int arg, char *args[])
{
    const char *filename = "c.txt";
    FILE *f = fopen(filename, "r");
    if (f == NULL) //文件c.txt打开错误
    {
        printf("open %s failed %s\n", filename, strerror(errno));
        return -1;
    }
    char sValue[100];
    char buf[100];
    int iValue[1024];
    memset(iValue, 0, sizeof(iValue));
    int i = 0;
    while (!(feof(f)))//循环遍历文件中每一行
    {
        memset(sValue, 0, sizeof(sValue));
        memset(buf, 0, sizeof(buf));
        fgets(sValue, sizeof(sValue), f); //从c.txt文件中读取一行
        ParseValueStr(buf, sValue); //解析带等号的字符串并去掉字符传尾部空格
        iValue[i] = atoi(buf);
        i++; //累加器,记载c.txt文件中总行数
    }
    fclose(f);
    printf("最大值为%d\n", max(iValue, i));
    printf("最小值为%d\n", min(iValue, i));
    printf("平均值为%f\n", avg(iValue, i));
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值