c语言如何读取文件并输出

我有个TXT文件,里面包含10W个单词,要求,用户输入单词,在TXT文本中精确查找,并输出。。。我只学会了读取TXT文本
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char ch;
if((fp=fopen("C:\\Users\\hp\\Desktop\\words.txt","r"))==NULL)
{
printf("cannot open the file!");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
return 0;
}
每个英文单词后面有中文注释,存在\n换行符。
准确查找并输出。。。

c语言读取文件并输出的代码如下:

#include<stdio.h>

inta;

charb,c[100];

intmain(){

FILE*fp1=fopen("input.txt","r");//打开输入文件

FILE*fp2=fopen("output.txt","w");//打开输出文件

if(fp1==NULL||fp2==NULL){//若打开文件失败则退出

puts("不能打开文件!");

return0;

fscanf(fp1,"%d",&a);//从输入文件读取一个整数   

b=fgetc(fp1);//从输入文件读取一个字符

fgets(c,100,fp1);//从输入文件读取一行字符串

printf("%ld",ftell(fp1));//输出fp1指针当前位置相对于文件首的偏移字节数

fputs(c,fp2);//向输出文件写入一行字符串

fputc(b,fp2);//向输出文件写入一个字符

fprintf(fp2,"%d",a);//向输出文件写入一个整数

fclose(fp1);//关闭输入文件

fclose(fp2);//关闭输出文件,相当于保存

return0;

C语言中使用fopen()函数实现文件的读取,使用fgetc()函数读取文件中的字符,使用fclose()实现文件的关闭,注意:打开文件流必须要关闭文件流,不然会持续占用计算机内存资源。



扩展资料:

fopen函数

C语言中fopen函数可以两个参数,fopen(const char *filename, const char *mode)。

第一个参数filename:这是 C 字符串,包含了要打开的文件名称。

第二个参数mode:这是 C 字符串,包含了文件访问模式。

参考资料来源:百度百科-fopen

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-11-19

1、C语言标准库提供了一系列文件操作函数。文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。
2、例程:

#include<stdio.h>
int a;
char b,c[100];
int main(){
    FILE * fp1 = fopen("input.txt", "r");//打开输入文件
    FILE * fp2 = fopen("output.txt", "w");//打开输出文件
    if (fp1==NULL || fp2==NULL) {//若打开文件失败则退出
        puts("不能打开文件!");
        rturn 0;
    }
    fscanf(fp1,"%d",&a);//从输入文件读取一个整数
    b=fgetc(fp1);//从输入文件读取一个字符
    fgets(c,100,fp1);//从输入文件读取一行字符串
    
    printf("%ld",ftell(fp1));//输出fp1指针当前位置相对于文件首的偏移字节数
    
    fputs(c,fp2);//向输出文件写入一行字符串
    fputc(b,fp2);//向输出文件写入一个字符
    fprintf(fp2,"%d",a);//向输出文件写入一个整数
    
    fclose(fp1);//关闭输入文件
    fclose(fp2);//关闭输出文件,相当于保存
    return 0;
}

第2个回答  推荐于2017-12-16
//每个单词之间必须用换行符隔开

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define BUF_MAX 1024
int main()
{
char findStr[100];
char *pStr=(char*)malloc(BUF_MAX);
FILE*pFile;
if((pFile=fopen("C:\\Users\\hp\\Desktop\\words.txt","rt"))==NULL)
{
fprintf(stderr,"打开文件错误");
return 1;
}
puts("请输入你要查找的单词:");
scanf("%s",findStr);
bool bFind=false;
while(fgets(pStr,BUF_MAX-1,pFile)!=NULL)
{
if(strstr(pStr,findStr)!=NULL)
{
bFind=true;
printf("%s",pStr);
}
}
if(!bFind)
puts("没有找到");
fclose(pFile);
return 0;
}追问

有QQ吗?我想加你QQ

追答

896442328

本回答被网友采纳
第3个回答  2012-10-29
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define WORD_LENGTH 30#define LINE_LENGTH 256
int get_word_in_line(char *line)
{
if (line == NULL)
return -1;

int word_length = 0;
char *pline = line;

while (*pline != '\0')
{
if(isalpha(*pline++))
word_length ++;
else
break;
}

return word_length;
}

int main()
{
FILE *fp;
char word_search[WORD_LENGTH] = {0};
char line_buf[LINE_LENGTH] = {0};

if ((fp = fopen("words.txt", "r")) == NULL)
{
printf("cannot open the file!");
exit(0);
}

fprintf(stdout,"Plear input a word that you want to search:\n"); gets(word_search);
while (fgets(line_buf, LINE_LENGTH, fp) != NULL && line_buf[0] != '\n')
{
int word_length = get_word_in_line(line_buf);
if(strncmp(word_search, line_buf, word_length) == 0
&& word_search[word_length] == '\0')
{
fputs(line_buf, stdout);
fclose(fp);
return 0;
}
memset(line_buf, 0, LINE_LENGTH);
}

fprintf(stdout, "cannot find the word in the file!\n");
fclose(fp);
return 0;
}

楼上用 函数strstr()有个缺陷
假如文件中有
search “查找,同义词 find”
当查找单词 find 时,同时会检录出如上一行追问

- -有QQ没?我想加你QQ

第4个回答  2012-10-30
楼上正解
相似回答