passing string variable into char Array

I have program where i have to check to see if file exist, if it does not then it needs to be created. SO I have a read file that works fine, if th efile exist it reads whats in it, if it does it says the file does not exist. Now Im trying to creata function that creates the file if it doesnt exist. so in my read function when the person enters the name of the file to be checked for..I pass that name to a variable called name..Hoping that I could then pass it into my create file function if it does not exist..and use that variable to pass the name they entered into the createfile array..called filename.. but I am having trouble because i get error when i try to pass from a string name to char array.. even when I change the varialbe name to char, or char [256] it will not work.. I try to fing a way to convert th string to a char using the strncopy function but still no dice..here the code i have for the createfile funciton

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  void CreateNew(ofstream & FileNew)//Create file
{
    char filename[256];
    string name;
   // char newname;
   // cout<<"Enter file name to create: ";
 //cin.getline(filename, 256);
 strcpy(filename,name.c_str());
 // filename = name;
    FileNew.open(filename);
    name = filename;
    if(FileNew.is_open())
    {
    	cout<<"\n";
        cout<<name<<".txt file is successfully created!\n";
		cout<<"\n";
		fileyes=0;
		
    }
    else
        cout<<"can not open a file...\n"<<endl;
}
Seems like you are doing it the hard way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  void CreateNew(ofstream & FileNew)//Create file
{
    string name;
    //get filename via getline
    FileNew.open(name.c_str());
    if(FileNew.is_open())
    {
    	cout<<"\n";
        cout<<name<<".txt file is successfully created!\n";
		cout<<"\n";
		fileyes=0;
		
    }
    else
        cout<<"can not open a file...\n"<<endl;
}


[edit] Though I would see if it is open first and if so close it before opening a new file.

Last edited on
Ok I tried that but its still didnt work..maybe something is wrong with my main function.
Here is the all the code..Please ignore the extra variable, I plan to add more to the code using those variables.

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

//Darius Dempsey
//CIS215
//Create a file, append to it, and read it.

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew);
void ReadNew(ifstream & FileGet);
void AppendNew(ofstream & FileApp);
int response;
int choice;
char trash[2];
char ename[256];
int fileyes=0;
int s=1;
int i=0;
string f;

//Main
int main()
{	ofstream FileCreate;
    ifstream FileRead;
    ofstream fileAppend;
   
	cout<<"Do you want to find information about an employee? (0 for yes, 1 for no) ";
	cin>>response;
	cin.getline(trash, 2);
	while(response == 0)
	
	{ 
		ReadNew(FileRead);
	
		if(fileyes==1)
		{
			CreateNew(FileCreate);
		}	
		cout<<"fileyes = "<<fileyes<<endl;	
			
	cout<<"enter another name (0 for yes, 1 for no)  ";
	cin>>response;
}	
			

    return 0;
}



//functions

/*void CreateNew(ofstream & FileNew)//Create file
{
    char filename[256];
    string name;
   // char newname;
   // cout<<"Enter file name to create: ";
 //cin.getline(filename, 256);
 strcpy(filename,name.c_str());
 // filename = name;
    FileNew.open(filename);
    name = filename;
    if(FileNew.is_open())
    {
    	cout<<"\n";
        cout<<name<<".txt file is successfully created!\n";
		cout<<"\n";
		fileyes=0;
		
    }
    else
        cout<<"can not open a file...\n"<<endl;
}*/


void ReadNew(ifstream & FileGet)// Read file
{   char filename[256];
	string name;
	char newname;
    string line;
    fileyes=0;
   cout<<"Enter file name to extract info.: ";
   cin.getline(filename, 256);
   name = filename;
    FileGet.open(filename);
    if(FileGet.is_open())
    {
        while(1)
        {
            getline(FileGet, line);
            cout<<line<<endl;

            if(FileGet.eof())
                break;
        }
    }
    else
    	fileyes=1;
       cout<<"read1\n ";
}

 void CreateNew(ofstream & FileNew)//Create file
{
    string name;
    FileNew.open(name.c_str());
    if(FileNew.is_open())
    {
    	cout<<"\n";
        cout<<name<<".txt file is successfully created!\n";
		cout<<"\n";
		fileyes=0;
		
    }
    else
        cout<<"can not open a file...\n"<<endl;
}
You must give it a filename line 110 is undefined until you read in a file.

Try adding this after 110 and before 111.
1
2
std::cout << "Please enter a filename: ";
std::getline(std::cin, name);

that kinda defeats the purpose..I dont want ot ask twice.. the project requires me to write a program that ask for the name of the file, if the file does not exist then it creats the file.. If I add the cod eyou just wrote, I will be asking the for the name of the file twice.. I can only ask once..
if I could ask for it one time around line 37 and 38..and then pass it to a variable ..."name"... and then use that in both funcitons to pass to the filename array.. that would be great..either way i can only ask for the file name once..and then check to see if it exist if it does not..then i have to create the file..
I would consider restructuring. The read and create functions both need a filename so you should pass that as a parameter.
I tried to use a pointer, but it still doesnt work...Can somebody please help me with this.. I triend all kinds of way to make this work, and looked all over cplusplus tutorial, and google other sites..I can't figure this out

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
//Create a file, append to it, and read it.

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew, char *name);
void ReadNew(ifstream & FileGet);
void AppendNew(ofstream & FileApp);
int response;
char trash[2];
int fileyes=0;
char *name[256];

//Main
int main()
{	ofstream FileCreate;
    ifstream FileRead;
    ofstream fileAppend;
   
	cout<<"Do you want to find information about an employee? (0 for yes, 1 for no) ";
	cin>>response;
	cin.getline(trash, 2);
	while(response == 0)
	
	{ 
		ReadNew(FileRead);
	
		if(fileyes==1)
		{
			CreateNew(FileCreate, name);
		}	
		cout<<"fileyes = "<<fileyes<<endl;	
			
	cout<<"enter another name (0 for yes, 1 for no)  ";
	cin>>response;
}	
			

    return 0;
}


I resolved it..This code works

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

//Create a file, append to it, and read it.

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
void ReadNew(ifstream & Fileget, ofstream & FileNew);
int response;
char trash[2];
int fileyes=0;
char *name[256];

//Main
int main()
{	ofstream FileCreate;
    ifstream FileRead;
   
	cout<<"Do you want to find information about an employee? (0 for yes, 1 for no) ";
	cin>>response;
	cin.getline(trash, 2);
	while(response == 0)
	
	{ 
		ReadNew(FileRead, FileCreate);
	
		cout<<"fileyes = "<<fileyes<<endl;	
			
	cout<<"enter another name (0 for yes, 1 for no)  ";
	cin>>response;
}	
    return 0;
}


//functions

void ReadNew(ifstream & FileGet, ofstream & FileNew)// Read file
{   char filename[256];
	string name;
    string line;
    fileyes=0;
   cout<<"Enter file name to extract info.: ";
   cin.getline(filename, 256);
   name = filename;
    FileGet.open(filename);
    if(FileGet.is_open())
    {
        while(1)
        {
            getline(FileGet, line);
            cout<<line<<endl;

            if(FileGet.eof())
                break;
        }
    }
    else
    	fileyes=1;
  	
      FileNew.open(filename);
    name = filename;
    if(FileNew.is_open())
    {
    	cout<<"\n";
        cout<<name<<".txt file is successfully created!\n";
		cout<<"\n";
		
    }
    else
        cout<<"can not create file...\n"<<endl;	
    	
       cout<<"read1\n ";
}

I resolved it, This code works

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
//Create a file, append to it, and read it.

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
void ReadNew(ifstream & Fileget, ofstream & FileNew);
int response;
char trash[2];
int fileyes=0;
char *name[256];

//Main
int main()
{	ofstream FileCreate;
    ifstream FileRead;
   
	cout<<"Do you want to find information about an employee? (0 for yes, 1 for no) ";
	cin>>response;
	cin.getline(trash, 2);
	while(response == 0)
	
	{ 
		ReadNew(FileRead, FileCreate);
	
		cout<<"fileyes = "<<fileyes<<endl;	
			
	cout<<"enter another name (0 for yes, 1 for no)  ";
	cin>>response;
}	
    return 0;
}


//functions

void ReadNew(ifstream & FileGet, ofstream & FileNew)// Read file
{   char filename[256];
	string name;
    string line;
    fileyes=0;
   cout<<"Enter file name to extract info.: ";
   cin.getline(filename, 256);
   name = filename;
    FileGet.open(filename);
    if(FileGet.is_open())
    {
        while(1)
        {
            getline(FileGet, line);
            cout<<line<<endl;

            if(FileGet.eof())
                break;
        }
    }
    else
    	fileyes=1;
  	
      FileNew.open(filename);
    name = filename;
    if(FileNew.is_open())
    {
    	cout<<"\n";
        cout<<name<<".txt file is successfully created!\n";
		cout<<"\n";
		
    }
    else
        cout<<"can not create file...\n"<<endl;	
    	
       cout<<"read1\n ";
}


Topic archived. No new replies allowed.