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.

What is the symptom of it 'not working' ? (Please be more precise about failures when you post).

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

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

Nobody said anything, so I have no idea what you tried. Post the code.

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;
}

Oh, I think I see the problem. Look on line 1 of your program, what is it missing? Where is #include <fstream> ?

Sorry, no. Tried that, no luck I'm afraid

cosmos22 -- you're not expecting it to display the image or open a text editor, are you?

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.

<deleted>

What exactly does it do then, if it doesn't open it>?

Define 'open'.

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?

Do you have read access to the file, i.e. you can e.g open it in a text editor?

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

No, how do you go about doing that?

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.

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.