當前位置:首頁 » 操作系統 » linux一行一行讀取文件

linux一行一行讀取文件

發布時間: 2025-06-09 13:56:49

linux 命令 read (文件管理)——想玩轉linux就請一直看下去

Linux read命令用於從標准輸入讀取數值。

read 內部命令被用來從標准輸入讀取單行數據。這個命令可以用來讀取鍵盤輸入,當使用重定向的時候,可以讀取文件中的一行數據。

參數說明:

1、簡單讀取

測試結果為:

2、-p 參數,允許在 read 命令行中直接指定一個提示。

測試結果為:

3、-t 參數指定 read 命令等待輸入的秒數,當計時滿時,read命令返回一個非零退出狀態。

執行程序不輸入,等待 5 秒後:

4、棗山高除了輸入時間計時,還可以使用 -n 參數設置 read 命令計數輸入的字元。當輸入的字元數目達到預定數目時,自動退出,並將輸入的數唯衫據賦值給變數。

該例子使用了-n 選項,後接數值 1,指示 read 命令只要接受到一個字元就退出。只要按下一個字元進行回答,read 命令立即接受輸入並將其傳給變數,無需按回車鍵。

只接收 2 個輸入就退出:

執行程序輸入兩個字元:

5、 -s 選項能夠使 read 命令中輸入的數據不顯示在命令終端上(實際上,數據是顯示的,只是 read 命令將文本顏色設置成與背景相同的顏色)。輸入密碼常用這個選項。

執行程序輸入密碼後是不顯示的:

6.讀取凳尺文件

每次調用 read 命令都會讀取文件中的 "一行" 文本。當文件沒有可讀的行時,read 命令將以非零狀態退出。

通過什麼樣的方法將文件中的數據傳給 read 呢?使用 cat 命令並通過管道將結果直接傳送給包含 read 命令的 while 命令。

測試文件 test.txt 內容如下:

測試代碼:

執行結果為:

使用 -e 參數,以下實例輸入字元 a 後按下 Tab 鍵就會輸出相關的文件名(該目錄存在的):

㈡ linux c怎麼實現從文件的最後一行一行向前讀文件

下面的例子使用mmap讀最後20行(假設最後20行不會超過1024位元組)
/*-
* Copyright (C), 1988-2014, mymtom
*
* vi:set ts=4 sw=4:
*/
#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */
/**
* @file last20.c
* @brief
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
char *memchrr(const void *v1, const char *v2, int c)
{
char *s1, *s2;
char *p;
s1 = (char *)v1;
s2 = (char *)v2;
for (p = s2; p >= s1; --p) {
if (*p == c)
return p;
}
return NULL;
}
#define READSIZE 1024
int main(int argc, char *argv[])
{
int ret;
FILE *fp;
char *addr;
size_t len;
int prot;
int flags;
int fd;
off_t off;
off_t rem;
long pagesize;
struct stat buf;
pagesize = sysconf(_SC_PAGESIZE);
fp = fopen("last20.c", "rb");
fd = fileno(fp);
ret = fstat(fd, &buf);
if (buf.st_size <= READSIZE || buf.st_size <= pagesize) {
off = 0;
len = buf.st_size;
} else {
off = buf.st_size - READSIZE;
rem = off % pagesize;
off = off - rem;
len = READSIZE + rem;
}
/*
printf("size=%d READSIZE=%d off=%d len=%d\n",
(int)buf.st_size, (int)READSIZE, (int)off, (int)len);
*/
prot = PROT_READ;
flags = MAP_PRIVATE;
addr = mmap(NULL, len, prot, flags, fd, off);
fclose(fp);
{
int i, n;
char *head, *tail;
size_t size;
char line[1024];
tail = addr + len - 1;
n = 20;
for (i = 0; i < n; ++i) {
head = memchrr(addr, tail - 1, '\n');
if (head == NULL) {
size = tail - addr;
memcpy(line, addr, size);
line[size] = '\0';
} else {
size = tail - head - 1;
memcpy(line, head + 1, size);
line[size] = '\0';
tail = head;
}
printf("%s\n", line);
if (head == NULL) {
break;
}
}
}
munmap(addr, len);
return 0;
}
運行結果為:
./last20 | tac | cat -n
line[size] = '\0';
} else {
size = tail - head - 1;
memcpy(line, head + 1, size);
line[size] = '\0';
tail = head;
}
printf("%s\n", line);
if (head == NULL) {
break;
}
}
}
munmap(addr, len);
return 0;
}

㈢ linux shell讀取文件每一行加入用戶輸入

cat是讀取文件裡面的內容,read讀到的是文本的
用戶輸入,直接 read即可
比如 read passwd,用戶輸入的就可用$passwd調用
你的腳本很亂,記得搞清楚你到底要實現什麼

熱點內容
壓縮式拖把 發布:2025-06-09 17:28:41 瀏覽:495
android經典 發布:2025-06-09 17:22:06 瀏覽:373
外部電腦怎樣連接伺服器 發布:2025-06-09 17:17:19 瀏覽:329
javac編譯亂碼 發布:2025-06-09 17:06:05 瀏覽:912
java32位加密 發布:2025-06-09 17:05:23 瀏覽:809
java下周 發布:2025-06-09 17:05:18 瀏覽:223
python課程設計題目 發布:2025-06-09 17:04:31 瀏覽:843
刷機包如何看安卓系統 發布:2025-06-09 16:49:08 瀏覽:188
鮮板栗怎麼存儲 發布:2025-06-09 16:23:23 瀏覽:497
博弈樹演算法 發布:2025-06-09 16:21:45 瀏覽:224