473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I pass a path in file handling with C++ to create or open a file?

I have a folder "pics" in g: drive. There I want to create some text.txt file. Thus, the final path is "g:\pics\text.txt".
How can I do that?
Jul 14 '12 #1
3 5285
divideby0
131 128KB
for writing

Expand|Select|Wrap|Line Numbers
  1. ofstream outf("g:\\pics\\text.txt");
for reading

Expand|Select|Wrap|Line Numbers
  1. ifstream inf("g:\\pics\\text.txt");
for either

Expand|Select|Wrap|Line Numbers
  1. fstream f_io("g:\\pics\\text.txt", ios::in | ios::out);
If your path is stored in a variable, then if using a char array for the path, replace the string literal with the variable name. If using a member of the string class, use string_obj.c_str() or maybe string_obj.data() instead of the string literal "g:\\pics\\text.txt"

Hope that helps
Jul 17 '12 #2
Sorry Sir its not working! Please help some other way!
Jul 17 '12 #3
divideby0
131 128KB
I can't really tell what you're wanting to do without seeing what you've tried. The more detail you can provide as in code, error messages (if any), and such the more likely someone will be able to help you out.

Did you include the fstream header? Did you check the status of trying to open the file? Is the path valid? Is the disk write-protected? Try something like

if you're using the standard namespace

Expand|Select|Wrap|Line Numbers
  1. fstream f("g:\\pics\\text.txt", ios::in);
  2.  
  3. if(!f.is_open())
  4.    cerr << "unable to open file";
  5. else
  6.    cout << "file is open";
if you're not using the standard namespace,

Expand|Select|Wrap|Line Numbers
  1. std::fstream f("g:\\pics\\text.txt", ios::in);
  2.  
  3. if(!f.is_open())
  4.    std::cerr << "unable to open file";
  5. else
  6.    std::cout << "file is open";
what does that show?
Jul 17 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
11
by: Josh | last post by:
Hi, I am having a problem with Python. I am new to Python as a programming language, but I do have experience in other languages. I am experiencing strange problems with File handling and wonder...
2
by: Aggelos I. Orfanakos | last post by:
Hello. Under Gentoo Linux, I issue: $ python timeit.py python: can't open file 'timeit.py' $ ls -al /usr/lib/python2.3/timeit.py -rw-r--r-- 1 root root 9833 Oct 19 02:17...
7
by: D & J G | last post by:
Is there a way to instruct Windows file-handling - 'Opens with:' - from within VB6? I want to ensure that files with the .rtf (rich text) extension always go to Word Pad, no matter which future...
8
by: Raghu | last post by:
Is it possible to write a new file and after writing few bytes, is it possible to read from it from another file stream while write continues? Is there another steam for this type of operation? ...
8
by: paraidy | last post by:
Hi all, as from object i need to read all byte from a file example c: \myphoto.jpg and recreate the file with another name to another directory c:\photo\recreatedphoto.jpg can someone write a...
2
by: ielement | last post by:
I'm new to programing in Visual Basic, but what im wondering is, how can i open a file and create a file by clicking a button? Like when i click button 1 it creates a file (to desktop) and when i...
0
by: perlprod | last post by:
Hi, I want to generate a data file that is to be read by a 'C' program. But I got in dilemma after checking the file generated by the following program. $buffer = ""; $path = "C:/Documents...
0
by: Rinoa | last post by:
vb6 Vista I'm attempting to get the file path returned by an open file dialog so that I can put it in the text property of a text box. However, I have no idea what the ofd property would be. Help?
3
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.