Linux下用C语言将文件内容读入数组

本文最后更新于2017年8月19日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

=Start=

缘由:

学习、总结需要

正文:

参考解答:
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_LINES 32
#define LINE_SIZE 128
char lines[MAX_LINES][LINE_SIZE];
int read_file_to_array() {
    FILE *fp;
    fp = fopen("/tmp/paths.txt""r"); /* path_s = /etc/passwd, /etc/shadow, /etc/hosts, /bin/ps */
    if (fp == NULL) {
        perror("fopen");
        return -1;
    }
    char *line = NULL, *index = NULL;
    char path_str[1024] = {0}, *path_str_p, *token = NULL, delim[] = ", ";
    size_t len = 0;
    ssize_t nread;
    int i = 0;
    while ((nread = getline(&line, &len, fp)) != -1) {
        if (strncmp(line, "path_s"6) == 0) {
            index = strchr(line, '=');
            if (index) {
                line[strcspn(line, "\r\n")] = 0;    // remove trailing newline character
                printf("path_s = '%s'\n", line);
                strncpy(path_str, index+1, nread-7);
                path_str_p = path_str;
                token = strsep(&path_str_p, delim);
                while(token != NULL){
                    if (token[0] != '\0') {
                        printf("\tpath = '%s'\n", token);
                        if (i < MAX_LINES) {
                            strncpy(lines[i], token, LINE_SIZE);
                            i++;
                        }
                    }
                    token = strsep(&path_str_p, delim);
                }
            }
            
            break;
        }
    }
    if(line)
        free(line);
    fclose(fp);
    return i;
}
int main(int argc, char const *argv[])
{
    int i, ret;
    ret = read_file_to_array();
    printf("\npath counts = %d\n", ret);
    for (i = 0; i < ret; ++i) {
        // printf("#%d\n", i);
        if (lines[i][0] != '\0') {
            printf("line[%d] = '%s'\n", i, lines[i]);
        }
    }
    return 0;
}

&

Linux下C语言中如何删除行尾的换行符?
line[strcspn(line, "\r\n")] = 0;    // remove trailing newline character
/*
#include <string.h>
strspn, strcspn - get length of a prefix substring
size_t strspn(const char *s, const char *accept);
    The strspn() function calculates the length (in bytes) of the initial segment of s which consists entirely of bytes in accept. strspn()函数计算在字符串s中包含整个accept字符串的子串长度。
size_t strcspn(const char *s, const char *reject);
    The strcspn() function calculates the length of the initial segment of s which consists entirely of bytes not in reject. strcspn()函数计算在字符串s中不包含reject字符串中字符的子串长度。
*/
参考链接:

=END=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/3549.html

《Linux下用C语言将文件内容读入数组》上有2条评论

  1. C/C++ 二维数组
    http://www.cnblogs.com/xudong-bupt/p/7668061.html

    void print_arr_fun1(int arr[][3], int row){
    for (int i = 0; i < row; ++i){
    for (int j = 0; j < 3; ++j)
    cout << arr[i][j] << " ";
    cout << endl;
    }
    }

    void print_arr_fun2(int *arr, int row, int col){
    for (int i = 0; i < row; ++i){
    for (int j = 0; j < col; ++j)
    cout << *(arr + i * row + j) << " ";
    cout << endl;
    }
    }

    void print_arr_fun3(int **arr, int row, int col){
    for (int i = 0; i < row; ++i){
    for (int j = 0; j < col; ++j)
    cout << arr[i][j] << " ";
    cout << endl;
    }
    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注