给你写了一个,累死了,给我加分啊~~
程序运行完之后txt中的内容会变成三段,如果想要再运行一遍,需要将txt内容还原成最初的,否则会出错。
#include
#include
typedef struct
{
char nation[128];
int gold;
int silver;
int bronze;
int sum;
}medal;
void main()
{
medal table[20];
medal tmp;
char buf[256];
char title[256];
FILE *fp;
int table_len = 0,i,j;
fp = fopen("text.txt","r+");
if(fp == NULL)
{
printf("read error!\n");
return;
}
fgets(title,256,fp);
fputs(title,stdout);
while(fgets(buf, 256, fp) != NULL)
{
sscanf(buf, "%[^1-9]%d%d%d%d",table[table_len].nation,&table[table_len].gold, &table[table_len].silver, \
&table[table_len].bronze,&table[table_len].sum);
printf("%s%-6d%-8d%-8d%d\n",table[table_len].nation,table[table_len].gold,table[table_len].silver, \
table[table_len].bronze,table[table_len].sum);
table_len++;
}
printf("\nOrder by country name:\n");
fprintf(fp, "\nOrder by country name:\n");
fputs(title,stdout);
fputs(title,fp);
for(j=0; j<(table_len-1); j++)
{
for(i=0; i<(table_len-1-j);i++)
{
if(strcmp(table[i].nation, table[i+1].nation) > 0)
{
tmp = table[i];
table[i] = table[i+1];
table[i+1] = tmp;
}
}
}
for(i=0;i
printf("%s%-6d%-8d%-8d%d\n",table[i].nation,table[i].gold,table[i].silver, \
table[i].bronze,table[i].sum);
fprintf(fp,"%s%-6d%-8d%-8d%d\n",table[i].nation,table[i].gold,table[i].silver, \
table[i].bronze,table[i].sum);
}
printf("\nOrder by number of medals:\n");
fprintf(fp, "\nOrder by number of medals:\n");
fputs(title,stdout);
fputs(title,fp);
for(j=0; j<(table_len-1); j++)
{
for(i=0; i<(table_len-1-j);i++)
{
if(table[i].sum > table[i+1].sum)
{
tmp = table[i];
table[i] = table[i+1];
table[i+1] = tmp;
}
}
}
for(i=0;i
printf("%s%-6d%-8d%-8d%d\n",table[i].nation,table[i].gold,table[i].silver, \
table[i].bronze,table[i].sum);
fprintf(fp,"%s%-6d%-8d%-8d%d\n",table[i].nation,table[i].gold,table[i].silver, \
table[i].bronze,table[i].sum);
}
fclose(fp);
}
#include
#include
//假设要打开的文件内容全是整数(如果是浮点数参照修改)
//数据与数据之间用空格分开
//每行多少数据可以不相同
//数组从小到大排序
void sortit(int *a,int n) {
int i,j,m;
for(i=0;ifor(j=i+1;j if(a[i]>a[j]){
m=a[i];a[i]=a[j];a[j]=m;
}
}
//输出数组元素
void printit(int *a,int n){
int i;
for(i=0;iprintf("%5d",a[i]);
if (i%10==9) printf("\n"); //设置每10个换行
}
}
//从文件fp中读取n个数据到a
void getdata(FILE *fp,int *a,int n){
int i;
fseek(fp,0,0);
for(i=0;ifscanf(fp,"%d",&a[i]);
}
//获取文件中整数数椐的个数
int getdatabound(FILE *fp){
int i=0,n;
fseek(fp,0,0);
while(fscanf(fp,"%d",&n)!=EOF) i++;
return i;
}
int main(){
int n,*a;
char *myfile="d:\\data.txt";
FILE *fp;
if ((fp=fopen(myfile,"r"))==NULL){
printf("打开文件%s失败\n",myfile);
return 0;
}
n=getdatabound(fp); //获取该文件中有多少整数
printf("该文件共有%d个整数\n",n);
a=(int *)malloc(sizeof(int)*n); //分配n个整数的空间
getdata(fp,a,n); //从文件fp中读取n个数据到a
fclose(fp);
printf("排序前的数组\n");
printit(a,n); //输出数组的值
printf("\n");
sortit(a,n);
printf("排序后的数组\n");
printit(a,n);
printf("\n");
}
下面是可以实现1的代码。
#include
#include
#include
char buf[439];
int read_txt( FILE *name );
main()
{
int res;
FILE *file;
file = fopen ( "C:\\Users\\yyy\\Desktop\\111.txt", "r" );
res = read_txt( file );
if (res == 0)
printf("sucessful !\n");
else
printf("error !\n");
system( "pause" );
return 0;
}
int read_txt( FILE *name )
{
if ( name != NULL )
fread(buf, 1024, 1, name);
printf("%s\n", buf);
return 0;
}
d