C语言打印记事本内搜索字符串所在行信息


    本程序采用C语言编写,使用方法:

   1.双击“甲骨文字符串查询作品.exe”运行程序;

   2.运行前请确保此可执行程序目录下有1.txt文件。

   3.根据提示输入一个字符串,程序将显示存在所搜索字符串的所有行!

    程序如果问题,请联系xingyun2684@gmail.com!

                                                

2014-7-31日安阳师范学院机房完成。

      

程序截图:

 

源码如下:

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int main()
{
    FILE *fp;
    char str[1024],search[1024];
    int i;
    char *j;
    system("color a");
    system("title 甲骨文字符串查询系统by星云");
    system("cls");
    while(1){
        system("cls");
        printf("
	本程序将打印1.txt内,程序将显示所搜索字符串的该行信息
");
        printf("
	程序运行前请确保根目录下有1.txt文件。
");
        printf("
	程序如有问题,请联系xingyun2684@gmail.com!

");
        if (fp=fopen("1.txt","r"))
        {
            
            i=0;
            printf("请输入要搜索的字符:
");
            scanf("%s",search);
            printf("
");
            while (fgets(str,sizeof(str),fp))
            {
                i++;        
                j=strstr(str,search);
                if(j)
                {    
                    printf("%s
",str);
                    
                }
            }    
            fclose(fp);        
        } 
        printf("

按任意键继续
");
        getch();
    }
    printf("

按任意键退出程序!

");
    getch();
    return 0;
}
原文地址:https://www.cnblogs.com/xingyunblog/p/3883656.html