{Beginner}{G++} Having problems with some "char"s

I have written this program which demonstrates how to access files using C++. But when I compile it (using g++) I get a few errors which are out of my understanding. Please help me with it. I am not asking to debug, but to please explain what those errors mean and how would I remove them.
Here is the source 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
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <fstream>
using namespace std;


const char NULLSTRING = ' ';

char filename[255] = NULLSTRING;
char text[16384]=NULLSTRING;
void Prompt();
void New();
void Existing(char*);
void Clear();
void Exit();


int main()
{
	Prompt();
	return 0;
}


void Clear()
{
	system("clear");
}

void Welcome()
{
	Clear();
	cout<<"Welcome."<<endl;
	cout<<"This is a demonstration of reading and writing to files using C++"<<endl;
	cout<<"For better understanding read the source code."<<endl;
	cout<<"Hit any key to launch the demo."<<endl;
	cout<<"\n..."<<endl;
	getchar();
	Prompt();
}

void Prompt()
{
	char choice;
	cout<<"Please choose from following:\n"<<endl;
	cout<<"1: Create New File"<<endl;
	cout<<"2: Display Contents of an Existing File"<<endl;
	cout<<"0: Quit program\n"<<endl;
	choice = getchar();
	switch (choice)
	{
	case '1':
		New();
	break;
	case '2':
		Existing(filename);
	break;
	case '0':
		Exit();
	break;
	default:
		cout<<"Wrong choice entered."<<endl;
		Prompt();
	break;
	}
}

void New()
{
	char readoption;
	Clear();
	cout<<"Please enter name of the file."<<endl;
	gets(filename);
	cout<<"Now you can enter text for the file."<<endl;
	cout<<"You text can include all characters except ~"<<endl;
	cout<<"When finished, type ~ and press Return."<<endl;
	cout<<"\n"<<endl;
	cout<<"~"<<endl;
	getline(text, 16384,'~');
	ofstream fout;
	fout.open(filename);
	fout<<text<<'~'<<endl;
	fout.close();
	Clear();
	cout<<"Do you want to read contents of your file now? (y/n)\t:";
	readoption=getchar();
	switch(readoption)
	{
	case 'y':
		Existing(filename);
	break;
	case 'n':
		filename = NULLSTRING;
		Prompt();
	break;
	default:
		cout<<"Invalid Option, Operation Cancelled, Back to Menu."<<endl;
		filename = NULLSTRING;
		Prompt();
	break;
	}
}

void Existing(char nameasarg)
{
	if (nameasarg = NULLSTRING)
	{
		cout<<"Please specify filename."<<endl;
		gets(filename);
		Existing(filename);
	}
	else
	{
		ifstream fin;
		fin.open(nameasarg);
		if(fin.fail())
		{
			cout<<"Sorry! This file doesn't exist. Check Spelling."<<endl;
			filename = NULLSTRING;
			Existing(filename);
		}
		else
		{
			getline(fin, text, '~');
			cout<<"Press any key to return to main menu."<<endl;
			getchar();
			Prompt();
		}
	}

}

void Exit()
{
	exit(0);
}


All of the errors are about char variables and conversions... For convenience I am also posting the O/P of G++.

main.cpp:11: error: array must be initialized with a brace-enclosed initializer
main.cpp:12: error: array must be initialized with a brace-enclosed initializer
main.cpp: In function ‘void New()’:
main.cpp:81: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
main.cpp:95: error: incompatible types in assignment of ‘const char’ to ‘char [255]’
main.cpp:100: error: incompatible types in assignment of ‘const char’ to ‘char [255]’
main.cpp: In function ‘void Existing(char)’:
main.cpp:117: error: invalid conversion from ‘char’ to ‘const char*’
main.cpp:117: error: initializing argument 1 of ‘void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]’
main.cpp:121: error: incompatible types in assignment of ‘const char’ to ‘char [255]’
main.cpp:126: error: invalid conversion from ‘void*’ to ‘char**’
main.cpp:126: error: cannot convert ‘char*’ to ‘size_t*’ for argument ‘2’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
8
9
10
11
const char NULLSTRING = ' ';

char filename[255] = NULLSTRING;
char text[16384]=NULLSTRING;
NULLSTRING isn't a string, it's a single character

Line 81,126 getline(text, 16384,'~'); http://www.cplusplus.com/reference/iostream/istream/getline/

Line 95 filename = NULLSTRING, Line 100,121 filename = NULLSTRING;, NULLSTRING is a character, you can't use assignment with arrays

Line 117 fin.open(nameasarg); nameasarg is a single char

Ok... I got what the errors mean. But I am not able to manage a workaround. I have changed NULLSTRING to a string but it returns a similar error that conversion from string to character-array is not possible. (By the way what is the difference between a string and an array of characters?)

I also tries to change NULLSTRING form const char to char[16] but it says the arrays should be initialized inside braces. That cannot be done here since I want to keep the scope of NULLSTRING global... Enclosing it in braces would limit the scope.

Well I read the getline() manual but the problem is still the same. I don't get where am I doing the mistake... char* is a pointer I understand that. But what is a char**? I have seen this type of variable first time in the definition of getline().

The further errors in Line 95, 100, 121 are associated with the variable type of NULLSTRING and I have already mentioned what I have tried.

The last error, again, how do I use namesarg[] as an argument to the function?

Will be really thankful if you could help.
By the way what is the difference between a string and an array of characters?
This may be a point to clarify.
In C, strings are pointers to null-terminated sequences of characters ( character arrays ) Which are quite cumbersome to use.
http://www.cplusplus.com/doc/tutorial/ntcs/
C++ standard library defines std::string which is much more flexible
http://www.cplusplus.com/reference/string/string/
I would strongly recommend you to switch to std::strings, your code would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string filename; // you don't have to worry about the size
string text; // you don't need to initialize it to an empty string as it's done automatically

//...

cout<<"Please enter name of the file."<<endl;
getline(cin,filename);

//...

  case 'n':
    filename = "";
    Prompt();

//...

void Existing(const string &nameasarg) // by const reference = faster than by value with objects

//...

fin.open( nameasarg.c_str() ); // c_str retursn a C string from the std::string 



I also tries to change NULLSTRING form const char to char[16] but it says the arrays should be initialized inside braces. That cannot be done here since I want to keep the scope of NULLSTRING global... Enclosing it in braces would limit the scope.
The compiler means this:
char NULLSTRING[16] = { ' ' };
Hi there buddy... I tried the tricks you told me. I must tell you that strings have always freaked me out... I don't know most people consider them to be one of the easiest parts of C++ but since the time I learnt C++, I have been dreading strings. And the problem is, one way or the other, I always come across strings in my practice. So I really need to get over stringophobia very soon, LOLxxx.

Anyway, I did as you told. Then compiled it. Last time, before I had followed your latest advice, the compiler said I can't convert from char to char* in functions New() and Existing()... Now, as the char[] have been changed to string, the compiler is returning a similar error saying that I cannot convert from std::string to char*... I have changed the argument of Existing() to const string &var but it is still taking it as char*... I am not getting much of it. I don't want to annoy you actually, but would be glad if you could help me work it thoroughly. I have read the manuals from the links that you gave me but it's all about classes and objects, not that I don't know about them, but I need an overview of strings, not an interview, :) lolxxx.

Again apologizing if I am bothering you.
The best way would be changing everything to a string. You can ignore the const& thing if you want.
1
2
3
4
void Existing(string nameasarg);

string s = "foobar";
Existing ( s );

If you really want to have C string parameters, you can do like this:
1
2
3
4
void Existing(char *nameasarg);

string s = "foobar";
Existing ( s.c_str() );
Can #include string handle this or do I have to #include cstring ?
I think <string> already includes <cstring> but anyway, you don't need cstring
Bazzy, you cannot rely on such include file dependencies. Header dependencies are implementation specific. For maximum portability, one should excplicitly include every header from which you use symbols.
Got it... I think structmem.c_str() works without #include <cstring> ... But I have included it anyway.
Topic archived. No new replies allowed.