导航:首页 > 编程系统 > linuxrewind函数

linuxrewind函数

发布时间:2023-03-06 09:02:30

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

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("不能打开文件 ");

exit(0);

}

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

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("不能打开文件 ");

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 地址: ");

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

{

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("读取文件的内容如下: ");

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

{

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

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

}

fclose(fp1);

}

//

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

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

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

⑵ linux下哪个系统调用函数可以得到指定目录下的所有文件

opendir/readdir/closedir: 读取目录资讯
--------------------------------------------------------------------------------
#include
DIR * opendir(const char * pathname);
int closedir(DIR *dir);
struct dirent * readdir(DIR *dir);
int rewinddir(DIR *dir);
struct dirent {
long d_ino; /* inode number */
off_t d_off; /* offset to this dirent */
unsigned short d_reclen; /* length of this d_name */
char d_name [NAME_MAX+1]; /* file name (null-terminated) */
};
opendir开启一个目录操作DIR,closedir关闭之。
readdir则循序读取目录中的资讯。

以下是个标准例。
--------------------------------------------------------------------------------
#include <sys/types.h>
#include <direct.h>
char ** dirGetInfo(const char *pathname)
{
char ** filenames;
DIR * dir;
struct dirent * ent;
int n = 0;
filenames = (char **)malloc(sizeof(char*));
filenames[0]=NULL;

dir = opendir(pathname);
if (!dir) return filenames;
while ((ent = readdir(dir))) {
filenames = (char**)realloc(filenames,sizeof(char*)*(n+1));
filenames[n] = strp(ent->d_name);
n++;
}

closedir(dir);
filenames = (char **)realloc(filenames,sizeof(char*)*(n+1));
filenames[n] = NULL;
return filenames;
}

阅读全文

与linuxrewind函数相关的资料

热点内容
iphone5移动卡贴 浏览:990
电脑文件的格式 浏览:127
extjs的xtype 浏览:959
suse11iso文件要u盘安装 浏览:153
如何将报表统计数据转化为图形 浏览:444
如何寄快递材料文件 浏览:265
java构造方法private 浏览:475
手机文件找回恢复 浏览:516
word怎么把u盘里的文件拔掉 浏览:976
港版苹果用的插排 浏览:1000
雕刻机编程去哪里学 浏览:436
编程怎么与steam教育融合 浏览:697
js制作鼠标拖拽小块 浏览:310
将图纸拆分为多个CAD文件 浏览:779
如何鉴别dsd文件 浏览:902
thinkphp不能用js 浏览:664
苹果11粘腻app是什么意思 浏览:670
安卓手机中木马了怎么办 浏览:964
java组建模型 浏览:53
wifi万能密码安全吗 浏览:785

友情链接