3. 向文件中输入字符串
代码:
#include "stdio.h"
void main() {
FILE *file;
file = fopen("test.txt", "w");
char str[200];
gets(str);
int i = 0;
while(str[i]) {
if(str[i] >= 'a' && str[i] <= 'z') str[i] = str[i] + 'A' - 'a';
i++;
}
fputs(str, file);
fclose(file);
}
输出结果:
4.合并字符串,并且写入新文件C中。
代码:
#include "stdio.h"
void init();
char *read(char *file_name, char *str);
char *merge(char *A, char *B, char *C);
void sort(char *str);
void write(char *file_name, char *str);
void main() {
init();
char A[200];
read("A.txt", A);
char B[200];
read("B.txt", B);
char C[400];
merge(A, B, C);
printf("%s\n", C);
sort(C);
printf("%s\n", C);
write("C.txt", C);
}
void init() {
FILE *afile, *bfile;
afile = fopen("A.txt", "w");
bfile = fopen("B.txt", "w");
char A[200] = "afjfaojfoqjefqpoqhf";
char B[200] = "ajiafdhiophpahofhap";
fputs(A, afile);
fputs(B, bfile);
fclose(afile);
fclose(bfile);
}
char *read(char *file_name, char *str) {
FILE *file = fopen(file_name, "r");
fgets(str, 200, file);
fclose(file);
return str;
}
char *merge(char *A, char *B, char *C) {
int count = 0;
for(int i = 0; A[i]; i++) {
C[i] = A[i];
count++;
}
for(int i = 0; B[i]; i++) {
C[count] = B[i];
count++;
}
C[count] = 0;
return C;
}
void sort(char *str) {
char *c = str;
char *p = str + 1;
char temp;
while (*c)
{
p = c + 1;
while(*p) {
if(*c > *p) {
temp = *p;
*p = *c;
*c = temp;
}
p++;
}
c++;
}
}
void write(char *file_name, char *str) {
FILE *file = fopen(file_name, "w");
fputs(str, file);
fclose(file);
}
输出结果:
5. 将平均分写入文件中保存
参考第九章9_5题
代码:
#include "stdio.h"
struct Student
{
int num;
char name[10];
int score[3];
int mean;
};
void print(struct Student *student, int count);
void input(struct Student *student, int count);
int get_mean(struct Student *student, int count);
void main() {
int count = 2;
struct Student *student = (struct Student *) malloc (count * sizeof(struct Student));
input(student, count);
print(student, count);
//printf("The mean score is: %d\n", get_mean(student, count));
get_mean(student, count);
FILE *file;
file = fopen("stud", "w+");
for(int i = 0; i < count; i++) {
fwrite(&student[i], sizeof(struct Student), 1, file);
}
print(student, count);
// for(int i = 0; i < count; i++) {
// fread(&student[i], sizeof(struct Student), 1, file);
// printf("The student info is %d, %s, %d, %d, %d, %d\n", student->num, student->name, student->score[0], student->score[1], student->score[2], student->mean);
// }
fclose(file);
free(student);
}
void print(struct Student *student, int count) {
for(int i = 0; i < count; i++) {
printf("The student info is %d, %s, %d, %d, %d, %d\n", student->num, student->name, student->score[0], student->score[1], student->score[2], student->mean);
student++;
}
}
void input(struct Student *student, int count) {
for(int i = 0; i < count; i++) {
printf("Please input student info(like 001,jin,100):");
scanf("%d %s %d %d %d", &(student + i)->num, (student + i)->name, (student + i)->score, (student + i)->score + 1, (student + i)->score + 2);
//(student + i)->num=1; (student + i)->name="jin"; (student + i)->score=100;
}
}
int get_mean(struct Student *student,int count) {
for(int i = 0; i < count; i++) {
//printf("get_mean:%d", student->score[0]);
student->mean = (student->score[0] + student->score[1] + student->score[2])/3;
student++;
}
return 0;
}
输出结果:
6. 平均成绩排序
代码:
struct Student *head_node = c;
struct Student *node = c->next;
while (head_node->next)
{
while(node) {
if(head_node->mean > node->mean) {
swap(&head_node->mean, &node->mean);
for(int i = 0; i < 3; i++) {
swap(&(head_node->score[i]), &(node->score[i]));
}
swap(&head_node->num, &node->num);
for(int i = 0; i < 10; i++) {
swap_c(&(head_node->name[i]), &(node->name[i]));
}
}
node = node->next;
}
head_node = head_node->next;
node = head_node->next;
}
}
void swap(int *a, int *b) {
int temp;
temp = *b;
*b = *a;
*a = temp;
}
void swap_c(char *a, char *b) {
char temp;
temp = *b;
*b = *a;
*a = temp;
}
输出结果:
11. 读写文件
代码:
#include "stdio.h"
void main() {
FILE *file;
file = fopen("test.txt", "a");
char str[3][200];
for(int i = 0; i < 3; i++) {
gets(str[i]);
int j = 0;
while(str[i][j]) {
if(str[i][j] >= 'a' && str[i][j] <= 'z') str[i][j] = str[i][j] + 'A' - 'a';
j++;
}
fputs(str[i], file);
}
fclose(file);
file = fopen("test.txt", "r");
char str1[600];
fgets(str1, 600, file);
printf("%s", str1);
fclose(file);
}
输出结果:
C程序设计最后一篇,记录每一步,加油!