#includestdio.h
云龍網站制作公司哪家好,找創新互聯!從網頁設計、網站建設、微信開發、APP開發、響應式網站設計等網站項目制作,到程序開發,運營維護。創新互聯于2013年成立到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯。
#includestring.h
int main()
{
char s[100];
int i;
FILE *fp;
fp = fopen("1.txt", "r");
printf("**********顯示學生信息************\n");
printf("學號 姓名 性別 年齡 C分數 數學分數 英語分數\n");
while(fscanf(fp, "%s", s) != EOF)
{
for(i = 0; i strlen(s); i++)
if(s[i]!='#') putchar(s[i]);
else printf(" ");
putchar('\n');
}
}
有區別的
if(fd=open("tem.txt",O_RDWR)==-1)
這里的話是先運行open("tem.txt",O_RDWR)==-1這個的,這個的值是0或者1的‘
那么FD的值就不是文件的頭指針了
而下面的是先運行fd=open("tem.txt",O_RDWR
然后再將FD和-1作比較的
1、函數名: write
表頭文件:#includeunistd.h
定義函數:ssize_t write (int fd,const void * buf,size_t count);
函數說明:write()會把指針buf所指的內存寫入count個字節到參數fd所指的文件內。當然,文件讀寫位置也會隨之移動。
返回值:如果順利write()會返回實際寫入的字節數。當有錯誤發生時則返回-1,錯誤代碼存入errno中。
錯誤代碼:
EINTR 此調用被信號所中斷。
EAGAIN 當使用不可阻斷I/O 時(O_NONBLOCK),若無數據可讀取則返回此值。
EBADF 參數fd非有效的文件描述詞,或該文件已關閉。
程序例:
#includestdlib.h
#includeunistd.h
#includestdio.h
#includestring.h
#includefcntl.h
#includeerrno.h
intmain(void)
{
inthandle;
charstring[40];
intlength,res;
/*
Createafilenamed"TEST.$$$"inthecurrentdirectoryandwrite
astringtoit.If"TEST.$$$"alreadyexists,itwillbeoverwritten.
*/
if((handle=open("TEST.$$$",O_WRONLY|O_CREAT|O_TRUNC,
S_IREAD|S_IWRITE))==-1)
{
printf("Erroropeningfile.\n");
exit(1);
}
strcpy(string,"Hello,world!\n");
length=strlen(string);
if((res=write(handle,string,length))!=length)
{
printf("Errorwritingtothefile.\n");
exit(1);
}
printf("Wrote%dbytestothefile.\n",res);
close(handle);
return0;
}
structxfcb{
charxfcb_flag;/*Contains0xfftoindicatexfcb*/
charxfcb_resv[5];/*ReservedforDOS*/
charxfcb_attr;/*Searchattribute*/
structfcbxfcb_fcb;/*Thestandardfcb*/
};
2、函數名: read
表頭文件:#includeunistd.h
定義函數:ssize_t read(int fd,void * buf ,size_t count);
函數說明:read()會把參數fd 所指的文件傳送count個字節到buf指針所指的內存中。若參數count為0,則read為實際讀取到的字節數,如果返回0,表示已到達文件尾或是無可讀取的數據,此外文件讀寫位置會隨讀取到的字節移動。
附加說明:如果順利read()會返回實際讀到的字節數,最好能將返回值與參數count 作比較,若返回的字節數比要求讀取的字節數少,則有可能讀到了文件尾、從管道(pipe)或終端機讀取,或者是read()被信號中斷了讀取動作。當有錯誤發生時則返回-1,錯誤代碼存入errno中,而文件讀寫位置則無法預期。
錯誤代碼:
EINTR 此調用被信號所中斷。
EAGAIN 當使用不可阻斷I/O 時(O_NONBLOCK),若無數據可讀取則返回此值。
EBADF 參數fd 非有效的文件描述詞,或該文件已關閉。
程序例:
#include
#include
#include
#include
#include
#include
int?main(void)
{
void?*buf;
int?handle,?bytes;
buf?=?malloc(10);
/*
Looks?for?a?file?in?the?current?directory?named?TEST.$$$?and?attempts
to?read?10?bytes?from?it.?To
}
if?((bytes?=?read(handle,?buf,?10))?==?-1)?{
printf("Read?Failed.\n");
exit(1);
}
else?{
printf("Read:?%d?bytes?read.\n",?bytes);
}
return?0;
1.糾正:read和write是UNIX或者一些類UNIX系統,比如LINUX系統中使用的,稱為LINUX系統函數。這種函數只能在特定的操作系統下使用,可移植性差。fread和fwrite是C庫函數。這種函數基本在任何操作系統都能使用,可移植性高。2.基礎知識介紹只介紹LINUX系統函數,常用的有creat,open,close,read,write,lseek,access,一般用于文件編程3.如何使用談到如何使用就必須說到另一個知識,文件描述符(file description),是一個非負數。函數原型:int read(int fd, const void *buf, size_t length)功能: 從文件描述符fd所指向的文件中讀取length個字節到buf所指向的緩存區中,返回值為實際讀取的字節數int write(int fd, const void *buf, size_t length)功能: 把length個字節從buf所指向的緩存區中寫到件描述符fd所指向的文件中,返回值為實際寫入的字節數 例子:#define LENGTH 1024#define BUFFES_SIZE 1024int n1, n2;int fd1, fd2;int buffer[BUFFES_SIZE];fd1 = open( "HEllo1.txt", O_RDWR | O_CREAT, O_IRUSE | O_IWUSR);fd2 = open( "HEllo2.txt", O_RDWR | O_CREAT, O_IRUSE | O_IWUSR);n1 = read( fd1, buffer, LENGTH);n2 = write( fd2, buffer, n1); 好了累死了,答案完全原創,希望對你有幫助
1、可以事先檢查一下傳遞給 read() 函數的 fd 是否合法,即在 'if ((nread = read(fd,myBuff2,strlen(myBuff2)))0)' 之前判斷 if ( fd == NULL ) printf("出錯啦!\n");
2、read()函數是文件操作函數,在c語言中很重要。
函數的返回值如下:
(1)如果成功,返回讀取的字節數;
(2)如果出錯,返回-1并設置errno;
(3)如果在調read函數之前已是文件末尾,則返回0
read內部是調_read, _read的返回值在msdn中有這樣的描述
_read returns the number of bytes read, which might be less than count if there are fewer than count bytes left in the file or if the file was opened in text mode, in which case each carriage return–line feed (CR-LF) pair is replaced with a single linefeed character. Only the single linefeed character is counted in the return value. The replacement does not affect the file pointer.
注意這一段: in which case each carriage return–line feed (CR-LF) pair is replaced with a single linefeed character
就是說如果用text模式打開的話, 文件換行時可能在文本中有2個字符----換行和縮進(CR-LF), 而在return的時候系統是把它作為1個回車符號('\n')所返回的. 所以會導致這個情況
標題名稱:c語言read函數代碼 c語言readfile函數
網頁路徑:http://m.kartarina.com/article8/dodecip.html
成都網站建設公司_創新互聯,為您提供網站內鏈、電子商務、自適應網站、企業網站制作、手機網站建設、面包屑導航
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯