1-Topshiriq
“C” dasturi yordamida berilgan katalogdagi faylllarni chiqarish dasturini tuzing.Algoritmi
1. Dasturni boshlang
2. Dirent tuzilmasiga o'zgaruvchini e'lon qiling (fayl tizimidan mustaqil katalogni belgilaydi)
va shuningdek DIR uchun
3. Opendir tizim qo'ng'irog'i yordamida ko'rsatiladigan katalog yo'lini ko'rsating
4. Katalog mavjudligini tekshiring va foydalanib katalog tarkibini o'qing
readdir tizim chaqiruvi (ko'rsatgichni keyingi faol katalog yozuviga qaytaradi)
5. Katalogdagi barcha fayllar ro'yxatlanguniga qadar yuqoridagi amalni takrorlang
6. Dasturni to'xtating
#include
#include
#include
#include
#include
#include
int main ()
{
/* mahalliy o'zgaruvchilar, tarkib va ta'riflar */
char path[294]; /* optik yo'l */
DIR *dirP; /* katalogga ko'rsatgich */
int filedes; /* fayl tavsiflovchisini oching */
struct dirent *direntP; /* katalogga kirish tuzilishi */
struct stat info; /* dir / fayl haqida ma'lumot */
int volume_number; /* what it says... */
int rc = 0; /* funktsiyani qaytarish kodlari */
int kk = 0; /* mahalliy hisoblagich */
char data[] = "Tezkor qizil tulki panjara ustidan sakrab tushdi";
/* QOPT pfs root katalogini ochish va katalog yozuvlarini o'qish orqali
QOPT jismoniy fayl tizimidan jildlar ro'yxatini oling. */
memset(path, /* Yo`l nomini o`chirish */
0x00,
sizeof(path));
strcpy(path, /* jismoniy fayl tizimini o'rnatish */
"/QOPT");
rc = stat("/QOPT", &info);; /* fayllar sonini aniqlash */
if (rc != 0)
perror("stat() failed:");
dirP = opendir(path); /* katalogni oching */
if (dirP == NULL)
perror("opendir() failed:");
for (kk = 1; kk <= info.st_nlink; kk++)
{
direntP = readdir(dirP);
if (direntP == NULL)
perror("readdir() failed:");
printf("%d) %s\n", kk, direntP->d_name);
}
// Foydalanuvchiga ishlashni xohlagan hajmini talab qiling va uni joriy katalogga aylantiring.
printf("\nEnter the number the volume you want to work with:\n");
scanf("%d", &volume_number);;
rewinddir(dirP); /* katalogning boshlanishi */
for (kk = 1; kk <= volume_number; kk++)
direntP = readdir(dirP); /*so'ralgan dir. kirish */
strcat(path, "/");
strcat(path, direntP->d_name);
rc = chdir(path); /* joriy ish dir-ni o'rnating. */
if (rc != 0)
perror("chdir() failed:");
if (getcwd(path, sizeof(path)) == NULL)
perror("getcwd() failed:");
printf("\nThe current working directory is: %s\n", path);
rc = closedir(dirP); /* katalogni yoping */
if (rc != 0)
perror("closedir() failed:");
// Faqat fayl yozishni yarating va oching. Agar fayl mavjud bo'lsa, u qisqartiriladi. Egasi o'qish, yozish va faylga vakolatni bajarishi kerak
strcat(path, "/");
printf("\nEnter a file name:\n");
scanf("%s", &path[strlen(path)]);
filedes = creat(path, S_IRWXU);
if (filedes == -1)
{
perror("creat() failed");
return 0;
}
rc = write(filedes, data, sizeof(data));
if (rc == -1)
perror("write() failed:");
close(filedes);
/* Faylni qayta o'qing va chop eting. */
memset(data, 0x00, sizeof(data));
filedes = open(path, O_RDWR);
if (filedes == -1)
{
perror("open() failed");
return 0;
}
read(filedes, data, sizeof(data));
if (filedes == -1)
{
perror("read() failed");
return 0;
}
printf("\nThe data written to file is: %s\n", data);
// Faylga ofsetni o'zgartiring va uning qismini o'zgartiring. Faylni to'liq o'qing, chop eting va faylni yoping.
lseek(filedes, 4, SEEK_SET);
rc = write(filedes, "slow old ", 9);
if (rc == -1)
{
perror("write() failed");
return 0;
}
lseek(filedes, 18, SEEK_SET);
rc = write(filedes, "went under ", 11);
if (rc == -1)
{
perror("write() failed");
return 0;
}
lseek(filedes, 0, SEEK_SET);
read(filedes, data, sizeof(data));
if (filedes == -1)
{
perror("read() failed");
return 0;
}
printf("\nThe data now is: %s\n", data);
close(filedes);
printf("Done...\n");
return 0;
}
Natija:
Do'stlaringiz bilan baham: |