c语言如何读写 linux文本文件?

Python012

c语言如何读写 linux文本文件?,第1张

Linux下C语言的文件(fputc,fgetc,fwrite,fread对文件读写操作)

//

fputc 向文件写入字符

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp

char ch

if((fp=fopen("test.txt","w"))==NULL)

{

printf("不能打开文件\n")

exit(0)

}

while ((ch=getchar())!='\n')

fputc( ch, fp )

fclose(fp)

}

-------------

小提示:

fp=fopen("test.txt","w") ,把"w"改为 "a" 可以创建文件并且追加写入内容

exit(0)需要包含 stdlib.h 头文件,才能使用

//

fgetc 读取字符

#include <stdio.h>

#include <stdlib.h>

main( int argc, char *argv[] )

{

char ch

FILE *fp

int i

if((fp=fopen(argv[1],"r"))==NULL)

{

printf("不能打开文件\n")

exit(0)

}

while ((ch=fgetc(fp))!=EOF)

putchar(ch)

fclose(fp)

}

文件结尾,通过判断 EOF

//

fwrite 的使用

使数组或结构体等类型可以进行一次性读写

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp1

int i

struct student{

char name[10]

int age

float score[2]

char addr[15]

}stu

if((fp1=fopen("test.txt","wb"))==NULL)

{

printf("不能打开文件")

exit(0)

}

printf("请输入信息,姓名 年龄 分数1 分数2 地址:\n")

for( i=0i<2i++)

{

scanf("%s %d %f %f %s",stu.name,&stu.age,&stu.score[0],&stu.score[1], stu.addr)

fwrite(&stu,sizeof(stu),1,fp1)

}

fclose(fp1)

}

//

fread 的使用

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp1

int i

struct student{

char name[10]

int age

float score[2]

char addr[15]

}stu

if((fp1=fopen("test.txt","rb"))==NULL)

{

printf("不能打开文件")

exit(0)

}

printf("读取文件的内容如下:\n")

for (i=0i<2i++)

{

fread(&stu,sizeof(stu),1,fp1)

printf("%s %d %7.2f %7.2f %s\n",stu.name,stu.age,stu.score[0],stu.score[1],stu.addr)

}

fclose(fp1)

}

//

fprintf , fscanf, putw , getw , rewind , fseek 函数

这些函数的话我就不演示了 ,

这些函数基本都一对来使用,例如 fputc 和 fgetc 一起来用.

命令是查询当前登录的每个用户,它的输出包括用户名、终端类型、登录日期及远程主机,在Linux系统中输入who命令输出如下:

我们先man一下who,在帮助文档里可以看到,who命令是读取/var/run/utmp文件来得到以上信息的。

我们再man一下utmp,知道utmp这个文件,是二进制文件,里面保存的是结构体数组,这些数组是struct utmp结构体的。

struct utmp {

short ut_type

pid_t ut_pid

charut_line[UT_LINESIZE]

charut_id[4]

charut_user[UT_NAMESIZE]

charut_host[UT_HOSTSIZE]

struct {

int32_t tv_sec

int32_t tv_usec

} ut_tv

/***等等***/

}

要实现who只需要把utmp文件的所有结构体扫描过一遍,把需要的信息显示出来就可以了,我们需要的信息有ut_user、ut_line、ut_tv、ut_host。

老师给的初始代码:who1.c运行结果如下:

需要注意的是utmp中所保存的时间是以秒和微妙来计算的,所以我们需要把这个时间转换为我们能看懂的时间,利用命令man -k time | grep 3搜索C语言中和时间相关的函数:

经过搜索发现了一个ctime()函数,似乎可以满足我们的需求,于是对代码中关于时间的printf进行修改:

printf("%s",ctime(&utbufp->ut_time))

编译运行发现出来的结果虽然已经转换成了我们能看懂的时间格式,但是很明显这个时间是错的:

搜索一下ut_time这个宏,发现它被定义为int32_t类型:

但是ctime()函数中要求参数的类型是time_t类型,所以重新定义一下类型,编译运行之后,发现时间已经改成了正确的,但是发现()中的内容被换行了,猜想ctime()函数的返回值可能自动在最后补了一个字符\n:

一开始想通过\r\b来实现“退行”,但实践后发现并不可取,最后考虑到直接修改字符串中最后一个字符为\0,让其字符串结束,使输出达到与系统who命令一样的效果,即在输出语句前添加如下代码:

cp[strlen(cp)-1] = '\0'

最后编译执行效果,发现解决了该问题:

虽然能看出基本上和who指令的执行结果一致,但是并非完全一样,主要在两点,第一是时间格式不一样,第二个是比who执行的结果多了几条,需要注意的是utmp中保存的用户,不仅仅是已经登陆的用户,还有系统的其他服务所需要的“用户”,所以在显出所有登陆用户的时候,应该过滤掉其他用户,只保留登陆用户。我们可以通过ut_type来区别,登陆用户的ut_type是USER_PROCESS。

先用if语句对执行结果进行过滤,效果如下:

接着解决时间格式问题,利用man命令收到了两个非常有用的函数:localtime()和strftime(),localtime()是把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,strftime()则是用来定义时间格式的,如:年-月-日,利用这两个函数对时间进行修改后,结果显示终于和系统中who命令一模一样:

最终完整的代码如下:

#include <stdio.h>

#include <stdlib.h>

#include <utmp.h>

#include <fcntl.h>

#include <unistd.h>

#include <time.h>

#define SHOWHOST

void show_time(long timeval){

char format_time[40]

struct tm *cp

cp = localtime(&timeval)

strftime(format_time,40,"%F %R",cp)

printf("%s",format_time)

}

int show_info( struct utmp *utbufp )

{

if(utbufp->ut_type == USER_PROCESS){

printf("%-8.8s", utbufp->ut_name)

printf(" ")

printf("%-8.8s", utbufp->ut_line)

printf(" ")

show_time(utbufp->ut_time)

printf(" ")

#ifdef SHOWHOST

printf("(%s)", utbufp->ut_host)

#endif

printf("\n")

}

return 0

}

int main()

{

struct utmp current_record

int utmpfd

int reclen = sizeof(current_record)

if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){

perror( UTMP_FILE )

exit(1)

}

while ( read(utmpfd, &current_record, reclen) == reclen )

show_info(&current_record)

close(utmpfd)

return 0

}