写一段程序C语言实现读取记事本特定数字
时间: 2023-04-02 16:05:15 浏览: 61
#include <stdio.h>
int main() {
FILE *fp;
int num;
fp = fopen("note.txt", "r"); // 打开文件
if (fp == NULL) { // 判断文件是否打开成功
printf("无法打开文件\n");
return 1;
}
while (fscanf(fp, "%d", &num) != EOF) { // 读取文件中的数字
if (num == 123) { // 判断是否为特定数字
printf("找到了特定数字!\n");
break;
}
}
fclose(fp); // 关闭文件
return ;
}
相关问题
写一段程序C语言实现读取记事本数字
以下是一段C语言程序,可以读取记事本中的数字:
```c
#include <stdio.h>
int main() {
FILE *fp;
char filename[] = "note.txt";
int num;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法打开文件 %s\n", filename);
return 1;
}
while (fscanf(fp, "%d", &num) != EOF) {
printf("%d\n", num);
}
fclose(fp);
return ;
}
```
这个程序会打开名为 "note.txt" 的文件,读取其中的数字,并将它们逐个输出到屏幕上。如果文件无法打开,则会输出一条错误信息。
写一段程序C语言实现读取记事本中特定的文字
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp;
char str[100];
char target[20] = "特定的文字";
fp = fopen("note.txt", "r");
if (fp == NULL) {
printf("无法打开文件\n");
return 1;
}
while (fgets(str, 100, fp) != NULL) {
if (strstr(str, target) != NULL) {
printf("%s", str);
}
}
fclose(fp);
return ;
}
// 请将 "特定的文字" 替换为你要查找的文字。记事本文件名为 "note.txt",请将其放在程序所在的文件夹中。