Yin kundayen adireshi a C++ ya fi aikin banza kawai. Yana zurfafa cikin ayyuka masu rikitarwa a cikin tsarin ƙididdigewa kuma yana gabatar da aiki mai mahimmanci na sarrafa tsarin fayil. Ma'amala da kundayen adireshi - ƙirƙira, gogewa ko karantawa - wani muhimmin sashi ne na shirye-shiryen matakin-OS. Fahimtar 'mkdir', yadda yake aiki, da cikakkun bayanansa sun tabbatar da cewa suna da fa'ida ga kowane mai mu'amala da shirye-shiryen C++.
C++ ba shi da goyan bayan ƙasa don ƙirƙirar kundayen adireshi, don haka muna buƙatar amfani da takamaiman ayyuka na dandamali. Hanyar gama gari don yin wannan ita ce amfani da aikin mkdir da aka samar ta hanyar ma'aunin POSIX, wanda galibi ana samunsa akan tsarin UNIX.
#include
#include
int main ()
{
idan (mkdir ("sabon_directory", 0777) == -1)
{
perror ("ba zai iya ƙirƙirar kundin adireshi ba");
dawo 1;
}
dawo 0;
}
Bincika aikin `mkdir` a Zurfi
The An bayyana aikin mkdir a cikin sys/stat.h da sys/types.h fayilolin rubutun shine abin da muka yi amfani da shi. Manufarsa ita ce ƙirƙirar sabon kundin adireshi mai suna a matsayin 'new_directory'. Idan aikin ya ci karo da kowane batu yayin tsarin halitta, ya dawo -1.
A cikin lambar mu, idan mkdir ya dawo -1, muna buga saƙon kuskure da ya dace ta amfani da aikin ta'addanci. Lambar 0777 da aka wuce zuwa mkdir ita ce izinin shiga da muke so mu saita don sabon kundin adireshi. Yana nufin cewa kowa zai iya karantawa, rubuta, da aiwatar da fayiloli a cikin kundin da muka ƙirƙira.
Matsayin Dakunan karatu a cikin Aikin mkdir
The sys/stat.h da sys/types.h fayilolin kai samar da maɓalli iri-iri na alama da iri, da ayyuka don sarrafa tsarin fayil. Haɗe da waɗannan a cikin lambar mu tana ba da damar amfani da takamaiman madaidaitan ayyuka da ayyuka a cikin ayyukan mu na mkdir.
Don ƙirƙirar kundayen adireshi a cikin C++, yana da mahimmanci a lura cewa babu wani ginanniyar ayyuka don yin hakan. Don haka, yin amfani da dakunan karatu da kyau yana da mahimmanci don cimma sakamakon da ake so. A wasu lokuta, masu haɓakawa na iya yin amfani da haɓakawa :: ɗakin karatu na tsarin fayil, wanda ke ba da ayyuka masu ɗaukuwa don sarrafa hanyoyi, fayiloli, da kundayen adireshi.
Sauran Ayyuka
Bayan POSIX, Hakanan zaka iya amfani da kiran API na asali, wanda ya bambanta dangane da tsarin aiki na asali.
Ɗayan irin wannan madadin zai iya zama aikin 'CreateDirectory' a cikin Windows API:
#include
int main ()
{
idan (!CreateDirectory("new_directory", NULL))
{
dawo 1;
}
dawo 0;
}
Irin waɗannan ɗakunan karatu ko APIs suna ba da ayyukan da suka wajaba don aiwatar da ayyuka masu rikitarwa, tare da daidaita tazarar inda iyawar C++ ba ta wadatar da farko ba. Don haka, kowane ɗakin karatu ko API yana da keɓantacce kuma yana ba da kusurwoyi daban-daban don magance sarrafa adireshi a cikin C++.