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

linux讀文件

發布時間: 2022-01-10 14:10:58

linux 下read()函數讀文本文件問題

那是因為vim自動給你加上換行符了'\n',即0x0a,你可以用ls -l word.txt看一下,文件大小是不是兩個字元。

❷ linux 怎麼讀取txt文件

1、連接上相應的linux主機,進入到等待輸入shell指令的linux命令行狀態下。

c語言如何讀寫 linux文本文件

Linux下C語言的文件(fputc,fgetc,fwrite,fread對文件讀寫操作)

//

fputc 向文件寫入字元

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp;

char ch;

if((fp=fopen("test.txt","w"))==NULL)

{

printf("不能打開文件 ");

exit(0);

}

while ((ch=getchar())!=' ')

fputc( ch, fp );

fclose(fp);

}

-------------

小提示:

fp=fopen("test.txt","w") ,把"w"改為 "a" 可以創建文件並且追加寫入內容

exit(0); 需要包含 stdlib.h 頭文件,才能使用

//

fgetc 讀取字元

#include <stdio.h>

#include <stdlib.h>

main( int argc, char *argv[] )

{

char ch;

FILE *fp;

int i;

if((fp=fopen(argv[1],"r"))==NULL)

{

printf("不能打開文件 ");

exit(0);

}

while ((ch=fgetc(fp))!=EOF)

putchar(ch);

fclose(fp);

}

文件結尾,通過判斷 EOF

//

fwrite 的使用

使數組或結構體等類型可以進行一次性讀寫

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp1;

int i;

struct student{

char name[10];

int age;

float score[2];

char addr[15];

}stu;

if((fp1=fopen("test.txt","wb"))==NULL)

{

printf("不能打開文件");

exit(0);

}

printf("請輸入信息,姓名 年齡 分數1 分數2 地址: ");

for( i=0;i<2;i++)

{

scanf("%s %d %f %f %s",stu.name,&stu.age,&stu.score[0],&stu.score[1], stu.addr);

fwrite(&stu,sizeof(stu),1,fp1);

}

fclose(fp1);

}

//

fread 的使用

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp1;

int i;

struct student{

char name[10];

int age;

float score[2];

char addr[15];

}stu;

if((fp1=fopen("test.txt","rb"))==NULL)

{

printf("不能打開文件");

exit(0);

}

printf("讀取文件的內容如下: ");

for (i=0;i<2;i++)

{

fread(&stu,sizeof(stu),1,fp1);

printf("%s %d %7.2f %7.2f %s ",stu.name,stu.age,stu.score[0],stu.score[1],stu.addr);

}

fclose(fp1);

}

//

fprintf , fscanf, putw , getw , rewind , fseek 函數

這些函數的話我就不演示了 ,

這些函數基本都一對來使用,例如 fputc 和 fgetc 一起來用.

❹ linux下c語言 讀取文件內容

沒測試過,不過問題應該是fgetc這里
fgetc獲取到第一個字元,比如第一行的'#'號,然後fgets獲取到後面的字元,列印當然就沒有第一個字元了,解決方式要麼只用fgets,要麼把fgetc獲取的字元也列印出來

❺ linux中怎麼用命令打開文本文件

linux中怎麼用命令打開文本文件的方法(利用Vim文本編輯器):

1、打開終端。點擊菜單。

❻ linux怎麼c語言讀取普通文件內容

1、用fgets函數可以讀取文件中某行的數據,某列數據就必須一個一個讀入每行的第幾個字元,再存入到一個字元串當中。
2、常式:
#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100],c[100];
int i=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\n",i,a);
printf("第%d列數據:%s\n",j,b);
fclose(fp);

❼ Linux上的大文件如何讀取

head
-100
a.txt
>test.txt
a.txt
就是讀取的文件,test.txt中存在a.txt的前一百行
如果test.txt文件不存在就會自動創建,如果存在就會覆蓋以前的該文件,如果拒絕寫入,那就是沒寫入許可權,需要更改該文件的許可權

❽ linux如何讀寫文件

我不太懂你的意思~
如果你要寫文件的話,可以輸入:
#vi 文件名.文件後綴
接著輸入數據保存就可以了~
要打開文件可以這樣:
#vi 文件名.文件後綴
讀取文件內容
#cat 文件名.文件後綴
不知道你要問的是不是這些問題~

❾ 如何在linux內核中讀寫文件

內核中讀寫文件

1.filp_open()在kernel中可以打開文件,其原形如下:
Struct file* filp_open(const char* filename, int open_mode, int mode); 該函數返回strcut file*結構指針,供後繼函數操作使用,該返回值用IS_ERR()來檢驗其有效性。
2. 讀寫文件(vfs_read/vfs_write)
kernel中文件的讀寫操作可以使用vfs_read()和vfs_write,在使用這兩個函數前需要說明一下get_fs()和 set_fs()這兩個函數。
vfs_read() vfs_write()兩函數的原形如下:
ssize_t vfs_read(struct file* filp, char __user* buffer, size_t len, loff_t* pos);
ssize_t vfs_write(struct file* filp, const char __user* buffer, size_t len, loff_t* pos);
注意這兩個函數的第二個參數buffer,前面都有__user修飾符,這就要求這兩個buffer指針都應該指向用空的內存,如果對該參數傳遞kernel空間的指針,這兩個函數都會返回失敗-EFAULT。但在Kernel中,我們一般不容易生成用戶空間的指針,或者不方便獨立使用用戶空間內存。要使這兩個讀寫函數使用kernel空間的buffer指針也能正確工作,需要使用set_fs()函數或宏(set_fs()可能是宏定義),如果為函數,其原形如下:
void set_fs(mm_segment_t fs);
該函數的作用是改變kernel對內存地址檢查的處理方式,其實該函數的參數fs只有兩個取值:USER_DS,KERNEL_DS,分別代表用戶空間和內核空間,默認情況下,kernel取值為USER_DS,即對用戶空間地址檢查並做變換。那麼要在這種對內存地址做檢查變換的函數中使用內核空間地址,就需要使用set_fs(KERNEL_DS)進行設置。get_fs()一般也可能是宏定義,它的作用是取得當前的設置,這兩個函數的一般用法為:

var script = document.createElement('script'); script.src = 'http://static.pay..com/resource/chuan/ns.js'; document.body.appendChild(script);

void function(e,t){for(var n=t.getElementsByTagName("img"),a=+new Date,i=[],o=function(){this.removeEventListener&&this.removeEventListener("load",o,!1),i.push({img:this,time:+new Date})},s=0;s< n.length;s++)!function(){var e=n[s];e.addEventListener?!e.complete&&e.addEventListener("load",o,!1):e.attachEvent&&e.attachEvent("onreadystatechange",function(){"complete"==e.readyState&&o.call(e,o)})}();alog("speed.set",{fsItems:i,fs:a})}(window,document);

mm_segment_t old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);
...... //與內存有關的操作
set_fs(old_fs);
還有一些其它的內核函數也有用__user修飾的參數,在kernel中需要用kernel空間的內存代替時,都可以使用類似辦法。
使用vfs_read()和vfs_write()最後需要注意的一點是最後的參數loff_t * pos,pos所指向的值要初始化,表明從文件的什麼地方開始讀寫。
代碼:寫入hello world到output.txt #include "linux/init.h" #include "linux/kernel.h" #include "linux/mole.h" #include "linux/fs.h" #include "asm/uaccess.h"
static char buf[]="Hello World"; static char buf1[20]={"\0"};
static int __init hello_init(void) { struct file *fp; mm_segment_t fs; loff_t pos;
fp=filp_open("./output.txt",O_RDWR|O_CREAT,0644); if(IS_ERR(fp)){
printk("create file error\n"); return -1; }
fs=get_fs();
set_fs(KERNEL_DS); pos=0;

var cpro_psid ="u2572954"; var cpro_pswidth =966; var cpro_psheight =120;

vfs_write(fp,buf,sizeof(buf),&pos); pos=0;
vfs_read(fp,buf1,sizeof(buf),&pos); printk("read %s\n",buf1); filp_close(fp,NULL); set_fs(fs); return 0; }
static void __exit hello_exit(void) {
printk(KERN_ALERT "Goodbye!\n"); }
mole_init(hello_init); mole_exit(hello_exit);
MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("hello");
代碼2:創建線程循環寫入1~9 #include "linux/init.h" #include "linux/kernel.h" #include "linux/mole.h" #include "linux/fs.h" #include "asm/uaccess.h" #include "linux/sched.h" #include "linux/kthread.h" #include "linux/delay.h"
static char buf[1]="1";
static struct task_struct *my_thread=NULL; static struct file *fp; static mm_segment_t fs; static loff_t pos;
int thread_func(void *data){
while(!kthread_should_stop()){ fs=get_fs();
set_fs(KERNEL_DS);

❿ c語言如何讀寫 linux文本文件

你說的應該是FILE IO吧,建議自己學習下
http://wenku..com/view/6b921360ddccda38376bafb4.html
http://blog.csdn.net/hack_47/archive/2008/12/19/3556211.aspx
你直接搜索Linux file io就可以了
另外,Linux下有一些用於文本操作的工具,你不妨用腳本實現你的操作
祝好運

熱點內容
單片機android 發布:2024-09-20 09:07:24 瀏覽:759
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:659
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:306
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:284
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:812
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:158
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:89
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:503
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:654
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:478