C语言:怎样读取一个文件的内容,并拷贝到结构体中

C语言:怎样读取一个文件的内容,并拷贝到结构体中,第1张

先声明一个结构体AAA的实例stru

Struct AAA stru;

先用fopen打开文件

FILE fp=fopen("XXXtxt","rb");

然后用fread读取就行了

fread( &stru, sizeof(Struct AAA), 1 ,fp);

四个整型 一个浮点型, 所以 用数组的话 只能是用浮点型数组, 即float a[5];

或者用五个变量, 可以是四个整型,一个浮点型 int a,b,d,e; float c;

打开文件部分相同

FILE fp = fopen("input1txt", "r");

读取数据, 数组方式:

int i;

float a[5];

fscanf(fp, "%f,", &a[0]);

for(i = 1; i <5; i ++)

    fscanf(fp, "%f",&a[i]);

变量方式:

int a,b,d,e; 

float c;

fscanf(fp,"%d,%d%f%d%d", &a,&b,&c,&d,&e);

1通过fopen函数打开文本,例如file

fp=fopen("intxt","r");//返回一个file类型的句柄

2然后就可以通过fcanf()函数对txt文本进行读取

3 *** 作完文本之后用fclose()函数

关闭已经打开的文件。

#include

int main()

{

int data;

file fp=fopen("intxt","r");

if(!fp)

{

printf("can't open file\n");

return -1;

}

while(!feof(fp))

{

fscanf(fp,"%d",&data);

printf("%4d",data);

}

printf("\n");

fclose(fp);

return 0;

}

哥哥我想出的办法是这样的:

因为兄弟文件格式是一行一行的, 且每行开头是一个关键字,然后后面是相应数据, 所以哥哥是这么想的, 比如要读"AB2345"这个关键字对应行的内容, 那首先用变量或者宏定义定义下来, 然后从文件开头开始, 一个字符一个字符的扫描, 对每一行的开始的6个字符组成的关键字读出来跟"AB2345"这个关键字比较, 判断是否是想要读取的, 如果是, 那么通过ftell, fseek 两个函数分别得出当前指针的位置和适当移动指针的位置, 最后读取相应内容输出来!

详细代码如下:

#include <stdioh>

#include <stdlibh>

#include <stringh>

#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("testtxt", "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;

}

C语言可以使用fopen()函数读取txt文本里。

示例:

#include <stdioh>

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\n" );

else

printf( "The file 'data' was opened\n" );

/ Open for write /

if( (stream2 = fopen( "data2", "w+" )) == NULL )

printf( "The file 'data2' was not opened\n" );

else

printf( "The file 'data2' was opened\n" );

/ Close stream /

if(fclose( stream2 ))

printf( "The file 'data2' was not closed\n" );

/ All other files are closed: /

numclosed = _fcloseall( );

printf( "Number of files closed by _fcloseall: %u\n", numclosed );

}

扩展资料

使用fgetc函数

#include <stdioh>

#include <stdlibh>

void main( void )

{

FILE stream;

char buffer[81];

int  i, ch;

/ Open file to read line from: /

if( (stream = fopen( "fgetcc", "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] = '\0';

printf( "%s\n", buffer );

fclose( stream );

}

以上就是关于C语言:怎样读取一个文件的内容,并拷贝到结构体中全部的内容,包括:C语言:怎样读取一个文件的内容,并拷贝到结构体中、c语言从文件读取数据、C语言读取文件内容,按行读等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9486974.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存