fstream question

D

Dan M

When you specify the file for an fstream inside your code, do you refer
directly to the files location?

e.g. c:\file

or do you just place the file to be read in the same folder as the .cpp
file?

I'm trying to read in some data in a file and the resources I have looked at
do not give me this information.

Thank you so much

Dan M
 
L

lallous

Hello
When you specify the file for an fstream inside your code, do you refer
directly to the files location?

e.g. c:\file

or do you just place the file to be read in the same folder as the .cpp
file?

I'm trying to read in some data in a file and the resources I have looked
at do not give me this information.

Thank you so much

You can refer to the file either in a relative manner or absolute manner,
either method works.
If you want your code to work, it is better to address files in a relative
manner, either relatively to the current directory or some directory one
level above it, such as: ./prog.exe and ./data./*.txt (your data files).

Regards,
Elias
 
K

Karl Heinz Buchegger

Dan said:
When you specify the file for an fstream inside your code, do you refer
directly to the files location?

e.g. c:\file

or do you just place the file to be read in the same folder as the .cpp
file?

Depends on the file content.
If it is some vital and relatively constant information, such as eg. some
program settings, I use argv[0] to determine the path to the executable
(if argv[0] provides this, there is no guarantee for that) and use that
path to build the complete filename.

If the file contains user data, I let the user specify which file he wants
to process. Most operating systems provide a way to have some sort of
utilities directory, where programs can be started easily from. The
users data file needs not to reside on this directory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,518
Latest member
RomanGratt

Latest Threads

Top