How to read the two lines of character data between begin and end in a TXT document in C language

WBOY
Release: 2024-01-09 08:34:13
forward
743 people have browsed it

How to read the data between two lines of characters in a TXT document in C language, such as between begin and end

Assume the file structure is: 1E1F 0309 luan7 8 zao

2E2F 1518

begin

12 39692

12 39712

9 39724

end

begin

14 39692

8 39724

end

luan7 8 zao

qi qi guai guai

begin

312 39692

142 39712

95 39724

end

Contains multiple segments of begin, end data

#include #include

main( ) {

FILE *fin;

int a[1000],b[1000],n=0,i;

char one_line[80];

fin=fopen("abc.txt","r");

if (fin==NULL){printf("open file error\n");return 0;}

Lab1:while(1){

if ( fgets(one_line,80,fin)==NULL){

printf("Can not fund begin any more\n");goto Lab2;}

if (strncmp(one_line,"begin",5)==0)break;

}

for (i=0;i

if (strncmp(one_line,"end",3)==0)break;

sscanf(one_line,"%d %d",&a[n],&b[n]);

n ;

}

goto Lab1;

Lab2: fclose(fin);

printf("I read:\n");for (i=0;ireturn 0;

}

What to do if the data is extracted from txt in C language and separated by semicolons

fgets reads each line of data and then uses strchr to find the position where semicolon; appears, and then performs the corresponding conversion!

This is an example I wrote to read a specified string. You can refer to this and modify it. The string to floating point number function atof

#include

#include

#define N 10

int readstr(const char* str,char p[][128],int plen,int savepos);

int main()

{

char str[]="2012/10/12;123456.0;456123.0";

char date[N][128]={0};

int len=readstr(str,date,N,0);

for (int i=0;i

{

printf("%s\n",date[i]);

}

return 0;

}

int readstr(const char* str,char p[][128],int plen,int savepos)

{

if(plen

{

printf ("The array is too small, please change the storage location!");

return 0;

}

static int len=0;

char *find=strchr(str,';');

if(find==NULL)

{

strcpy(p[savepos],str);

len ;

return len;

}

else

{

strncpy(p[savepos],str,find-str);

len ;

readstr(find 1,p,N,len);

}

return len;

}

How to read the next line of data of a specified string in C language in txt

Use fgets to read a row, and use sscanf to read the first column of data

The following assumes that the first column of data is used as a string, and is read using sscanf(buf,"%s",... format.

Similarly, use %d for integers and %f for floating points....

#include

#include

main(){

char buf[100];

char col[100][30];

int n=0;

FILE *fp = fopen("a.txt", "r");

while ( fgets(buf, 100, fp) != NULL) {

if (sscanf(buf,"%s",col[n]) == 1){printf("%s\n", col[n]); n ; };

}

return 0;

}

If you want to read the first number of each line, (only read one digit using format):

int x[100];

...

while ( fgets(buf, 100, fp) != NULL) {

if (sscanf(buf," ",&x[n]) == 1){ printf("%d\n",x[n]);n ;};

The above is the detailed content of How to read the two lines of character data between begin and end in a TXT document in C language. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template