I'm trying to open a file, using the ifstream function. I'm using a .jpg image as an example, but it doesn't work, any ideas?


Thank you.

int main(int argc, char *argv[])
{
char file[20];
strcpy(file,"C:\\Fraps\\1.1.jpg");           
ifstream outfile;
outfile.open(file);
return 0;
}

1. if you're using ifstream (meaning input stream) why did you name it outfile instead of infile?

2. what do you mean by "it doesn't work" ? The only reason I know of why your code will not open the file is if there is no such file as "1.1.jpg" in that directory. Its unusual to use two dots in a filename, but its still a legal filename on MS-Windows file system.

I tried what you said, and it didn't work. Any ideas?

Edit: I ran the program, and it didn't open the file. Dispite doing as Ancient said

Here it is...

using namespace std;

int main(int argc, char *argv[])
{
char file[20];
strcpy(file,"C:\\Fraps\\document.txt");         
ifstream infile;
infile.open(file);
return 0;
}

Please stop saying "I tried it and it doesn't work" ! what did you do ? I can't see your monitor from where I'm sitting so you will have to post code, and also post the error statement(s) you compiler puked out at you.

Yes, I've extended the char. amount. I'm confused dave, I thought this function is supposed to open the file? Still no luck

What do you mean by open?

The file is opened, you do nothing, it closes. Whoo-hoo. What are you expecting?

Do you think it's supposed to launch an image viewer or a text editor? What?

What is the point in running the program then, if you can't see the program open?

cosmos22 you are now acting like a troll. If you can't describe what you expect to happen when you open a file, then no answer will be sufficient.

What are you expecting?!

What is the point in running the program then, if you can't see the program open?

The open() method only makes the file available for reading or writing by your program. It does nothing else. You have to write additional code to read the file into memory so that your program can do something with it. That could be as little as 5 or 6 lines of code or it could be as much as thousands of lines of code depending on what YOU want your program to do. Programs don't do things magically, you have to write the code to make that happen.

This article has been dead for over six months. Start a new discussion instead.