Chinaunix首页 | 论坛 | 博客
  • 博客访问: 56999
  • 博文数量: 22
  • 博客积分: 975
  • 博客等级: 准尉
  • 技术积分: 222
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-02 21:41
文章分类
文章存档

2011年(8)

2010年(8)

2009年(6)

我的朋友

分类: C/C++

2009-10-30 11:00:02

一个逐行读取配置文件,然后标记nand flash 的block table的小工具.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>


#define GLOB_SBD_IOCTL_MARK_BLOCK _IO(0, 0x770B)
#define GLOB_SBD_IOCTL_MARK_BLOCK_AS_SPARE _IO(0, 0x770C)
#define GLOB_SBD_IOCTL_MARK_BLOCK_AS_DATA _IO(0, 0x770E)

#define BASE    100
#define TAG ","

int main(int argc, char **argv){
    FILE *fp;
    char temp, *p;
    char *bad = "Bad\0";
    char *discarded = "Discarded\n";
    char *free = "Free\n";
    char *data = "Data\n";
    int pbt[9000], i = 100, j, fd, n;

    if (argc != 2) {
        printf("Please input filename\n");
        return -1;
    }

    char * filename = argv[1];
    fp = fopen(filename, "rw");
    if(fp==NULL) {
        printf("Read Error!");
        fclose(fp);
        exit(0);
    }
/*
    fd = open("/dev/nda", O_RDWR);
    if (fd < 0) {
                printf("open error!\n");
                return -1;
    }
*/

    char buf[100];
    while(fgets(buf, 100, fp) != NULL){
                i = 0;
                for (p = strtok(buf, TAG); p; p = strtok(NULL, TAG)) {
                    if(i == 0){                    /* the first part strtok by tag*/
                        i++;
                        sscanf(p, "%d", &n); /*tranvent string p to decimal int n*/
                        if(n < BASE) break;
                    }else{
                        if(strcmp(p, discarded) == 0){
                            if(ioctl(fd, GLOB_SBD_IOCTL_MARK_BLOCK, n))
                                printf("%s\n Mark logical block %d as discared fail.\n ",strerror(errno), n);
                        }
                        if(strcmp(p, free) == 0){
                             if(ioctl(fd, GLOB_SBD_IOCTL_MARK_BLOCK_AS_SPARE, n))
                                 printf("%s\n Mark logical block %d as spare fail.\n ",strerror(errno), n);
                        }
                        if(strcmp(p, data) == 0){
                             if(ioctl(fd, GLOB_SBD_IOCTL_MARK_BLOCK_AS_DATA, n))
                                printf("%s\n Mark logical block %d as used fail.\n ",strerror(errno), n);
                         }
                    }
                 }
    }

  fclose(fp);

}


fgets() -- 读取文件的一行
fgets()
--------------------------------------------
char *fgets(char *s, int size, FILE *stream);
s      是保存读取内容的缓冲区
size   是缓冲区大小
stream 是一个FILE指针

fgets()函数
    读取到它遇到的第一个换行符;
    或者读取到比缓冲区的最大长度少一个字符;
    或者读到文件末尾。

如果fgets()函数在达到缓冲区最大数目之前读完了一整行,它将在字符串的空字符之前添加一个换行符以标识一行结束。

fgets 会自动在读到的一行数据最后加上 换行符 \n, 所以在字符串比较时要注意这个 \n

 

其中还用到了strtok函数来分割字符串,我自己用计数来实现判断分割的哪部分.

阅读(608) | 评论(0) | 转发(0) |
0

上一篇:installing git

下一篇:diff and patch

给主人留下些什么吧!~~