1. c語言怎樣修改文件中的結構體數組
write(&cus[i],sizeof(struct client),1,fp) 是什麼意思
答:C語言把一個結構體數組寫入文件分三步: 1、以二進制寫方式(wb)打開文件 2、調用寫入函數fwrite()將結構體數據寫入文件 3、關閉文件指針 相應的,讀文件也要與之匹配: 1、以二進制讀方式(rb)打開文件 2、調用讀文件函數fread()讀取文件中的數據.
2. c語言中如何修改儲存在文件的結構體內容中。小文件
假定一個結構體變數定義如下:
struct Student {...} a;
將結構體保存到文件中:
fwrite(&a,sizeof(struct Student), 1, fp); //從a的地址里讀取1個結構體,向fp文件中寫入讀取到的數據
從文件中讀取結構體數據:
fread(&a,sizeof(struct Student),1,fp); //從fp文件中讀取一個結構體數據,保存到a的地址里。
3. 在c語言中,怎麼修改文件中相關數據信息
三種方法:
(1)將文件中數據讀入內存中,修改後,清空源文件,存入新數據。
(2)以讀寫的方式打開文件,將文件指針移動到要修改的地方,寫入新數據。新數據將會覆蓋掉舊數據。
(3)以讀寫的方式打開文件,將文件指針定位到需要修改數據的末尾,然後刪除需要修改的數據(通過循環n次執行fputc(8,fp),直到清空需要修改的數據為止.
8為退格鍵對應的ascii)。
然後計算需要加入的新數據長度,通過fputc(32,fp)來添加空格到文件中(32為空格鍵的ascii).
然後根據指針位置,填入數據覆蓋掉這些空格。
第一種方法適合數據量較小的情況,
後兩種情況適合數據量較大的情況,但程序寫起來麻煩,如果用心寫代碼,也用不了多久。呵呵。
4. c語言 結構體程序求修改
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#define ID 1
#define ID1 2
#define ID2 3
#define ID3 4
struct human
{
int blood;
int state;//狀態
int rate;
};
int num=0; //回合數
struct human one={400,0,50};
struct human two={400,0,50};
struct human three={400,0,50};
struct human four={400,0,50};
void show(struct human h)
{
printf("第%d回合\n角色%d 體力%d %s 回復率%d%\n",++num,ID,h.blood,h.state?"正常":"中毒",h.rate);
}
void show1(struct human h1)
{
printf("\n角色%d 體力%d %s 回復率%d%%\n",ID1,h1.blood,h1.state?"正常":"中毒",h1.rate);
}
void show2( struct human h2)
{
printf("\n角色%d 體力%d %s 回復率%d%%\n",ID2,h2.blood, (h2.state ? "中毒":"正常"),h2.rate);
}
void show3(struct human h3)
{
printf("\n角色%d 體力%d %s 回復率%d%%\n",ID3,h3.blood,h3.state?"正常":"中毒",h3.rate);
}
int main()
{
char ch;
srand( (unsigned)time( NULL ) );
show(one);
show1(two);
show2(three);
show3(four);
while(ch=getchar())
{
if(ch==10)
{
int r=rand();
if(r>16384)
{
one.state=1;
one.blood=one.blood-r/100;
if(one.blood<=0)
{
one.blood=0;
}
}
else one.state=0;
show(one);
if(r<16384)
{
two.state=1;
two.blood=two.blood-r/130;
if(two.blood<=0)
{
two.blood=0;
}
}
else two.state=0;
show1(two);
if(r>16384)
{
three.state=1;
three.blood=three.blood-r/105;
if(three.blood<=0)
{
three.blood=0;
}
}
else three.state=0;
show2(three);
if(r<16384)
{
four.state=1;
four.blood=four.blood-r/150;
if(four.blood<=0)
{
four.blood=0;
show3(four);
return 0;
}
}
else four.state=0;
show3(four);
}
}
return 0;
}
你的程序我基本上看懂了,上面的代碼能夠正常執行
1.c語言不支持bool類型,所以定義state時,應該定義成int state
2.char ch,應該寫在函數前面,因為c語言不支持變數的隨定義隨引用(即如果要使用一個變數,只需先寫一定義變數的語句即可,並不需要在函數開頭定義)
3.c語言不支持引用類型,引用是c++增加的特性
void show(human &h)
應該寫成void show( struct human h)
4.較低版本的c語言不支持直接使用自定義的結構體類型定義變數,結構體類型前還需要加struct關鍵字 如human one是不行的,必須是struct human one
5. C語言怎麼修改文件信息
#include<stdio.h>
#include<stdlib.h>//文件操作函數頭文件
struct st{//定義一個結構體
char a[11];//301
char b[11];//01
char c[29];//xiaoming
char d[9];//m1
};
int main()
{
FILE *fp;//文件指針
fp=fopen("D:\123.txt","r+");//用fopen函數打開D盤下,名為123.txt的文件;
struct st s={"301","01","xiaoming","m"};//各成員的初值;
scanf("%s%s",s.c,s.d);//輸入之後會把原有的信息覆蓋掉;
fprintf(fp,"%s %s %s",s.a,s.b,s.c,s.d);//用fprintf函數將固定格式的數據寫入文件;
printf("%s %s %s %s",s.a,s.b,s.c,s.d);
fclose(fp);
return 0;
/*這樣就可以修改文件內容了,之前沒仔細看,沒看到是文件操作*/
}
6. C語言編程 結構體 請在源程序中修改,請注釋。
#include<stdio.h>
#include<stdlib.h>//forexit()
#defineMAXLENTH100
typedefstruct{
intID;
floatheight;
}STUDENT;
intgetRecs(STUDENTs[]);
voidsort(STUDENTs[],intn);
voiddisplay(STUDENTdata[],intn);
/*1.從文件獲取學生身高表;2.按身高升序排序後在屏幕上列印身高表。
1.intgetRecs(STUDENTSs[])
功能:從文件records.txt中讀數據到結構體數組s中,並返回人數n。
2.voidsort(STUDENTSs[],intn)
功能:對結構體數組s按身高從低到高排序。
*/
intmain(){
STUDENTdata[MAXLENTH];
intn=getRecs(data);
sort(data,n);
display(data,n);
return0;
}
intgetRecs(STUDENTs[]){
intn=0;
FILE*fp=fopen("records.txt","rt");
if(!fp){
printf("can'topenfile
");
exit(1);
}
while(!feof(fp)){
fscanf(fp,"%d%f",&s[n].ID,&s[n].height);
n++;
}
fclose(fp);
returnn;
}
voidsort(STUDENTs[],intn){
inti,j,k;
STUDENTtemp;
for(i=0;i<n-1;++i){
k=i;
for(j=i+1;j<n;++j)
if(s[k].height>s[j].height)k=j;
if(k!=i){
temp=s[i];
s[i]=s[k];
s[k]=temp;
}
}
}
voiddisplay(STUDENTdata[],intn){
inti;
if(n==0)printf("
順序表是空的!");
else{
for(i=0;i<n;++i)
printf("%5d%.2f
",data[i].ID,data[i].height);
}
}