I'm starting to learn C++, and I wrote this program as a test:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char *cString;
ifstream File("file.txt");
File >> cString;
cout << cString << endl;
system("PAUSE");
return 0;
}

And when I try to run it it gives me an error and says the program needs to close... Anyone know whats wrong?

(ps. File.txt has "Hello World!" in it)

Recommended Answers

All 5 Replies

The error would be helpfu for us to help you, but lemme give this crystal ball a try...

You never initialize cString, so it's just pointing to some random place in memory. Then you try to read from the file into that place in memory. When that happens, you get an access violation (in VC++, it probably says something about address 0xC0000005 or something). You need to initialize cString before you write to it.

Might I ask why you don't just the std::string class? It would allow you to proceed without the hassle of initializing the variable (initializing is a good habit though), and it would also make sure you don't read in something that would cause a buffer overflow. Like if someone had a text file with the word pneumonoultramicroscopicsilicovolcanoconiosis, if you're expecting something shorter it's fairly likely that your char* won't be big enough and you'll get another access violation.

The error is a windows error where it says the application encountered an error, and you can send a report or not. As for the cString, I've never neede to intialize it before. Also what library has the std::string function?

The error is a windows error where it says the application encountered an error, and you can send a report or not. As for the cString, I've never neede to intialize it before.

Depends what you're trying to do with it. As you know, the pointer can't do anything legally unless it's pointing to allocated memory. So if the function you're using it with doesn't allocate memory, boom! It crashes.

Also what library has the std::string function?

It's in the standard template library. Just do something like:

#include <iostream>
int main() {
   std::string something;

   // rest of program...
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string sString;
ifstream File("file.txt");
File >> sString;
cout << sString << endl;
system("PAUSE");
return 0;
}

Alright I changed it to this, now it doesn;t give an error, however it still doesn't display anything...

You've probably got some whitespace or some weird charecters at the beginning of your file that's confusing your program. Consider creating a loop which keeps reading data from the ifstream until the member function eof() returns true.

Just so that you know, even if the program did read it in, it would only read in "hello" because there's a space between "hello" and "world" - the >> reads until the next space or newline. You'll want the getline() function if you've got more than 1 word to read in (which is a better method than using >> anyway).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.