beat365英国-365bet开户地址-365bet体育足球

C语言读写txt文件

C语言读写txt文件

写入和读取txt文件

#include

#include

int main( int argc, char *argv[] )

{

int rc=0;

FILE *fp;

char c[] = " \r\n \

\r\n \

WEB RESTRICTION \r\n \

\r\n \

\r\n \

WARRNING

\r\n \

YOU ARE NOT ALLOWED TO ACCESS THIS PAGE !!!

\r\n\

\r\n \

\r\n";

char buffer[20];

/* 打开文件用于读写 */

fp = fopen("file.txt", "w+");

/* 写入数据到文件 */

fwrite(c, strlen(c) + 1, 1, fp);

/* 查找文件的开头 */

fseek(fp, 0, SEEK_SET);

/* 读取并显示数据 */

fread(buffer, strlen(c)+1, 1, fp);

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

fclose(fp);

return rc;

}

读取指定的txt文件

#include

#include

int main( int argc, char *argv[] )

{

int rc=0;

FILE *fp;

char buffer[256]={0};

/* 打开文件用于读写 */

fp = fopen("file.txt", "r");

/* 读取并显示数据 */

fread(buffer, sizeof(buffer), 1, fp);

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

fclose(fp);

return rc;

}

读取一行

#include

#include

int main( int argc, char *argv[] )

{

int rc=0;

FILE *fp;

char buffer[256]={0};

/* 打开文件用于读写 */

fp = fopen("file.txt", "r");

int len;

if(fp != NULL){

while(fgets(buffer,256,fp) != NULL)

{

len = strlen(buffer);

buffer[len-1] = '\0';

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

}

fclose(fp);

}

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

return rc;

}

# gcc -o str.o str.c

# ./str.o

0 dddddddddddddddd

0 ddddddddd

0 eeeeeeeeeeee

1 eeeeeeeeeeee

相关推荐