Opening a file in C++

Started by
9 comments, last by tychon 18 years ago
I've been trying to open a text file in Cpp using ifstream but for some reason, it doesn't seem to be working. I don't get any errors or warnings when I compile it but when I run the program, I just get a blank console window. If the program works, it should say 'press any key to continue' but it only says this AFTER I've clicked the X button to close the window. I've got an if statement that returns the message 'Sorry, the file could not be opened' if the file isn't open but it doesn't even display that so I'm not completely sure whether the file is opening. Here is my code for opening the file: =================================================== int main(int argc, char **argv, char *filept[]) { string line; bool output = true; ifstream file (filept[1]); //Make the pointer point to the vertex file file.open("vertexNumbers.txt", ios::in); //Open the vertex file if (file.is_open()) =================================================== Thanks, Mooncinder
"I have gamed for ninety-eight hours and cannot feel my teeth." - Ethan, Ctrl+Alt+Del
Advertisement
In the ifstream constructor you are opening the filename passed in via the commandline args. In the next .open() statement, you open up a different file, implicitely closing the file which you opened in the constructor.

That is not a legal version of main(). You will need to remove the 3rd parameter altogether.

In pseudo-code, what is it that you are trying to accomplish?
Quote:Original post by taby
That is not a legal version of main(). You will need to remove the 3rd parameter altogether.


If I remember correctly, main() is legal so long as it returns int and is not called by anything but the system. main( int argc, char* argv[] ) is just one form that is so common that it's mentioned in the standard -- other parameters are acceptable, though aren't specially handled like argc and argv.

Quote:Original post by Mooncinder
int main(int argc, char **argv, char *filept[]){string line;bool output = true;ifstream file (filept[1]); //Make the pointer point to the vertex filefile.open("vertexNumbers.txt", ios::in); //Open the vertex fileif (file.is_open())


I'm honestly not sure what you mean by the line of "Make the pointer point to the vertex file". Passing filept[1] to an ifstream constructor just tells it to open the file that is named in filept[1]. It doesn't actually do anything to filept[1] itself. What you're probably looking for is std::ifstream file ( argv[1] ).
BEGIN

SET pointer to point to filename
OPEN filename

IF filename is open
(edit file)
ELSE OUTPUT "File can't be opened."

END

This is basically what I'm trying to do in pseudo-code. If I remove 'char *filept[]' from int main, I get a 'debug assertion failed' error when I run the program. When I removed the file.open line, there were no errors, I just got the 'file could not be opened' line in the console window. I'm just trying to find out why the file won't open.

Thanks for replying,
Mooncinder
"I have gamed for ninety-eight hours and cannot feel my teeth." - Ethan, Ctrl+Alt+Del
Surely if you have other parameters in addition to argc and argv, they are pointing at unallocated stack space that will be overwritten by locals and suchlike?
Quote:Original post by EasilyConfused
Surely if you have other parameters in addition to argc and argv, they are pointing at unallocated stack space that will be overwritten by locals and suchlike?


Unless they're a special parameter that's handled by your compiler/OS, pretty much, yes. I can't remember any off hand, but I do remember some systems having special parameters for main().
Thanks Tychon. That line was actually from my lecturer's example code which makes me worry a bit considering he's supposed to know what he's talking about! I tried using the line you suggested but I got a 'debug assertion error'. If anyone has any idea what could be causing this kind of error, it would be greatly appreciated.

Thanks,
Mooncinder
"I have gamed for ninety-eight hours and cannot feel my teeth." - Ethan, Ctrl+Alt+Del
Quote:Original post by Mooncinder
BEGIN

SET pointer to point to filename
OPEN filename

IF filename is open
(edit file)
ELSE OUTPUT "File can't be opened."

END


Why bother setting a new pointer when you already have all the input you need in argv? Try this out, passing an existing filename to the program when you run it.

#include <iostream>#include <fstream>int main( int argc, char *argv[] ){	std::ifstream file( argv[1] );	if ( !file )		std::cout << "Failure!";	else		std::cout << "Success!";}


Quote:Original post by Mooncinder
BEGIN

SET pointer to point to filename
OPEN filename

IF filename is open
(edit file)
ELSE OUTPUT "File can't be opened."

END

This is basically what I'm trying to do in pseudo-code. If I remove 'char *filept[]' from int main, I get a 'debug assertion failed' error when I run the program. When I removed the file.open line, there were no errors, I just got the 'file could not be opened' line in the console window. I'm just trying to find out why the file won't open.

Thanks for replying,
Mooncinder


What do you mean set pointer to point to filename? An ifstream isn't a pointer... It's a class...

When you open something you just open it with the constructor by supplying file-name or by using the "open()" method. There is not "setting" file-names and setting file-name pointers...

You should be fine with just the OPEN filename without the pointer part...
Thanks agi_shi, I guess I was just trying to make sense of my lecturer's dodgy code and failing miserably. I got rid of the filept pointer and it opened the file. It's not reading all of it yet but I'm definitely a lot closer to what I'm trying to do now.

Thanks a lot everyone! :)
"I have gamed for ninety-eight hours and cannot feel my teeth." - Ethan, Ctrl+Alt+Del

This topic is closed to new replies.

Advertisement