经典c代码集合

0 linux常用命令

du -h u-boot.bin    查看文件大小

 

 

1 解析命令行输入指令

 /* parse command line arguments */
    argv += 2;
    while (*argv) {
        if (strcmp(*argv, "-d") == 0) {
            argv++;
            if (*argv)
                device = atoi(*argv);
        }
        if (strcmp(*argv, "-p") == 0) {
            argv++;
            if (*argv)
                period_size = atoi(*argv);
        }
        if (strcmp(*argv, "-n") == 0) {
            argv++;
            if (*argv)
                period_count = atoi(*argv);
        }
        if (strcmp(*argv, "-D") == 0) {
            argv++;
            if (*argv)
                card = atoi(*argv);
        }
        if (strcmp(*argv, "-m") == 0) {
            argv++;
            if (*argv)
                loop_minutes = atoi(*argv);
        }
        if (strcmp(*argv, "-i") == 0) {
            argv++;
            if (*argv)
                loop_num = atoi(*argv);
        }
        if (*argv)
            argv++;
    }

 

 

2 关于文件的操作字节读取的操作

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>

#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
#define ID_FMT  0x20746d66
#define ID_DATA 0x61746164

struct riff_wave_header {
    uint32_t riff_id;
    uint32_t riff_sz;
    uint32_t wave_id;
};

struct chunk_header {
    uint32_t id;
    uint32_t sz;
};

struct chunk_fmt {
    uint16_t audio_format;
    uint16_t num_channels;
    uint32_t sample_rate;
    uint32_t byte_rate;
    uint16_t block_align;
    uint16_t bits_per_sample;
};




FILE *file;
char *filename;
int more_chunks = 1;
struct riff_wave_header riff_wave_header;
struct chunk_header chunk_header;
struct chunk_fmt chunk_fmt;

filename = argv[1];
file = fopen(filename, "rb");
if (!file) {
    fprintf(stderr, "Unable to open file '%s'\n", filename);
    return 1;
}

fread(&riff_wave_header, sizeof(riff_wave_header), 1, file);
if ((riff_wave_header.riff_id != ID_RIFF) ||
    (riff_wave_header.wave_id != ID_WAVE)) {
    fprintf(stderr, "Error: '%s' is not a riff/wave file\n", filename);
    fclose(file);
    return 1;
}

do {
    fread(&chunk_header, sizeof(chunk_header), 1, file);

    switch (chunk_header.id) {
    case ID_FMT:
        fread(&chunk_fmt, sizeof(chunk_fmt), 1, file);
        /* If the format header is larger, skip the rest */
        if (chunk_header.sz > sizeof(chunk_fmt))
            fseek(file, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR);
        break;
    case ID_DATA:
        /* Stop looking for chunks */
        more_chunks = 0;
        break;
    default:
        /* Unknown chunk, skip bytes */
        fseek(file, chunk_header.sz, SEEK_CUR);
    }
} while (more_chunks);

fclose(file);

 

 3 sprintf的使用

#define PCM_OUT        0x00000000
#define PCM_IN         0x10000000    

char fn[256];

snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,flags & PCM_IN ? 'c' : 'p');

 

4 calloc 申请空间并清空 比malloc好用一点点

params = calloc(1, sizeof(struct snd_pcm_hw_params));

if (params)
        free(params);

 

5 sizeof使用

uint8_t   val8;   // 长度1
uint32_t  val32;  // 长度4
uint8_t*  ptr8;   // 长度4
uint32_t* ptr32;  // 长度4
uint8_t   arr8[] = {1,2,3,4,5,6}; // 长度6
uint32_t  arr32[] = {11,12,13,14,15,16}; // 长度24

 

6 fseek使用 ftell使用

 

 

 

 

 

 

 

 

//

posted @ 2022-09-19 10:11  小城熊儿  阅读(182)  评论(0编辑  收藏  举报