導航:首頁 > 編程系統 > 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函數相關的資料

熱點內容
滑鼠右鍵的壓縮文件 瀏覽:44
awr導出cad文件 瀏覽:925
參公文件去哪裡找 瀏覽:827
excel表批量日期設置成文件夾存放 瀏覽:90
如何把資料庫加入其中 瀏覽:661
編程除法怎麼取消取整 瀏覽:625
股票編程軟體哪裡有賣 瀏覽:503
access導入多個txt文件 瀏覽:917
大臉app安卓下載 瀏覽:439
怎麼休改文件名 瀏覽:989
cdr導出圖片不顯示文件名 瀏覽:761
pcdmis如何離線編程 瀏覽:201
微信推文插入文件 瀏覽:844
生產文件櫃圖片 瀏覽:244
如何設置微信數據增加 瀏覽:95
excel文件不再增大 瀏覽:613
ug編程刀路怎麼復制不用滑鼠選取 瀏覽:97
excel文件打開擴展名錯誤 瀏覽:10
阿里巴巴怎麼做數據包 瀏覽:442
無線網路連接一直顯示未連接 瀏覽:25

友情鏈接