Solved

file i/o

Posted on 2000-05-13
4
183 Views
Last Modified: 2010-04-02
heres my delema, ive got the name of a file in a string, not including the directory.  What i need to do is open that file but it is in another directory for example here is a senario...

char file[20] = "filename.txt";
FILE * file2 = fopen(("//dir//%s",file),"w");

but alls that does is open the file in the current directory...

Thanks for any help...
0
Comment
Question by:beyonddeath
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 60 total points
ID: 2807614
The "C" way of doing this would be.

char Path[256];
char Location = "\\Dir\\";
char File[20] = "filename.txt";

strcpy(Path,Location); // Get the location.
strcat(Path,File); // Add on the file name.

FILE *File2 = fopen(Path,"w");

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2807619
(I should also mention that for more complex formating, you could use sprintf() to create the "final" string, but in this case, a strcpy() and strcat() are sufficient, and probably more efficient than sprintf().

However, the best way to handle this is to move to C++.  In C++ you should almost always use string classes instead of C-style (character array)  strings.  These are far safer and far easier to use.  You could write uour own or use the STL string class, or a string class from another source.  Similarly you should use the C++ file stream objects (fstream) in place of C I/O streams.  These are also far safer and more powerful than C I/O streams.

for example;

string Location = "\\Dir\\";
string File = "filename.txt";
string Path = Location + File;
ofstream File2(Path.c_str());
0
 

Author Comment

by:beyonddeath
ID: 2808320
Thats what i thought just couldnt remember the functions strcpy and strat ;)

THANKS
0
 

Author Comment

by:beyonddeath
ID: 2808346
works great as long as winsock will send stuff right always that 50/50 chance of it not sending what i tell it too stupid stupid stupid but this loads everything great!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now