GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Jul-2008, 16:55
Soybeanio Soybeanio is offline
New Member
 
Join Date: Jul 2008
Posts: 8
Soybeanio is on a distinguished road
Question

How can I literally OPEN a file using a C++ program?


All of you here actually really helped me by your answers. I didn't know you even CAN delete files from existence. Or string.find() erase() insert() and replace() functions. They should be useful.

Anyways. By literally OPEN a file, I mean actually having the same result as double-clicking it in windows. I know there is a way to do that, but I don't know how or if it is too complicated.

Thanks for any help, and I hope this is related to the thread.
  #2  
Old 12-Jul-2008, 18:34
Soybeanio Soybeanio is offline
New Member
 
Join Date: Jul 2008
Posts: 8
Soybeanio is on a distinguished road
Question

File directories


From what I know, I can only access a file if it's located in the same directory as the program I'm running. Is there a way to choose it using a directory or something? And if this is related, how can I make folders?

Thanks a lot for any help
  #3  
Old 12-Jul-2008, 23:24
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: How can I literally OPEN a file using a C++ program?


Don't know what you talking here. Can you rephrase your question ?
  #4  
Old 13-Jul-2008, 09:26
Soybeanio Soybeanio is offline
New Member
 
Join Date: Jul 2008
Posts: 8
Soybeanio is on a distinguished road
Smile

Using File Directories in Windows to access Files in a different directory


Sorry for the unclear questions, and sorry for making a new question at an already made thread. Anyways, the first question I don't really need an answer to anyways. This is the second one:

When I write a program, such as the following:

CPP / C++ / C Code:
// testing with files
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    fstream file("test.txt");           // same as file.open("test.txt");   
    file << "this is a test file\n";    // writes to file
}

That would make a file called test.txt which has 'this is a test file' written in it
in the same folder / directory as the program itself because there's nowhere else it was told to make it. My question is how do I tell the program to read, write, or make a file in a different directory from the program? Can I do that?

Thanks for any help
Last edited by admin : 13-Jul-2008 at 15:10. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #5  
Old 13-Jul-2008, 11:52
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Using File Directories in Windows to access Files in a different directory


Quote:
Originally Posted by Soybeanio
My question is how do I tell the program to read, write, or make a file in a different directory from the program?
...by specifying the pathname (either relative or absolute...) as the filename. Consider the following:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

int
main() {
    fstream fin;

    fin.open("../log");
    cout << (fin.is_open() ? "is open" : "is not open") << endl;

    return 0;
}
  • Where ever you create the application, create a file in the parent directory (in this case, "log"...).
  • Execute the application.
  • Delete the file in the parent directory.
  • Execute the application again.
When confronted with questions like this, it is useful to create simple test cases which exercise the feature which is unclear. Learning to use the language to clarify your own questions is a very useful skill to develop.
  #6  
Old 13-Jul-2008, 12:26
Soybeanio Soybeanio is offline
New Member
 
Join Date: Jul 2008
Posts: 8
Soybeanio is on a distinguished road
Thumbs up

Re: How can I literally OPEN a file using a C++ program?


Thank you very much. This is a lot of help
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Open, Read, Write to text file lanz C++ Forum 3 11-Apr-2008 13:58
Double linked List & File System NatsoumiMaya C Programming Language 1 10-Feb-2008 09:23
How to use the data obtain in one C file to another C file? TommyC C Programming Language 12 15-Jan-2008 10:51
malloc/free usage, and general good programming behavior lavaka C Programming Language 7 14-Jun-2007 20:50
Open file and print to a different file alex18 C++ Forum 7 06-Oct-2006 17:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:27.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.