[問題] C程式的問題..
題目如下:
Write a program to read a series of names, one per line, from standard input
,and write out those names spelled in reverse order to a file called file1.
Write another program to read the names in from file1, reverse them again,
sort them, and write them out to a file called file2.
第一題我已經做完了..
第一題的程式碼如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *file;
int i=0;
int j=0;
int k=0;
char filename[20];
char name[20];
char name2[20];
printf("Enter the name of the file: ");
gets(filename);
file=fopen(filename,"w");
if(file!=NULL){
printf("Enter several names (Enter EOF to end input):\n");
while(!feof(stdin)){
gets(name);
j=0;
for(i=(strlen(name)-1);i>=0;i--){
name2[j++]=name[i];
}
name2[j]='\0';
fprintf(file,"%s\n",name2);
for(k=0;k<19;k++){
name[k]=0;
}
}
}
fclose(file);
return 0;
}
第一題算是完成了..
但是第二題我不會排序..
程式碼如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *file;
FILE *file2;
char name[20]={0};
char name2[20]={0};
int i=0;
int j=0;
file2=fopen("file2","w");
if((file=fopen("file1","r"))!=NULL){
while(!feof(file)){
fread(&name,sizeof(name),1,file);
if(name!=0){
j=0;
for(i=(strlen(name)-1);i>=0;i--){
name2[j++]=name[i];
}
name2[j]='\0';
fwrite(&name2,sizeof(name2),1,file2);
}
}
}
fclose(file2);
fclose(file);
return 0;
}
請大家幫幫我= =
我的時間快到了..
謝謝大家
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.130.138.118
推
10/01 20:57, , 1F
10/01 20:57, 1F
→
10/01 21:01, , 2F
10/01 21:01, 2F
→
10/01 21:29, , 3F
10/01 21:29, 3F
→
10/01 21:29, , 4F
10/01 21:29, 4F
推
10/01 21:33, , 5F
10/01 21:33, 5F
→
10/01 21:33, , 6F
10/01 21:33, 6F
→
10/01 21:35, , 7F
10/01 21:35, 7F
→
10/01 21:35, , 8F
10/01 21:35, 8F
→
10/01 21:36, , 9F
10/01 21:36, 9F
→
10/01 21:36, , 10F
10/01 21:36, 10F
→
10/01 21:37, , 11F
10/01 21:37, 11F
→
10/01 21:38, , 12F
10/01 21:38, 12F
推
10/01 21:43, , 13F
10/01 21:43, 13F
推
10/01 23:43, , 14F
10/01 23:43, 14F
→
10/01 23:45, , 15F
10/01 23:45, 15F
→
10/02 01:15, , 16F
10/02 01:15, 16F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章