PDA

View Full Version : سوال: ساختن یک فولدر



Netsky
سه شنبه 19 خرداد 1388, 16:28 عصر
سلام دوستان . من تازه وارد دنیای CPP شدم . بنابراین نیازمند یاری شما دوستان عزیز هستم . :گریه::گریه::گریه:
من میخام یه فولدر در یه درایو بسازم .
لطفا کد مورد نظر برای انجام این کار رو برام بزارید .
با تشکر .........................

mehdi_doraghi
سه شنبه 19 خرداد 1388, 16:59 عصر
سلام NetSky
این هم سورس برنامه
Files and Folders using Data structure
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<dir.h>
#include<dos.h>
struct node
{
char *name; /* used to get file / folder name. */
int attrib; /* used to get it's attribute. */
struct node *next; /* concept of Linked list */
};
void main()
{
struct node *head,*head1;
struct node *list,*list1;
struct node * place(struct ffblk ff,struct node *first,int don);
void display(struct node *first);
void print(struct node *list,int *i);
int i,c,c1,done,done1;
struct ffblk f,f1;
head=NULL;
head1=NULL;
clrscr();
done=findfirst("*.*",&f,FA_DIREC|FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH);
/* struct variable "f" contains all files and folders information */
done1=findfirst("*.*",&f1,FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH);
/* struct variable "f1" contains all files information */
head=place(f,head,done); /* content of f is placed in struct head */
display(head);
/*
Note : f contains name of files and folders with their attributes
in f.ff_name, f.ff_attrib which is assigned to name, attrib in
the struct node
*/
printf("
*************************************************
");
getch();
head1=place(f1,head1,done1); /* content of f1 is placed in struct head1
*/
display(head1);
/*
Note : f1 contains name of files and folders with their attributes
in f1.ff_name, f1.ff_attrib which is assigned to name, attrib in
the struct node
*/
printf("
*************************************************
");
getch();
i=0;
c1=0;
/*
Here, head and head1 are compared so that we could extract only
the folders.
*/
list=head; /* head is assigned to list */
while(list!=NULL)
{
list1=head1; /* head1 is assigned to list1 */
if(list1==NULL) /* if there are 0 files */
print(list,&i); /* then display content of list */
else
{
while(list1!=NULL)
{
if(strcmp(list->name,list1->name)==0) /* compare list and list1 */
c1=1;
list1=list1->next;
}
if(c1==0) /* if folder found both in list and list1*/
print(list,&i); /* then display content of list */
}
c1=0;
list=list->next;
}
printf("
FOLDERS = %d",i);
printf("
*************************************************
");
printf("
Where,");
printf("
H - Hidden");
printf("
D - Directory");
printf("
R - Read only");
printf("
S - System");
printf("
A - Archive");
getch();
free(list1);
free(list);
free(head);
free(head1);
}
void print(struct node *list,int *i)
{
void property(struct node *list);
/* to display folders other than default folders (. and ..) */
if((strcmp(list->name,"."))!=0 && (strcmp(list->name,".."))!=0)
{
*i=*i+1; /* counts number of folders */
property(list);
printf(" %s
",list->name);
}
}
void property(struct node *list)
{ /* finds their attribute */
if(list->attrib & FA_HIDDEN)
printf("(H)");
if(list->attrib & FA_DIREC)
printf("(D)");
if(list->attrib & FA_RDONLY)
printf("(R)");
if(list->attrib & FA_SYSTEM)
printf("(S)");
if(list->attrib & FA_ARCH)
printf("(A)");
}
struct node * place(struct ffblk ff,struct node *first,int don)
{
static int j;
void create(struct node *first,char *ch,int d);
void insert(struct node *first,char *ch,int d);
int i=0,c=0;
char *p;
if(don!=0)
first=NULL;
else
{
while(don==0)
{
if(i==0)
{
first=(struct node *)malloc(sizeof(struct node));
if ((p = (char *) malloc(14)) == NULL)
exit(1);
strcpy(p,ff.ff_name);
create(first,p,ff.ff_attrib);
i=1;
}
else
{
if ((p = (char *) malloc(14)) == NULL)
exit(1);
strcpy(p,ff.ff_name);
insert(first,p,ff.ff_attrib);
}
don=findnext(&ff);
c=c+1;
}
}
if(j==0)
{
printf("
*************************************************
");
printf("
%d FILES & FOLDERS
",c);
j+=1;
}
else
printf("
%d FILES
",c);
return(first);
}
void create(struct node *first,char *ch,int d)
{
char *p;
if ((p = (char *) malloc(sizeof(ch))) == NULL)
exit(1);
p=ch;
first->name=p;
first->attrib=d;
first->next=NULL;
return;
}
void insert(struct node *first,char *ch,int d)
{
struct node *temp,*list;
char *p;
list=first;
while(list->next!=NULL)
list=list->next;
if ((p = (char *)malloc(sizeof(ch))) == NULL)
exit(1);
p=ch;
temp=(struct node *)malloc(sizeof(struct node));
temp->name=p;
temp->attrib=d;
temp->next=NULL;
list->next=temp;
return;
}
void display(struct node *first)
{
struct node *list;
void property(struct node *list);
list=first;
if(list==NULL)
printf("
NULL");
else
{
while(list->next!=NULL)
{
property(list);
printf("%s %d
",list->name,list->attrib);
list=list->next;
}
property(list);
printf("%s %d
",list->name,list->attrib);
}
return;
}

Nima_NF
سه شنبه 19 خرداد 1388, 17:30 عصر
ساخت فولدر و امثال آن به سیستم عامل و کامپایلر بستگی دارد. پس حتما کامپایلر خود را ذکر کنید.

tdkhakpur
سه شنبه 19 خرداد 1388, 18:27 عصر
سلام
کد زیر شاید بهت کمک کرد.


int mkdir(const char *path);
int _wmkdir(const wchar_t *path);

فایل dir.h رو include کنید

kitten
سه شنبه 19 خرداد 1388, 20:41 عصر
سلام دوستان
چه خبره بابا رفیقمون را از سی فراری ندین
دوست عزیز این طوری هم می شه :

#include<string.h>
system("mkdir c:\myfolder");
البته فقط برای ویندوز

Netsky
چهارشنبه 20 خرداد 1388, 10:44 صبح
سلام .
دوستان من با ویژوال سی ++ کار میکنم .
با تشکر ...............

amir_civil
چهارشنبه 20 خرداد 1388, 12:56 عصر
سلام
یه فولدر ساختن که دیگه این همه داستان نداره


BOOL WINAPI CreateDirectory(
__in LPCTSTR lpPathName,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes
);



#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
#pragma comment(lib,"kernel32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
::CreateDirectoryA("C:\\Amir30vil",NULL);
return 0;
}

ali_ahr7
چهارشنبه 15 دی 1389, 01:22 صبح
سلام
کد زیر شاید بهت کمک کرد.


int mkdir(const char *path);
int _wmkdir(const wchar_t *path);

فایل dir.h رو include کنید

سلام لطفا بگيد دقيقا چطور ميشه ازش استفاده كرد؟

r00tkit
چهارشنبه 15 دی 1389, 10:28 صبح
سلام



علی رضا همون جوری که تو C# برای ساخت فولدر از تابع /کلاس های استفاده می کردی اینجا هم باید همین کار رو بکنی ولی می تونی مستقیم از API استفاده کنی یا کد MFC بزنی ( که این mfc یه لایه برای api هستش )

تو باید ( این چیز هایی که ایست می کنم شاید یعضی قسمت هاش یکی باشن)


syntax( برای C/C++)
windows programming چیز هایی مثل handle و نوع های ویندوز و طریقهی اسم گذاری نوع ها مثل P ,T, W
خیلی از مفاهیم مثل API MFC چیه و....
از همه مهم تر سرچ کردن رو یاد بگیری : مثل این (http://www.google.com/search?hl=en&client=firefox-a&hs=dj3&rls=org.mozilla%3Aen-US%3Aofficial&q=mfc+create+directory&aq=f&aqi=&aql=&oq=&gs_rfai=)و این (http://www.google.com/search?hl=en&client=firefox-a&hs=o5i&rls=org.mozilla%3Aen-US%3Aofficial&q=c%2B%2B+create+directory+&aq=f&aqi=&aql=&oq=&gs_rfai=)

کتاب پیشنهادی من windows via c/c++ هستش البته قبلش یه کتابی مثل the c programming language و C++ Primer

بعد بیا سمت

Directory::CreateDirectory

یا
CreateDirectory(L"d:\\Geek1982",NULL);
RemoveDirectory(L"d:\\Geek1982");
MoveFile(L"d:\\Geek1982",L"d:\\myfolder\\Geek1982");


----
از این به بعد کلی با هم کار داریم منم خیلی وقته از C# حافظی کردم