对文本内容进行增,删,改,保存
再写另一个类似的题目时,发现这个代码错误很多,做更改如下,希望各位大佬指正:
首先需要明确的一点:当结构作为函数参数被传递时,用指针作为参数,特点是可以修改原结构数据;传递结构,则有利于保护原始数据。
不然,就看各种烫烫烫,或者各种乱码,看得人抓狂。
以及对指向结构的指针,记得要初始化。
1.删除部分
/*传递了结构,但是下面声明了一个指针指向结构的指针,再将变更后的结构赋给指针*/
void del(struct book library[])
{
extern int count;
struct book* pt;
pt = &library[0];
int del;
int i = 0, j = 0;
puts("Which content would you like to delete?Press q to the menu:");
for (int index = 0; index < count; index++,pt++)
printf("%s by %s : %.2f.\n", pt->title, pt->author, pt->value);
while (scanf("%d", &del) == 1)
{
while (i < count)
{
if (j == (del - 1)) //加入需要删除第一条目录,在结构数组中该目录其实是第0条,所以减去1
{
j++;
continue;
}
else
{
library[i] = library[j] ; //变更结构内容,此时的结构为原结构副本,并不会影响原始数据
pt[i] = library[i];
i++;
j++;
}
}
count = count - 1; //我将count声明为一个外部变量,方便全局调用。
puts("Which content would you like to delete?Press q to the menu:");
}
puts("What is you next moving:");
while (getchar() != '\n') //这个将缓存中换行符读取掉的语句真的不知道写了多少遍....
continue;
}
2.保存
if (count > 0) //保存
{
extern int count;
puts("Here is the list of your book:");
for (index = 0; index < count; index++)
printf("%s by %s : %.2f.\n", library[index].title, library[index].author, library[index].value);
pbooks = fopen("book.dat", "w+b"); //在另一个相似程序中,复制了这段,忘记改文件名,崩溃了一天.....
fwrite(&library[0], size, count, pbooks); //由于会删去某个结构,所以选择了”W+b“模式,打开文件清楚所有内容,再重新写入
} //可以增添个判断,即如果没有删过内容,那么就可以用”a+b",在接着以前的内容继续写入
//fwrite(&library[filecount],size,count-filecount,pbooks);
else
puts("No books.");
fclose(pbooks);
3.更改结构的数据,利用指向结构的指针。
本来使用switch来对更改部分做选择,但是考虑到case中的break,会直接跳出循环,所以用了else if,如果修改完后,发现还需要继续修改,可以直接选择需要修改的部分。
void change(struct book * ptr)
{
char ch;
struct book temp;
int i = 0;
puts("Which item do you want to change,press Q to jump to the menu?");
while (scanf("%d", &i) == 1)
{
temp = ptr[i - 1];
while (getchar() != '\n')
continue;
puts("Which part do you want to change?");
puts("a. Title b.Author c.Value d.ignore ");
while ((ch = getchar()) == 'a' || ch == 'b' || ch == 'c')
{
while (getchar() != '\n')
continue;
if (ch == 'a')
{
puts("Enter the new title:");
s_gets(temp.title, MAXTITL);
}
else if (ch == 'b')
{
puts("Enter the new author:");
s_gets(temp.author, MAXTITL);
}
else if (ch == 'c')
{
puts("Enter the new value:");
scanf("%f", &temp.value);
while (getchar() != '\n')
continue;
}
else
break;
ptr[i - 1] = temp;
puts("Need to change this item again,y/n?");
if ((ch = getchar()) == 'y')
{
puts("Which part do you want to change?");
puts("a. Title b.Author c.Value d.ignore ");
while (getchar() != '\n')
continue;
}
else
break;
}
puts("Which item do you want to change (press Q to jump to the menu):");
}
while (getchar() != '\n')
continue;
}
4,新增内容,同样传递指针
void add(struct book * pt) { struct book temp; temp = *pt; puts("Please add new book title:"); puts("Please press [Enter] at the start of a line to stop."); while (count < MAXBKS && s_gets(temp.title, MAXTITL) != NULL && temp.title[0] != '\0') { puts("Enter the author:"); s_gets(temp.author, MAXAUTL); puts("Enter the value:"); scanf("%f", &temp.value); pt[count] = temp; count++; while (getchar() != '\n') continue; if (count < MAXBKS) puts("Keeping add new book title ,press [Enter] at the start of a line to menu . "); } }
完整代码如下:
1 # include <stdio.h> 2 # include <stdlib.h> 3 # include <string.h> 4 5 # define MAXTITL 30 6 # define MAXAUTL 30 7 # define MAXBKS 10 8 9 char* s_gets(char* st, int n); 10 11 struct book { 12 char title[MAXTITL]; 13 char author[MAXAUTL]; 14 float value; 15 }; 16 17 int count = 0; 18 19 void menu(struct book library[]); 20 void add(struct book library[]); 21 void del(struct book library[]); 22 void change(struct book*); 23 int main() 24 { 25 struct book library[MAXBKS]; 26 extern int count; 27 int index, filecount; 28 FILE* pbooks; 29 int size = sizeof(struct book); 30 31 if ((pbooks = fopen("book.dat", "r+b")) == NULL) //二进制模式读写文件,并在文件末尾更新 32 { 33 fputs("Can't open book.dat file\n", stderr); 34 exit(EXIT_FAILURE); 35 } 36 37 rewind(pbooks); 38 39 while (count < MAXBKS && fread(&library[count], size, 1, pbooks) == 1) 40 { 41 if (count == 0) 42 puts("Current contents of book.dat:"); 43 printf("%s by %s :%.2f\n", library[count].title, library[count].author, library[count].value); 44 count++; 45 } 46 filecount = count; 47 48 if (count == MAXBKS) 49 { 50 fputs("The book.dat is full.", stderr); 51 exit(2); 52 } 53 54 menu(library); 55 56 57 if (count > 0) //保存 58 { 59 extern int count; 60 puts("Here is the list of your book:"); 61 for (index = 0; index < count; index++) 62 printf("%s by %s : %.2f.\n", library[index].title, library[index].author, library[index].value); 63 pbooks = fopen("book.dat", "w+b"); 64 fwrite(&library[0], size, count, pbooks); 65 } 66 else 67 { 68 puts("No books."); 69 } 70 71 puts("Bye."); 72 fclose(pbooks); 73 74 return 0; 75 } 76 77 void menu(struct book library[]) 78 { 79 struct book* ptr = &library[0]; 80 char c; 81 puts("Please select your moving:"); 82 puts("a. adding new book b. delete content"); 83 puts("c. change the content d. quit, saving change"); 84 while ((strchr("abcd",c = getchar()) != NULL)) 85 { 86 while (getchar() != '\n') 87 continue; 88 switch(c) 89 { 90 case 'a': add(library); break; 91 case 'b': del(library); break; 92 case 'c': change(ptr); break; 93 } 94 if (c == 'd') 95 break; 96 puts("a. adding new book b. delete content"); 97 puts("c. change the content d. quit, saving change"); 98 } 99 } 100 101 void add(struct book * pt) //增加新的内容 102 { 103 struct book temp; 104 temp = *pt; 105 puts("Please add new book title:"); 106 puts("Please press [Enter] at the start of a line to stop."); 107 while (count < MAXBKS && s_gets(temp.title, MAXTITL) != NULL && temp.title[0] != '\0') 108 { 109 puts("Enter the author:"); 110 s_gets(temp.author, MAXAUTL); 111 puts("Enter the value:"); 112 scanf("%f", &temp.value); 113 pt[count] = temp; 114 count++; 115 while (getchar() != '\n') 116 continue; 117 if (count < MAXBKS) 118 puts("Keeping add new book title ,press [Enter] at the start of a line to menu . "); 119 } 120 } 121 void del(struct book library[]) 122 { 123 124 struct book* pt; 125 pt = &library[0]; 126 127 int del; 128 int i = 0, j = 0; 129 130 puts("Which content would you like to delete?Press q to the menu:"); 131 for (int index = 0; index < count; index++,pt++) 132 printf("%s by %s : %.2f.\n", pt->title, pt->author, pt->value); 133 while (scanf("%d", &del) == 1) 134 { 135 while (i < count) 136 { 137 if (j == (del - 1)) 138 { 139 j++; 140 continue; 141 } 142 else 143 { 144 library[i] = library[j] ; 145 pt[i] = library[i]; 146 i++; 147 j++; 148 } 149 } 150 count = count - 1; 151 puts("Which content would you like to delete?Press q to the menu:"); 152 } 153 puts("What is you next moving:"); 154 while (getchar() != '\n') 155 continue; 156 157 } 158 159 void change(struct book * ptr) 160 { 161 char ch; 162 struct book temp; 163 164 int i = 0; 165 166 puts("Which item do you want to change,press Q to jump to the menu?"); 167 while (scanf("%d", &i) == 1) 168 { 169 temp = ptr[i - 1]; 170 while (getchar() != '\n') 171 continue; 172 173 puts("Which part do you want to change?"); 174 puts("a. Title b.Author c.Value d.ignore "); 175 while ((ch = getchar()) == 'a' || ch == 'b' || ch == 'c') 176 { 177 while (getchar() != '\n') 178 continue; 179 180 if (ch == 'a') 181 { 182 puts("Enter the new title:"); 183 s_gets(temp.title, MAXTITL); 184 } 185 else if (ch == 'b') 186 { 187 puts("Enter the new author:"); 188 s_gets(temp.author, MAXTITL); 189 } 190 else if (ch == 'c') 191 { 192 puts("Enter the new value:"); 193 scanf("%f", &temp.value); 194 while (getchar() != '\n') 195 continue; 196 } 197 else 198 break; 199 ptr[i - 1] = temp; 200 201 puts("Need to change this item again,y/n?"); 202 if ((ch = getchar()) == 'y') 203 { 204 puts("Which part do you want to change?"); 205 puts("a. Title b.Author c.Value d.ignore "); 206 while (getchar() != '\n') 207 continue; 208 } 209 else 210 break; 211 } 212 puts("Which item do you want to change (press Q to jump to the menu):"); 213 } 214 while (getchar() != '\n') 215 continue; 216 } 217 218 char* s_gets(char* st, int n) 219 { 220 char* ret_val; 221 char* find; 222 223 ret_val = fgets(st, n, stdin); 224 if (ret_val) 225 { 226 find = strchr(st, '\n'); 227 if (find) 228 *find = '\0'; 229 else 230 while (getchar() != '\n') 231 continue; 232 } 233 234 return ret_val; 235 }
浙公网安备 33010602011771号