1. c语言 怎么从文件中读取指定内容
哥哥我想出的办法是这样的:
因为兄弟文件格式是一行一行的, 且每行开头是一个关键字,然后后面是相应数据, 所以哥哥是这么想的, 比如要读"AB2345"这个关键字对应行的内容, 那首先用变量或者宏定义定义下来, 然后从文件开头开始, 一个字符一个字符的扫描, 对每一行的开始的6个字符组成的关键字读出来跟"AB2345"这个关键字比较, 判断是否是想要读取的, 如果是, 那么通过ftell, fseek 两个函数分别得出当前指针的位置和适当移动指针的位置, 最后读取相应内容输出来!
详细代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 1024
#define KEY "AB2345"
#define KEY_LEN 7
int main()
{
int ch = 0;
int first = 1;//开始时的标志,因为是一个字符一个字符的扫描
int flag = 0;//文件开头是不是所要读内容的标志
int count = 0;//遇到'\n'的个数
int pre_pos = 0, cur_pos = 0;//前一次和当前文件指针的位置
char buf[BUF_SIZE] = {0};
FILE *fp = NULL;
fp = fopen("test.txt", "r");
if (fp == NULL)
{
printf("Cann't open the file!\n");
exit(1);
}
else
{
while ((ch = fgetc(fp)) != EOF)
{
if (first)
{
//若要读取的内容在文件开头就有时
//移动指针到文件开头
fseek(fp, -1L, SEEK_CUR);
fgets(buf, KEY_LEN, fp);
if (strcmp(buf, KEY) == 0)
{
first = 0;
flag = 1;
continue;
}
else
{
first = 0;
}
}
if (ch == '\n')
{
count++;//遇到'\n'的个数
pre_pos = cur_pos;//上次遇到'\n'时文件指针的位置
cur_pos = ftell(fp);//当前遇到'\n'时文件指针的位置
//文件开头内容符合要求的就适当移动指针位置
//然后读取输出来
if (count == 1 && flag == 1)
{
fseek(fp, 0L, SEEK_SET);
memset(buf, 0, sizeof(buf));
fgets(buf, cur_pos - 1, fp);
printf("%s\n", buf);
}
//之后内容符合要求的就适当移动指针位置
//然后读取输出来
else
{
memset(buf, 0, sizeof(buf));
fgets(buf, KEY_LEN, fp);
if (strcmp(buf, KEY) == 0)
{
fseek(fp, (-1) * (KEY_LEN - 1), SEEK_CUR);
memset(buf, 0, sizeof(buf));
fgets(buf, cur_pos-1-pre_pos, fp);
printf("%s\n", buf);
}
}
}
}
}
fclose(fp);
return 0;
}
2. C语言如何读取文件中指定的某一段
#include<stdio.h>
#include<string.h>
#define TBEGIN "B"
#define TEND "END B"
void main() { FILE *fp; char buffer[256];
if ( fp=fopen("c:\test.txt","r") ) {
while ( !feof(fp) ) { fgets(buffer,255,fp); if ( strcmp(buffer,TBEGIN)==0 ) break; }
while ( !feof(fp) ) {
fgets(buffer,255,fp); if ( strcmp(buffer,TEND)==0 ) break; else printf("%s ",buffer);
}
fclose(fp);
} else printf("无法打开文本文件. ");
}
3. C语言怎样读取文本的每一行
FILE *fin;
char *one_line;
one_line = (char*)malloc( MAX_LINE_SIZE * sizeof(char) );
if((fin = fopen(argv[1], "r"))==NULL)
{
printf("can not open file %s !\n", argv[1]);
exit(-1);
}
while( fgets(one_line, MAX_LINE_SIZE, fin) != NULL )
{
printf("读一行打印一行%s",one_line);
}
fclose(fin);
4. C语言如何读取文件中的内容一直读到结尾,每次读5个字(包括标点符号)
先要打开文件,使用fopen,fread读取文件内容,判断文件尾的方法我忘掉了,躯体你查查,读到内存buffer中,再显示出来,释放内存空间,关闭文件。基本上就这些步骤。
5. C语言如何读取txt文本里面的内容
C语言可以使用fopen()函数读取txt文本里。
示例:
#include <stdio.h>
FILE *stream, *stream2;
void main( void )
{
int numclosed;
/* Open for read (will fail if file "data" does not exist) */
if( (stream = fopen( "data", "r" )) == NULL )
printf( "The file 'data' was not opened " );
else
printf( "The file 'data' was opened " );
/* Open for write */
if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened " );
else
printf( "The file 'data2' was opened " );
/* Close stream */
if(fclose( stream2 ))
printf( "The file 'data2' was not closed " );
/* All other files are closed: */
numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u ", numclosed );
}
(5)c语言怎么读取句子扩展阅读
使用fgetc函数
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
FILE *stream;
char buffer[81];
int i, ch;
/* Open file to read line from: */
if( (stream = fopen( "fgetc.c", "r" )) == NULL )
exit( 0 );
/* Read in first 80 characters and place them in "buffer": */
ch = fgetc( stream );
for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )
{
buffer[i] = (char)ch;
ch = fgetc( stream );
}
/* Add null to end string */
buffer[i] = '