Jump to content
xisto Community
kvarnerexpress

Adding Myfileexists Function To My Basic Code?

Recommended Posts

Here is a short and basic piece of my first ever application written in Visual C++ 6.0 . I have also commented the code well to show you EXACTLY what my code is meant to do with each function. What exactly I am asking for, is for someone to please replace anywhere that you see "EXISTS" with the proper function "myFileExists" to actually check to see if the file exists. Also I am asking for anyone that may see an error in this code, to please point it out to me and how I can fix it.If anyone can help me with getting this very basic code to work, it would be GREATLY appreciated. Here it is:Code:#include <iostream.h>#include <string.h>main (){ string File; // define "File" as a stringif (File == FILENAME) // define "File" as a "Filename"{ if (File EXISTS) // check if "File" exists { cout << "MESSAGE" << endl; // if so displays appropriate message }}else // if "File" doesn't exist{if (File == FILENAME) // define "File" as NEW "Filename"{ if (File EXISTS) // check if NEW "File" exists { cout << "MESSAGE" << endl; // if so displays appropriate message }}}else // if "File" doesn't exist{if (File == FILENAME) // define "File" as NEW "Filename"{ if (File EXISTS) // check if NEW "File" exists { cout << "MESSAGE" << endl; // if so displays appropriate message }}}else // if "File" doesn't exist{if (File == FILENAME) // define "File" as NEW "Filename"{ if (File EXISTS) // check if NEW "File" exists { cout << "MESSAGE" << endl; // if so displays appropriate message }}}else // if "File" doesn't exist{if (File == FILENAME) // define "File" as NEW "Filename"{ if (File EXISTS) // check if NEW "File" exists { cout << "MESSAGE" << endl; // if so displays appropriate message }}}else // if "File" doesn't exist{if (File == FILENAME) // define "File" as NEW "Filename"{ if (File EXISTS) // check if NEW "File" exists { cout << "MESSAGE" << endl; // if so displays appropriate message }}}return (0);}PS = I know I could use a loop for this, but this just complicates everything for me with creating new arrays and such. I would like to start SIMPLE as I do not need to check for more than 6 different files.Thanks ALOT in advance!!Peace.

Share this post


Link to post
Share on other sites

This is to warn you about your post. You are supposed to put the CODEs in [ CODE ] tag. I have reduced 5 Hosting credits from your account. If I had considered your complete post as spam, you could have lost more. Consider this as a friendly warning. :)

Share this post


Link to post
Share on other sites

What is all this for anyway?

Anyway, a few tips before I start. You're using C++, so actually -use- C++.

#include <iostream> as opposed to #include <iostream.h>

The first is the C++ header, the second is the C header... use the first...

#include <string> - C++ style strings
#include <cstring> - Functions for C style strings (aka char*)

Again, use the C++ style.

A lot of other headers are slightly different in C++ than C...

e.g. <math.h> is <cmath>
<stdlib.h> is <cstdlib>


Okay, on to the testing.

I'm not going to give you the function, you'll need to work out that yourself, but I'll explain briefly about filestreams. These classes are included in the <fstream> header. You'll most likely need to add after your header declarations:

using std::ifstream;using std::ofstream;

if you don't have

using namespace std;


There are three types of filestreams: ifstream, ofstream and fstream.

ifstream is for extracting data from files only.
ofstream is for outputting data to files only.
fstream is a two way interface for files.

Now, whenever you want to use a filestream, you need to declare it first.

// fin and fout are used because filestreams work nearly identically// to the standard I/O cin and cout (except for a few extra functions).ifstream fin;             ofstream fout;

Then, you need to open the file. The member function is declared as:

void open(const char *s,        ios_base::openmode mode);

Now for the different streams, the default mode differs. Ifstreams default mode is ios::in, ofstreams default mode is ios::out, and fstreams default mode is ios::in | ios::out.

These basically describe how the file is going to be used... input, ouptut, or both. There are other options, but you can look into them as you need to.

Based off our original declarations, we'll open a file:

fin.open("test.txt", ios::in);fout.open("test2.txt, ios::out);

Ok, the question is, are they open. The fstream class provides another function for filestream called, is_open(). This function returns true if the file was opened.

if(fin.is_open())  cout << "test.txt opened\n";else  cout << "test.txt couldn't be opened\n";if(fin.is_open())  cout << "test2.txt opened\n";else  cout << "test2.txt couldn't be opened\n";

Another way of testing if a file could be opened is to test if the filestream has failed...

if(fin.fail())or if(fout.fail())

Now, when you open a file for outputting, unless something seriously goes wrong, or there's already a write protected file, the file will always be opened. This is because if the file doesn't exist, a new file is created.

For input, on the other hand, it will fail if the file doesn't exist.

And last of all, whenever you're done with a file make sure to close it.

fin.close();fout.close();

Hope that makes enough sense for you to use. If there are any details missing... use your MSDN, google, or ask here. :)

Good luck.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.