eak; 案例3: printf(“待替换字符:”); scanf(“%c”、&d); printf(“一个新字符:”); scanf(“%c”和“&n”); 更新文件(d,n); 打破 案例4: deletefile(); 打破 } getchar();
eak; 案例3: printf(“待替换字符:”); scanf(“%c”、&d); printf(“一个新字符:”); scanf(“%c”和“&n”); 更新文件(d,n); 打破 案例4: deletefile(); 打破 } getchar(); 返回0; }eak; 案例3: printf(“待替换字符:”); scanf(“%c”、&d); printf(“一个新字符:”); scanf(“%c”和“&n”); 更新文件(d,n); 打破 案例4: deletefile(); 打破 } getchar();,c,file,sorting,C,File,Sorting,eak; 案例3: printf(“待替换字符:”); scanf(“%c”、&d); printf(“一个新字符:”); scanf(“%c”和“&n”); 更新文件(d,n); 打破 案例4: deletefile(); 打破 } getchar(); 返回0; } 一致的格式是一个很好的起点。你也应该阅读。并学习如何使用调试器逐步完成代码。 #include<stdio.h> #include<stdlib.h> char id[20], year[20], ti
一致的格式是一个很好的起点。你也应该阅读。并学习如何使用调试器逐步完成代码。
#include<stdio.h>
#include<stdlib.h>
char id[20], year[20], title[20], name[20];
void write(char id[], char title[], char name[], char year[])
{
FILE *fp;
fp = fopen("book.txt", "r");
if(fp == NULL)
{
fp = fopen("book.txt", "w");
fprintf(fp, "ID TITLE NAME YEAR\n");
fprintf(fp, "%s\t\t%s\t\t%s\t\t%s\t\t", id, title, name, year);
}
else
{
fp = fopen("book.txt.", "a");
fprintf(fp, "%s\t\t%s\t\t%s\t\t%s\t\t", id, title, name, year);
exit(2);
}
fclose(fp);
}
void displayfile()
{
FILE *fp;
fp = fopen("book.txt", "r");
if(fp == NULL)
{
printf("there is no data");
}
while(!feof(fp))
{
fscanf(fp, "%[^#]#%[^#]#%[^#]#%[^#]#%s", id, title, name, year);
printf("\n%s\t\t\n%s\t\t\n%s\t\t\n%s\t\t", id, title, name, year);
}
fclose(fp);
}
void updatefile()
{
FILE *fp;
int ch;
fp = fopen("book.txt", "r+");
if(fp == NULL)
{
fprintf(stderr, "there is no data");
exit(1);
}
while ((ch = fgetc(fp)) !=EOF)
{
if(ch == 'i')
{
fseek(fp, -1, SEEK_CUR);
fputc('a', fp);
fseek(fp, 0, SEEK_CUR);
}
}
fclose(fp);
}
void deletefile()
{
int apus;
FILE *fp;
fp = fopen("book.txt", "r");
}
int main() {
int option;
printf("please select one of this : \n");
printf("1. Input Book Record\n");
printf("2. Display Book Record\n");
printf("3. Update Book Record\n");
printf("4. Erase Book Record\n");
scanf("%d", &option);
switch(option)
{
case 1:
printf("Book ID?");
scanf("%s", id);
printf("Book title?");
scanf("%s", title);
printf("author name?");
scanf("%s", name);
printf("year published?");
scanf("%s", year);
write(id, title, name, year);
break;
case 2:
displayfile();
break;
case 3:
updatefile();
}
getchar();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
char id[20], year[20], title[20], name[20];
void writeTofile(char id[], char title[], char name[], char year[])
{
FILE *fp;
fp = fopen("book.txt", "a"); // if the file is not exist, it will create it.
// do not forget the \n not a new line
fprintf(fp, "%s\t%s\t%s\t%s\n", id, title, name, year);
fclose(fp);
exit(2);
}
void displayfile()
{
FILE *fp;
fp = fopen("book.txt", "r");
if(fp == NULL)
{
fprintf(stderr, "there is no data");
exit(1);
}
while(!feof(fp))
{
fscanf(fp, "%s %s %s %s", id, title, name, year);
printf("%s: %s\t%s\t%s\n", id, title, name, year);
}
fclose(fp);
}
void updatefile(char d, char n)
{
// open the book.txt for read
// open the temp.txt for write
// move all the char from the book.txt to temp.txt
// up to the location where you want to change
// Now add the new chars to the temp file
// continue copying
// close the book.txt
// open it again for write (to delete its content)
//copy temp to the book.txt
// doen
}
void deletefile()
{
int apus;
FILE *fp;
// if you want to delete the file use
// remove("book.txt"), BUT
// if you want to empty the file use
//(opening he file with white will delete its content )
fp = fopen("book.txt", "w");
}
int main() {
int option;
char d, n;
printf("please select one of this : \n");
printf("1. Input Book Record\n");
printf("2. Display Book Record\n");
printf("3. Update Book Record\n");
printf("4. Erase Book Record\n");
scanf("%d", &option);
switch(option)
{
case 1:
printf("Book ID?");
scanf("%s", id);
printf("Book title?");
scanf("%s", title);
printf("author name?");
scanf("%s", name);
printf("year published?");
scanf("%s", year);
writeTofile(id, title, name, year);
break;
case 2:
displayfile();
break;
case 3:
printf("Character to be replaced: ");
scanf("%c", &d);
printf("A new character: ");
scanf("%c", &n);
updatefile(d, n);
break;
case 4:
deletefile();
break;
}
getchar();
return 0;
}