fprint和fwrite_施乐打印机和惠普打印机哪个好

fprint和fwrite_施乐打印机和惠普打印机哪个好fprintf与fwrite函数用法与差异1 #include<iostream> 2 #include<stdlib.h> 3 using namespace std; 4 5 typedef struct{ 6 int num; 7

fprintf与fwrite函数用法与差异   1 #include<iostream> 2 #include<stdlib.h> 3 using namespace std; 4 5 typedef struct{ 6 int num; 7 char str[20]; 8 double dou; 9 }Data; 10 11 typedef struct node{ 12 Data data; 13 struct node* next; 14 }Node; 15 16 Data* input(); 17 void insert(Node*& head,Data* data); 18 void enterData(Node*& head); 19 void listData(Node* head,void visit(Data* item)); 20 void visit(Data* item); 21 void saveTxt(Node* head); 22 Node* readTxt(); 23 void saveBit(Node* head); 24 Node* readBit(); 25 26 Data* input(){ 27 Data* data = (Data*)calloc(1,sizeof(Data)); 28 cout<<“An Int:”; 29 cin>>data->num; 30 cout<<“a string:”; 31 cin>>data->str; 32 cout<<“a double:”; 33 cin>>data->dou; 34 return data; 35 } 36 37 void insert(Node*& head,Data* data){ 38 if(data == NULL){ 39 cout<<“Error:data == NULL   ”; 40 return; 41 } 42 if(head == NULL){ 43 head = (Node*)calloc(1,sizeof(Node)); 44 head->data = *data; 45 head->next = NULL; 46 }else{ 47 Node* node = (Node*)calloc(1,sizeof(Node)); 48 node->data = *data; 49 node->next = head->next; 50 head->next = node; 51 } 52 } 53 54 void enterData(Node*& head){ 55 char c; 56 do 57 { 58 Data* p = input(); 59 insert(head,p); 60 cout<<“continue?[y/n]:”; 61 cin>>c; 62 } while (c==’y’||c==’Y’); 63 } 64 65 void visit(Data* item){ 66 if(item == NULL){ 67 cout<<“Error(visit):item == NULL”<<endl; 68 } 69 cout<<“Int=”<<item->num<<” str=”<<item->str<<” double=”<<item->dou<<endl; 70 } 71 void listData(Node* head,void visit(Data* item)){ 72 if(head == NULL){ 73 cout<<“Error(listData):head == NULL”<<endl; 74 } 75 Node* p = head; 76 while (p!=NULL) 77 { 78 visit(&(p->data)); 79 p = p->next; 80 } 81 } 82 83 void saveTxt(Node* head){ 84 int inres = 0; 85 FILE* fp = NULL; 86 if(head == NULL){ 87 cout<<“Error(saveTxt):head == NULL”<<endl; 88 return; 89 } 90 fp = fopen(“file.txt”,”w”); 91 if(fp == NULL){ 92 cout<<“Error(fopen):fp == NULL”<<endl; 93 return; 94 } 95 Node* p = head; 96 while (p!=NULL) 97 { 98 inres = fprintf(fp,”%d %s %lf   ”,p->data.num,p->data.str,p->data.dou); 99 cout<<“inres == “<<inres<<endl; 100 p = p->next; 101 } 102 fclose(fp); 103 } 104 105 Node* readTxt(){ 106 FILE* fp = NULL; 107 Node* head = NULL; 108 fp = fopen(“file.txt”,”r”); 109 if(fp == NULL){ 110 cout<<“Error(fopen):fp == NULL”<<endl; 111 return NULL; 112 } 113 while (!feof(fp)) 114 { 115 Data data; 116 int res = fscanf(fp,”%d %s %lf   ”,&data.num,data.str,&data.dou); 117 cout<<“res == “<<res<<endl; 118 if(res == -1){ 119 break; 120 } 121 insert(head,&data); 122 } 123 fclose(fp); 124 return head; 125 } 126 127 void saveBit(Node* head){ 128 FILE* fp = NULL; 129 if(head == NULL){ 130 cout<<“Error(saveBit):head == NULL”<<endl; 131 return; 132 } 133 fp = fopen(“fileBit.txt”,”w”); 134 if(fp == NULL){ 135 cout<<“Error(fopen):fp == NULL”<<endl; 136 return; 137 } 138 Node* p = head; 139 while (p!=NULL) 140 { 141 fwrite(&(p->data),sizeof(Data),1,fp); 142 p = p->next; 143 } 144 fclose(fp); 145 } 146 147 Node* readBit(){ 148 FILE* fp = NULL; 149 Node* head = NULL; 150 fp = fopen(“fileBit.txt”,”r”); 151 if(fp == NULL){ 152 cout<<“Error(fopen):fp == NULL”<<endl; 153 return NULL; 154 } 155 while (!feof(fp)) 156 { 157 Data data; 158 int res = fread(&data,sizeof(Data),1,fp); 159 cout<<“res == “<<res<<endl; 160 if(res == 0){ 161 break; 162 } 163 insert(head,&data); 164 } 165 fclose(fp); 166 return head; 167 } 168 169 int main(){ 170 Node* head = NULL,*headBit = NULL; 171 cout<<“sizeof(Data)==”<<sizeof(Data)<<endl; 172 //enterData(head); 173 //saveTxt(head); 174 head = readTxt(); 175 saveBit(head); 176 cout<<“bit—————   ”; 177 headBit = readBit(); 178 listData(headBit,visit); 179 cout<<“txt—————   ”; 180 listData(head,visit); 181 saveTxt(head); 182 return 0; 183 }

2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/58085.html

(0)
上一篇 2024年 8月 30日
下一篇 2024年 8月 30日

相关推荐

关注微信