=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字符串中字符的子串长度。 */ |
- https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221
- http://man7.org/linux/man-pages/man3/strcspn.3.html
参考链接:
- https://stackoverflow.com/questions/19173442/reading-each-line-of-file-into-array #malloc+realloc
- https://stackoverflow.com/questions/4237084/reading-from-a-file-and-storing-in-array #事先声明一个二维字符数组用于存放
- https://stackoverflow.com/questions/14536284/c-read-text-file-into-array-cant-see-whats-wrong
- https://stackoverflow.com/questions/19619343/file-input-into-array-by-line-as-string
- =
- https://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c #将文件内容读入一个字符串
=END=
《 “Linux下用C语言将文件内容读入数组” 》 有 2 条评论
linux c去除行首、行尾空格和读取配置文件中的键值对中的值 #trim()
http://blog.csdn.net/sondx/article/details/7245649
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;
}
}
`