try for read file

sorry, my english is not good

hi, guys..
sorry... i have problem...
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
158
159
160
161
162
163
#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 if(com=="")
        {
            getch();
            continue;
        }
        else{
            cout<<"command not found\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, ios::in | ios::binary);
    
    if(file.is_open())
    {
        
        while(!file.eof())
        {
            file.get(show);
            cout<<show;
        }
        file.close();
    }
    else{
        cout<<"   cannot reading file"<<endl;
        return 1;
    }
    file.close();
    return 0;
    
}
            
int create(char name[])
{
    char teks[9999];
    char* input;
    ofstream file;
    char c=0;
    
    location=strcat(location,name);
    file.open(location, ios::out | ios::binary);
    
    if(!file)
    {  
        cout<<"cannot opened files..\n";
        return 1;
    }
    else {
        do 
        {
            gets(teks);
            file<<c<<teks<<endl;
        } while((c = getchar()) != EOF);
        file.close();
    }
    
    cout<<"   file created..."<<endl<<endl;
    return 0;
}

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 0;
}


i'm making a program who can create a file and read it or remove it...base on user input.
but i have a problem when I input for create a file like this

create asd.txt


the program can create a file, but when I try to read/remove after I create it, like this


read asd.txt


the program cannot read it..
why? please help.. this is for my project in my school (really)..
thanks..
closed account (o3hC5Di1)
Hi,

Can you tell us a bit more on the problem please?

What's the program output or compiling error you get?
When you open he asd.txt file in a text editor, what do you see?

All the best,

NwN
Topic archived. No new replies allowed.