Solved

file i/o

Posted on 2000-05-13
4
186 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

912 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

16 Experts available now in Live!

Get 1:1 Help Now