资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言pread函数 c语言precede函数

计算机二级C语言一道选择题,求详细解法!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

/*

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了呼图壁免费建站欢迎大家使用!

* 此程序可以ls 多个文件

* 范例: ./myls file1 file2 dt1 dt2 后面参数可以多个,

*

*/

#include pwd.h

#include grp.h

#include sys/types.h

#include sys/stat.h

#include fcntl.h

#include unistd.h

#include stdlib.h

#include string.h

#include dirent.h

#include limits.h

#include time.h

#include stdio.h

/*

* 读取不是目录的文件

* argv : 文件名

* 返回值: 出错1, 正确0

*/

int lsregfile(char *argv) //读取不是目录的文件

{

struct stat buf;

if (lstat(argv, buf) 0) //读取文件信息。

{

printf("%s:stat error\n",argv);

return 1;

}

char str[10] = "";

/******************判断文件的类型***********************/

if (S_ISREG(buf.st_mode)) str[0] = '-';

else if (S_ISDIR(buf.st_mode)) str[0] = 'd';

else if (S_ISCHR(buf.st_mode)) str[0] = 'c';

else if (S_ISBLK(buf.st_mode)) str[0] = 'b';

else if (S_ISFIFO(buf.st_mode)) str[0] = 'p';

#ifdef S_ISLNK

else if (S_ISLNK(buf.st_mode)) str[0] = 'l';

#endif

#ifdef S_ISSOCK

else if (S_ISSOCK(buf.st_mode)) str[0] = 's';

#endif

else str[0] = 'U'; //表示未知文件类型

/************************* 输出权限 ************************/

str[1] = ((buf.st_mode S_IRUSR) != 0) ? 'r' : '-';

str[2] = ((buf.st_mode S_IWUSR) != 0) ? 'w' : '-';

str[3] = ((buf.st_mode S_IXUSR) != 0) ? 'x' : '-';

//判断设置-用户-ID

str[3] = ((buf.st_mode S_ISUID) != 0) ? 's' : str[3];

str[4] = ((buf.st_mode S_IRGRP) != 0) ? 'r' : '-';

str[5] = ((buf.st_mode S_IWGRP) != 0) ? 'w' : '-';

str[6] = ((buf.st_mode S_IXGRP) != 0) ? 'x' : '-';

//判断设置-用户组-ID

str[6] = ((buf.st_mode S_ISGID) != 0) ? 'x' : str[6];

str[7] = ((buf.st_mode S_IROTH) != 0) ? 'r' : '-';

str[8] = ((buf.st_mode S_IWOTH) != 0) ? 'w' : '-';

str[9] = ((buf.st_mode S_IXOTH) != 0) ? 'x' : '-';

printf("%s ", str);

// 输出硬链接数

printf("%3ld ", (long) buf.st_nlink);

// 输出文件所有者。

struct passwd *ppd;

ppd = getpwuid(buf.st_uid);

printf("%-5s ", ppd-pw_name);

// 输出文件所有者的组名

struct group *pgroup;

pgroup = getgrgid(buf.st_gid);

printf("%-5s ", pgroup-gr_name);

// 输出文件大小

printf("%5lld ", (long long) buf.st_size);

// 输出最近修改时间

struct tm *ptime = NULL;

ptime = gmtime(buf.st_mtime);

printf("%.4d-%.2d-%.2d %.2d:%.2d ",

(1900 + ptime-tm_year),

(1 + ptime-tm_mon),

ptime-tm_mday,

ptime-tm_hour,

ptime-tm_min);

// 输出文件名

printf("%s", argv);

if (str[0] == 'l') //如果是链接文件,获取链接对象名

{

printf(" - ");

char linkbuf[100] = "";

if (readlink(argv, linkbuf, sizeof(buf)) 0 )

{

printf("%s: readlink error\n",argv);

return 1;

}

printf("%s", linkbuf);

}

printf("\n");

return 0;

}

/*

* 读取目录里面不带.和..的所有文件的信息.

* argv : 文件名

* 返回值: 出错1, 正确0

*/

int lsdir(char *argv) //读取目录

{

printf("%s:\n", argv);

DIR *pread;

pread = opendir(argv);// 打开目录

if (pread == NULL)

{

printf("%s: opendir error\n",argv);

return 1;

}

if (chdir(argv) 0) //修改工作目录 使下面的stat能成功.

{

printf("%s chdir error\n", argv);

return 1;

}

struct dirent *pd = NULL;

long long ssize = 0;

while ((pd = readdir(pread)) != NULL)

{

if ((!strcmp(pd-d_name, ".")) || (!strcmp(pd-d_name, "..")))

continue; //当文件为.或..时跳过。

struct stat buf;

if (lstat(pd-d_name, buf) 0) //读取文件信息。

{

printf("%s: stat error\n", argv);

return 1;

}

char str[10] = "";

/******************判断文件的类型***********************/

if (S_ISREG(buf.st_mode)) str[0] = '-';

else if (S_ISDIR(buf.st_mode)) str[0] = 'd';

else if (S_ISCHR(buf.st_mode)) str[0] = 'c';

else if (S_ISBLK(buf.st_mode)) str[0] = 'b';

else if (S_ISFIFO(buf.st_mode)) str[0] = 'p';

#ifdef S_ISLNK

else if (S_ISLNK(buf.st_mode)) str[0] = 'l';

#endif

#ifdef S_ISSOCK

else if (S_ISSOCK(buf.st_mode)) str[0] = 's';

#endif

else str[0] = 'U'; //表示未知文件类型

/************************* 输出权限 ************************/

str[1] = ((buf.st_mode S_IRUSR) != 0) ? 'r' : '-';

str[2] = ((buf.st_mode S_IWUSR) != 0) ? 'w' : '-';

str[3] = ((buf.st_mode S_IXUSR) != 0) ? 'x' : '-';

//判断设置-用户-ID

str[3] = ((buf.st_mode S_ISUID) != 0) ? 's' : str[3];

str[4] = ((buf.st_mode S_IRGRP) != 0) ? 'r' : '-';

str[5] = ((buf.st_mode S_IWGRP) != 0) ? 'w' : '-';

str[6] = ((buf.st_mode S_IXGRP) != 0) ? 'x' : '-';

//判断设置-用户组-ID

str[6] = ((buf.st_mode S_ISGID) != 0) ? 'x' : str[6];

str[7] = ((buf.st_mode S_IROTH) != 0) ? 'r' : '-';

str[8] = ((buf.st_mode S_IWOTH) != 0) ? 'w' : '-';

str[9] = ((buf.st_mode S_IXOTH) != 0) ? 'x' : '-';

printf("%s ", str);

// 输出硬链接数

printf("%3ld ", (long) buf.st_nlink);

// 输出文件所有者。

struct passwd *ppd;

ppd = getpwuid(buf.st_uid);

printf("%-5s ", ppd-pw_name);

// 输出文件所有者的组名

struct group *pgroup;

pgroup = getgrgid(buf.st_gid);

printf("%-5s ", pgroup-gr_name);

// 输出文件大小

printf("%5lld ", (long long) buf.st_size);

ssize += buf.st_size;

// 输出最近修改时间

struct tm *ptime = NULL;

ptime = gmtime(buf.st_ctime);

printf("%.4d-%.2d-%.2d %.2d:%.2d ",

(1900 + ptime-tm_year),

(1 + ptime-tm_mon),

ptime-tm_mday,

ptime-tm_hour,

ptime-tm_min);

// 输出文件名

printf("%s", pd-d_name);

if (str[0] == 'l') //如果是链接文件,获取链接对象名

{

printf(" - ");

char linkbuf[100] = "";

if (readlink(pd-d_name, linkbuf, sizeof(buf)) 0 )

{

printf("%s: readlink error\n", argv);

return 1;

}

printf("%s", linkbuf);

}

printf("\n");

}

// 输出总用量

printf("总用量 %lld\n", ssize/1024);

///////////////////////////////////////////////////////

if (closedir(pread) 0) //关闭目录

{

printf("%s: closedir error\n", argv);

return 1;

}

}

/*

*

* 这个程序,把非目录文件和目录分开读。

*

*/

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

{

if (argc == 1)

{

printf("the argument is wrong\n");

exit(1);

}

int i;

for (i = 1; i argc; ++i)

{

// 保存当前的工作目录。以便返回当前工作目录

char bufpwd[80];

char *getp =NULL;

getp = getcwd(bufpwd, sizeof(bufpwd));

if (getp == NULL)

{

printf("%s : getcwd error\n",argv[i]);

return 1;

}

struct stat buf0; //获取文件信息

if (stat(argv[i], buf0) 0)

{

printf("%s: stat error\n", argv[i]);

continue; //出错跳过

}

if (!S_ISDIR(buf0.st_mode)) //如果读取的不是目录。就直接去读文件。

lsregfile(argv[i]); //读文件

else

lsdir(argv[i]); //读目录

// 改变工作目录回原来的工作目录,以便读取相对路径的文件

if (chdir(bufpwd) 0)

{

printf("%s : chdir error\n", argv[i]);

return 1;

}

}

return 0;

}

C语言中把文件里面的几组数据依次读出来分别赋值给几个结构体变量(用链表存储)怎么实现?求具体代码

feof(),用这个函数判断是否读到文件尾了。

fread(buf,size,count,fp);//buf输入数据起始地址,size 每个数据块的大小,count 每次写入的数据块个数,fp 文件指针

写好后是:

while(!feof(fp))

{

fread(temp[i],sizeof(struct use),1,fp);//这个读出来放数组里面

i++;

}

问题是你读的是txt文件,完全可以用fscanf()函数么。

读写数据块函数fread怎么用?

首先是最简单的读写一个字符的函数fputc和fgetc,在这个基础上又出现了putw和getw、fgets和fputs,此外还说过格式化读写函数fprintf和fscanf函数。

从原理上来说,只要fputc和fgetc函数基本就可以完成数据的读写操作了,但是在实际的使用中会遇到诸多不便,因此上面那一大堆函数就冒出来了(其实也不多)。上面函数中最方便的就是格式化读写函数fprintf和fscanf了,因为可以一行行的进行读取,但是有个问题就是这两位老人家动作比较慢,fprintf在写文件的时候要把二进制形式表示的数据转换为ASCII码形式,fscanf在读文件的时候又要将ASCII码转换为二进制的形式。

好不容易出来个好用的函数,竟然是个慢性子,聪明的你现在也许就在想了:那能不能不转换,直接读写二进制的数据呢?答案就是fread和fwrite,在需要频繁进行数据读写的时候,使用这两个函数将大大提升效率。

fread和fwrite函数的定义

fread(pBuffer,size,count,pFile);

fwrite(pBuffer,size,count,pFile);

要读写一个数据块的话,无论是读还是写,都需要指定一个起始地址,读的话从这个起始地址读,写的话从这个起始地址写,上面函数定义中的第一个参数pBuffer就是用于指定这个起始地址,size读写的字节数,count则指定读写多少个size大小的数据,pFile是文件结构指针。

fread和fwrite函数使用示例

说到数据块我们自然会想到结构体,如果一个文件中保存的是一个个结构体信息,那每一个结构体信息就可以看做一个数据块了。这里直接用前面文章中我们一直使用的学生成绩信息的结构体:

C语言: 知蚁博客

struct student

{

int nID; //学号

char chName[20]; //姓名

float fScores[3]; //3门课的成绩

};

实例程序有点小复杂的,首先我们自己新建一个txt文件,也就是ASCII文件啦,然后写上一些数据,如下:

1 zhangsan 78.0 79.0 80.0

2 lisi 79.0 77.0 78.0

3 wangwu 90.0 97.0 78.0

4 zhaokai 56.0 57.0 58.0

我们先用ASCII方式打开这个文件,通过fscanf函数读取里面的数据,然后通过fwrite写入到新的二进制格式的文件中,这样我们就得到了一个保存上面信息的二进制格式的文件了,下面就是通过fread函数进行数据的读取了。(记住:fread和fwrite一般用于二进制文件的输入输出,ASCII文件还是不要考虑了)。

C++语言: 知蚁博客

#include "stdio.h"

struct student

{

int nID; //学号

char chName[20]; //姓名

float fScores[3]; //3门课的成绩

};

void main()

{

FILE *pRead,*pWrite;

struct student tStu[4];

struct student *ptStu = NULL;

int nCount = 0;

//ASCII方式打开文件 用于读入

pRead=fopen("stu_scores.txt","r");

if(NULL == pRead)

{

return;

}

//二进制文件打开文件 用于写入

pWrite=fopen("stu_scores_bin.txt","wb");

if(NULL == pWrite)

{

fclose(pRead);

return;

}

//fscanf读取数据,fwrite写入数据

ptStu = tStu;

while(!feof(pRead))

{

fscanf(pRead,"%d %s %f %f %f\n",ptStu-nID,ptStu-chName,ptStu-fScores[0],ptStu-fScores[1],ptStu-fScores[2]);

fwrite(ptStu,sizeof(struct student),1,pWrite);

printf("%d %s %.1f %.1f %.1f\n",ptStu-nID,ptStu-chName,ptStu-fScores[0],ptStu-fScores[1],ptStu-fScores[2]);

ptStu++;

}

fclose(pRead);

fclose(pWrite);

memset(tStu,0×00,sizeof(tStu)); //清空数据

//二进制文件打开文件 用于读取

pRead=fopen("stu_scores_bin.txt","rb");

if(NULL == pRead)

{

printf("open file stu_scores_bin.txt failed");

return;

}

//下面有两种fread的读数据方式,将下面的1换成0,则使用第二种方式

#if 1

//一条条的读取

ptStu = tStu;

nCount = fread(ptStu,sizeof(struct student),1,pRead);

while(nCount0)

{

printf("%d %s %.1f %.1f %.1f\n",ptStu-nID,ptStu-chName,ptStu-fScores[0],ptStu-fScores[1],ptStu-fScores[2]);

ptStu++;

nCount = fread(ptStu,sizeof(struct student),1,pRead);

}

#else

//因为事先知道有4条信息,因此可以直接读取四条信息

fread(tStu,sizeof(struct student),4,pRead);

for(nCount=0; nCount4; nCount++)

{

printf("%d %s %.1f %.1f %.1f\n",tStu[nCount].nID,tStu[nCount].chName,tStu[nCount].fScores[0],tStu[nCount].fScores[1],tStu[nCount].fScores[2]);

}

#endif

fclose(pRead);

}

上面用fread读取的时候,我们既可以一条条的读取,也可以一次读入多条,这就是为什么参数中有size和count的原因

编写线程并发拷贝程序,通过模拟管道文件的实现(管道定义、创建管道、读管道程序和写管道程序程序实现)

#include sys/wait.h

#include assert.h

#include stdio.h

#include stdlib.h

#include unistd.h

#include string.h

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

{

int pfd[2];

pid_t cpid;

char buf;

assert(argc == 2);

if (pipe(pfd) == -1)

{

perror("pipe");

exit(EXIT_FAILURE);

}

//创建子进程

cpid = fork();

if (cpid == -1)

{

//创建子进程失败

perror("fork"); exit(EXIT_FAILURE);

}

if (cpid == 0)

{ /* 子进程中读取管道里面读数据 */

close(pfd[1]); /* 关闭不使用的管道写入端 */

while (read(pfd[0], buf, 1) 0)

{

write(STDOUT_FILENO, buf, 1);//在标准的输出设备上输出读取管到的内容

}

write(STDOUT_FILENO, "\n", 1);

close(pfd[0]);

_exit(EXIT_SUCCESS);

}

else

{ /* 父进程将参数 argv[1]字符串 写入管道 */

close(pfd[0]); /* 关闭不使用的管道读取端 */

write(pfd[1], argv[1], strlen(argv[1]));

close(pfd[1]); /* 关闭管道写入端,此时读取端就会收到 EOF 标志*/

wait(NULL); /* 等待子进程运行结束 */

exit(EXIT_SUCCESS);

}

}

C语言从文件里面读取数据

函数调用严重有问题,求#include"cipan.h"里面的详细内容,单看函数readdisk()的话没啥问题


当前标题:c语言pread函数 c语言precede函数
转载来源:http://cdkjz.cn/article/hhjiss.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220