#include<stdio.h> #include <string.h> #include <stdlib.h> int main() { FILE *f, *g; char temp[20]; char w[100][20]; int i, j = 0, count = 0; f = fopen("in.txt", "r"); g = fopen("words.txt", "w"); while (!feof(f)) //读入字符串(以单词的形式) fscanf(f,"%s", w[count++]); rewind(f); for(i=0;i<count;i++) { for(j=0;j<strlen(w[i]);j++) { if((w[i][j]<= 'z'&&w[i][j]>= 'a') || (w[i][j]<= 'Z'&&w[i][j]>= 'A')||w[i][j]==' ') { if(w[i][j]>='A'&&w[i][j]<='Z') w[i][j]+=32; } else w[i][j]='\0'; } } for (i = 0; i < count; i++) { if(strcmp(w[i], "which") == 0){ strcpy(w[count++], "purpose"); continue; }else if(strcmp(w[i], "york") == 0){ strcpy(w[count++], "th"); continue; } } count++; for(i=0;i<count;i++) { for(j=i;j<count-1;j++) { if(strcmp(w[i],w[j])>0) { strcpy(temp,w[i]); strcpy(w[i],w[j]); strcpy(w[j],temp); } } } fprintf(g,"%s\n",w[0]); for(i=1;i<count-1;i++) { if(strcmp(w[i],w[i-1])!=0) fprintf(g,"%s\n",w[i]); } fclose(f); fclose(g); return 0; }
时间: 2024-01-04 18:04:57 浏览: 34
该代码实现了读入一个文件中的单词,并将单词按字典序排序后输出到另外一个文件中。其中还包括了一些特殊操作,如将"which"替换为"purpose",将"york"替换为"th"等。
代码思路:
1. 打开输入文件和输出文件。
2. 读入输入文件中的单词,存储到一个二维字符数组中。
3. 对每个单词进行处理,将其中的大写字母转换为小写字母,并将非字母字符替换为'\0'。
4. 进行特殊操作,将"which"替换为"purpose",将"york"替换为"th"等。
5. 对存储单词的数组进行排序,按照字典序从小到大排序。
6. 将排序后的单词依次输出到输出文件中,去除重复的单词。
需要注意的是,该代码中存在一些问题,例如:
1. 在读取单词时,使用了feof函数进行判断,这样可能会读取到最后一个单词两次。
2. 在进行特殊操作时,修改了count的值,导致后面的单词无法被处理。
3. 在进行去重操作时,没有考虑到最后一个单词可能会和前面的单词重复。
因此,建议在使用该代码时进行修改和改进。
相关问题
编写C语言代码,将.c文件中的#include<stdio.h>展开
以下是一个简单的C语言代码,用于将.c文件中的#include<stdio.h>展开:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void expand_header(const char* filename) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file!\n");
return;
}
// 创建一个新的临时文件
char* tmp_filename = strdup("tmp.c");
FILE* tmp_fp = fopen(tmp_filename, "w");
// 逐行读取输入文件
char line[1024];
while (fgets(line, sizeof(line), fp)) {
// 检查是否包含#include<stdio.h>
if (strstr(line, "#include<stdio.h>") != NULL) {
// 打开stdio.h文件并将其内容复制到临时文件中
FILE* stdio_fp = fopen("stdio.h", "r");
if (stdio_fp != NULL) {
char stdio_line[1024];
while (fgets(stdio_line, sizeof(stdio_line), stdio_fp)) {
fputs(stdio_line, tmp_fp);
}
fclose(stdio_fp);
}
} else {
// 将当前行复制到临时文件中
fputs(line, tmp_fp);
}
}
// 关闭文件并删除原来的文件
fclose(fp);
remove(filename);
// 将临时文件重命名为原来的文件名
rename(tmp_filename, filename);
// 释放内存
free(tmp_filename);
fclose(tmp_fp);
}
int main() {
const char* filename = "test.c";
expand_header(filename);
printf("Header expanded successfully!\n");
return 0;
}
```
请注意,此代码只是一个简单的示例,仅展开了一个#include<stdio.h>语句,而且还假设stdio.h文件位于当前目录中。在实际情况中,可能需要展开多个#include语句,并且可能需要处理一些特殊情况,例如条件编译指令。此外,展开头文件可能会导致代码变得冗长和难以阅读,因此在实际项目中,应该尽量避免手动展开头文件。
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { FILE *fp; fp=fopen("hello.txt","w"); fprintf("1\n"); }
这段代码存在问题,因为 `fprintf()` 函数需要两个参数,第一个参数应该是文件指针,第二个参数是要写入文件的字符串。因此,正确的代码应该是这样的:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
FILE *fp;
fp = fopen("hello.txt", "w");
if (fp == NULL) {
printf("Failed to open file!\n");
return 1;
}
fprintf(fp, "1\n");
fclose(fp);
return 0;
}
```
这个程序打开一个名为 "hello.txt" 的文件,并向文件中写入字符串 "1\n",然后关闭文件。如果文件打开失败,程序会输出一条错误消息并退出。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)