help create read delete file
May 22, 2012 at 1:22pm UTC
hi I have problem about my program this is code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <direct.h>
using namespace std;
int create(char name[]);
void welcome();
int hapus(char name[]);
int baca(char name[]);
char * location;
int main()
{
//menset title pada jendela aplikasi
SetConsoleTitle(" " );
string com;
char param1[100];
char param2[100];
char fldr[100]="\\file\\" ;
char pathprog[MAX_PATH];
getcwd(pathprog, MAX_PATH);
location=strcat(pathprog,fldr);
mkdir("file" );
welcome();
do {
cout<<"user/cmd > " ; cin>>com;
if (com=="create" )
{
cin>>param1;
create(param1);
}
else if (com=="remove" )
{
cin>>param1;
hapus(param1);
}
else if (com=="exit" )
{
exit(0);
}
else if (com=="read" )
{
cin>>param1;
baca(param1);
}
else {
cout<<"command not found\n\n" ;
}
}while (true );
return 0;
}
void welcome(){
cout<<" +---------------------------------------+\n" ;
cout<<" | WELCOME |\n" ;
cout<<" | textman v.1 (developer preview) |\n" ;
cout<<" +---------------------------------------+\n" ;
cout<<" command :\n" ;
cout<<" \to> create file\n" ;
cout<<" \to> rename file\n" ;
cout<<" \to> remove file\n" ;
cout<<" \to> read file\n" ;
cout<<" \to> enable file\n" ;
cout<<" ---------------------------------------------\n\n" ;
}
int baca( char name[]){
ifstream file;
char show;
location=strcat(location, name);
file.open(location);
if (file.is_open())
{
while (!file.eof())
{
file.get(show);
cout<<show;
}
file.close();
}
else {
cout<<" cannot reading file" <<endl;
file.close();
}
return 1;
}
int create(char name[])
{
char teks[9999];
char * input;
FILE* files;
char c=0;
location=strcat(location,name);
files=fopen(location, "w" );
if (!files)
{ cout<<"cannot opened files..\n" ;
return 1;
}
else {
do
{
gets(teks);
input=strcat(teks, "\n" );
fputc(c, files);
fputs(input, files);
} while ((c = getchar()) != EOF);
}
fclose(files);
cout<<" file created..." <<endl<<endl;
return 1;
}
int hapus(char name[])
{
ifstream file;
int result;
location=strcat(location, name);
result=remove(location);
if ( result != 0 ){
perror( " Error deleting file" );
cout<<endl;
return 1;
}
else
puts( " File successfully deleted\n" );
return 1;
}
the problem is when I created a file I can't to remove or read that file and other file too.
so summary of my problem is I cannot do anything (in this case read and remove)
after I create a file..
please help me...
thanks..
Topic archived. No new replies allowed.