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] = '