Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

file i/o

Posted on 2000-05-13
4
Medium Priority
?
198 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 240 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

916 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