導航:首頁 > 操作系統 > linux文件讀一行

linux文件讀一行

發布時間:2023-06-06 19:01:39

linux findfile 怎麼讀取第一行

我把模型寫出來你自己添加點,還是比較簡單的,這里最後找到的文件就是你要的答案 CFileFind f; bool b=f.FindFile(D:\test\\*.*"); while(b){ b=f.FindNextFile();//讀取下一個文件 if(f.GetFilePath()==str){ } }

② Linux C語言怎麼讀取文件指定行內容

1、用fgets函數可以讀取文件中某行的數據,某列數據就必須一個一個讀入每行的第幾個字元,再存入到一個字元串當中。

2、常式:

#include<stdio.h>
#include<string.h>
voidmain()
{
chara[100],b[100],c[100];
inti=3,j=4,k=0;//第三行,第四列
FILE*fp=fopen("data.txt","r");
while(fgets(c,100,fp)){//讀入每行數據
i--;
if(i==0)strcpy(a,c);//讀到第三行數據
b[k++]=c[j-1];//把每行的那列字元拷到b中
}
b[k]=0;
printf("第%d行數據:%s ",i,a);
printf("第%d列數據:%s ",j,b);
fclose(fp);
}

③ 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文件讀一行相關的資料

熱點內容
新建文件夾1女演員三位 瀏覽:740
不用下載就能看的視頻網站 瀏覽:330
我一個神偷硬生生把國家偷成強國 瀏覽:600
樣子是五歲小男孩和郭富城演的 瀏覽:460
韓國演員也美娜 瀏覽:898
陸離是哪部小說的主角 瀏覽:49
華娛開局佟麗婭 瀏覽:17
男男生子小說現代攻姓章 瀏覽:541
永旺星星影院影訊 瀏覽:328
李彩潭巔峰之作 瀏覽:86
彎村紅羊電影 瀏覽:157
我和我的家教老師韓國 瀏覽:102
日本經典高分電影 瀏覽:627
動物真人版電影鳳凰定製 瀏覽:360
海客雲伺服器一個月怎麼算的 瀏覽:161
黑道小說主角外號瘋子 瀏覽:309
書包cc網電子書txt免費下載 瀏覽:354
帶一點黃的小說 瀏覽:257
法國倫理電影小僕人 瀏覽:187
印度搶打火機的電影叫什麼 瀏覽:291