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 04-Oct-2008, 23:42
nancycoder nancycoder is offline
New Member
 
Join Date: Oct 2008
Posts: 14
nancycoder is an unknown quantity at this point

How to pass file path as parameter to fopen()?


Hi ,

i need help about passing the file path as parameter for fopen(), fread(), fwrite() functions or for FILE * pointer. How can I do that? i.e.

CPP / C++ / C Code:
int main ()
{
FILE * fpin;
char s[50]

printf("Enter source file: ");
gets(s);
fpin = fopen (s, "r+");
}
So, s should be a file path anywhere in disk not at the currnet folder so that I can enter a full file path not just the file name.

Any suggestion will be appreciated.

Thanks!

Nancy
Last edited by admin : 05-Oct-2008 at 02:12. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 05-Oct-2008, 01:28
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: How to pass file path as parameter to fopen()?


Quote:
Originally Posted by nancycoder
So, s should be a file path anywhere in disk not at the currnet folder so that I can enter a full file path not just the file name.
Your question is a bit puzzling, as simple experimentation will yield the answer.
  • If the file data.txt to be read is in the same directory as the executable, the file can be opened via:
    CPP / C++ / C Code:
    FILE *fp = open("data.txt", "r");
  • If the file data.txt is in any other directory, it can be specified with a relative path:
    • eg. data.txt is in parent directory to the directory containing the executable:
      CPP / C++ / C Code:
      FILE *fp = open("..\\data.txt", "r");
  • ...or the file can be specified with an absolute path:
    • eg. data.txt can be found as \Users\me\data.txt:
      CPP / C++ / C Code:
      FILE *fp = open("\\Users\\me\\data.txt", "r");
    ...which assumes that the application resides in a Microsoft filesystem. Since Microsoft uses a backslash ('\') as the directory delimiter, and the backslash is considered to be a meta-character in C/C++, Microsoft directory delimiters have to be escaped (doubled) to be represented correctly within the application.
  #3  
Old 05-Oct-2008, 10:14
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: How to pass file path as parameter to fopen()?


A word of advice: do not use gets. If you want to know why simply google why is gets bad and you will get plenty of links. If you are not interested in the reason behind it and just want a safer method, investigate fgets instead.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
 
 

Recent GIDBlogAccepted for Ph.D. program 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
Power Calibration Error In Nero Fix (hopefully) matt3678 Computer Hardware Forum 60 20-Aug-2009 06:04
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 21:03
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 04:52

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

All times are GMT -6. The time now is 16:22.


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