Solved

file i/o

Posted on 2000-05-13
4
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

630 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