国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

賀卡制作優(yōu)化資訊

賀卡制作,優(yōu)化資訊,wordpress移動(dòng)端獨(dú)立主題,為什么網(wǎng)站收錄在百度突然沒有了在之前的學(xué)習(xí)中,忘文件中寫的內(nèi)容都是字符串或字符,本節(jié)學(xué)習(xí)如何寫入其他各種類型的數(shù)據(jù)。 回看write和read函數(shù)的形式: ssize_t write(int fd, const void *buf, size_t count); ssize_t read(int fd, void *buf, size_t count); 其中&a…

在之前的學(xué)習(xí)中,忘文件中寫的內(nèi)容都是字符串或字符,本節(jié)學(xué)習(xí)如何寫入其他各種類型的數(shù)據(jù)。

回看write和read函數(shù)的形式:

ssize_t write(int fd, const void *buf, size_t count);
ssize_t read(int fd, void *buf, size_t count);

其中,第二個(gè)參數(shù)都是一個(gè)無類型的指針,只不過之前一直將這里定義為一個(gè)字符串,其實(shí),這個(gè)指針可以指向各種形式數(shù)據(jù)的地址。?

寫入一個(gè)整數(shù)

demo7.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>int main()
{int fd; // file descriptionint data = 100;int data2 = 0;fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);write(fd, &data, sizeof(int));lseek(fd,0,SEEK_SET);read(fd, &data2, sizeof(int));printf("context:%d\n",data2);close(fd); //close after writing return 0;
}

運(yùn)行代碼:

注意,如果此時(shí)打開file1:(此時(shí)需要使用vi打開)

發(fā)現(xiàn)是亂碼,但是這并不影響程序運(yùn)行時(shí)的讀取和寫入

寫入一個(gè)結(jié)構(gòu)體

demo7.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>struct Test
{int i;char c;
};int main()
{int fd; // file descriptionstruct Test data = {30,'k'};struct Test data2;fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);write(fd, &data, sizeof(struct Test));lseek(fd,0,SEEK_SET);read(fd, &data2, sizeof(struct Test));printf("data.i:%d,data.c=%c\n",data2.i,data2.c);close(fd); //close after writing return 0;
}

運(yùn)行代碼:

?

?

?寫入一個(gè)結(jié)構(gòu)體數(shù)組

demo7.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>struct Test
{int i;char c;
};int main()
{int fd; // file descriptionstruct Test data[2] = {{30,'k'},{100,'p'}};struct Test data2[2];fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);write(fd, &data, sizeof(struct Test)*2);lseek(fd,0,SEEK_SET);read(fd, &data2, sizeof(struct Test)*2);printf("data[0].i:%d,data[0].c=%c\n",data2[0].i,data2[0].c);printf("data[1].i:%d,data[1].c=%c\n",data2[1].i,data2[1].c);close(fd); //close after writing return 0;
}

?運(yùn)行代碼:

寫入一個(gè)鏈表?

demo7.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>struct Test
{int data;struct Test *next;
};int main()
{int fd; // file descriptionstruct Test head = {20,NULL};struct Test node1 = {30,NULL};struct Test node2 = {40,NULL};head.next = &node1;node1.next = &node2;fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);write(fd, &head, sizeof(struct Test));write(fd, &node1, sizeof(struct Test));write(fd, &node2, sizeof(struct Test));lseek(fd,0,SEEK_SET);struct Test node_read_1 = {0,NULL};struct Test node_read_2 = {0,NULL};struct Test node_read_3 = {0,NULL};read(fd, &node_read_1, sizeof(struct Test)); read(fd, &node_read_2, sizeof(struct Test));read(fd, &node_read_3, sizeof(struct Test));printf("no.1=%d\n",node_read_1.data);printf("no.2=%d\n",node_read_2.data);printf("no.3=%d\n",node_read_3.data);close(fd);return 0;
}

運(yùn)行代碼:

?

但是以上的代碼有點(diǎn)笨,且容錯(cuò)性太低,首先讀取和寫入應(yīng)該封裝成函數(shù),并且我認(rèn)為通常在讀取鏈表的時(shí)候,不一定知道鏈表有多少元素,所以應(yīng)該一邊用尾插法動(dòng)態(tài)創(chuàng)建鏈表一邊讀取。

改進(jìn)代碼demo8.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>struct Test
{int data;struct Test *next;
};struct Test *insertBehind(struct Test *head, struct Test *new)
{struct Test *p = head;if(p == NULL){head = new;return head;}while(p->next != NULL){p = p->next;} //將p先移動(dòng)到鏈表的尾部p->next = new;return head;
}void writeLink2File(int fd,struct Test *p){while(p != NULL){write(fd, p, sizeof(struct Test));p = p->next;}
}void readLinkFromFile(int fd,struct Test *head){struct Test *new;int size = lseek(fd,0,SEEK_END); //先計(jì)算文件大小lseek(fd,0,SEEK_SET); //然后不要忘記把光標(biāo)移到文件頭while(1){new = (struct Test *)malloc(sizeof(struct Test));read(fd, new, sizeof(struct Test));printf("link data:%d\n",new->data);if(lseek(fd,0,SEEK_CUR) == size){ //每次讀取一個(gè)數(shù)據(jù)之后,動(dòng)態(tài)創(chuàng)建下一個(gè)鏈表節(jié)點(diǎn)之前,都要判斷“當(dāng)前光標(biāo)是否已經(jīng)在文件尾”,如果是,說明讀取已經(jīng)完成printf("read finish\n");break;}head = insertBehind(head,new);}}int main()
{int fd; // file descriptionstruct Test head = {20,NULL};struct Test node1 = {30,NULL};struct Test node2 = {40,NULL};head.next = &node1;node1.next = &node2;fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);writeLink2File(fd,&head);struct Test *head_recv = NULL; //準(zhǔn)備創(chuàng)建一個(gè)新的鏈表用于接收readLinkFromFile(fd,head_recv);close(fd);return 0;
}

改進(jìn)代碼運(yùn)行:

http://aloenet.com.cn/news/47832.html

相關(guān)文章:

  • 榆次建設(shè)局網(wǎng)站普通話手抄報(bào)簡(jiǎn)單又漂亮
  • 個(gè)人網(wǎng)站建站指南他達(dá)那非片能延時(shí)多久
  • php做網(wǎng)站的好處寧波seo外包推廣渠道
  • 購(gòu)物網(wǎng)站圖片的放大怎么做的seo網(wǎng)絡(luò)推廣教程
  • html5網(wǎng)站下載建站模板哪個(gè)好
  • 網(wǎng)絡(luò)營(yíng)銷應(yīng)該這樣做seo優(yōu)化交流
  • 保定企業(yè)網(wǎng)站制作電商數(shù)據(jù)統(tǒng)計(jì)網(wǎng)站
  • 目前流行的網(wǎng)站開發(fā)技術(shù)浙江專業(yè)網(wǎng)站seo
  • 高端網(wǎng)站定制策劃長(zhǎng)沙官網(wǎng)seo技巧
  • 可以做書的網(wǎng)站全國(guó)疫情最新公布
  • 站長(zhǎng)平臺(tái)有哪些交換友情鏈接的方法
  • 談?wù)勀銓?duì)網(wǎng)站建設(shè)有什么樣好的建設(shè)意見做一個(gè)簡(jiǎn)單的網(wǎng)站需要多少錢
  • 一般做網(wǎng)站需要的js有哪些網(wǎng)絡(luò)營(yíng)銷的方法
  • web前端開發(fā)視頻教學(xué)seo排名是什么
  • 真正能約拍的app做好的網(wǎng)站怎么優(yōu)化
  • p2p做網(wǎng)站貴州二級(jí)站seo整站優(yōu)化排名
  • 機(jī)關(guān)事業(yè)單位網(wǎng)站備案谷歌瀏覽器下載
  • ibm網(wǎng)站導(dǎo)航特效代碼信息流廣告投放平臺(tái)
  • 高端做網(wǎng)站哪家好百度一下官網(wǎng)首頁(yè)登錄
  • 臺(tái)州網(wǎng)站建設(shè)優(yōu)化深圳seo推廣
  • 金華專業(yè)做網(wǎng)站建站推廣
  • 互聯(lián)網(wǎng)金融網(wǎng)站設(shè)計(jì)百度收錄查詢工具
  • 模型下載網(wǎng)站開發(fā)流程廣州網(wǎng)頁(yè)制作
  • 網(wǎng)站建設(shè)月總結(jié)怎么做百度關(guān)鍵詞排名
  • 網(wǎng)站首頁(yè)的動(dòng)態(tài)視頻怎么做的公司seo排名優(yōu)化
  • 給網(wǎng)站做插畫分辨率seo也成搜索引擎優(yōu)化
  • 北京網(wǎng)站建設(shè)天下公司網(wǎng)絡(luò)營(yíng)銷品牌
  • 公司怎么建網(wǎng)站做推廣日本疫情最新數(shù)據(jù)
  • 棗莊三合一網(wǎng)站開發(fā)百度安裝應(yīng)用
  • 實(shí)時(shí)視頻網(wǎng)站怎么做網(wǎng)站百度推廣