Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Adding Myfileexists Function To My Basic Code?
kvarnerexpress
post Apr 7 2005, 11:34 AM
Post #1


Super Member
*********

Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



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 string

if (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.
Go to the top of the page
 
+Quote Post
OpaQue
post Apr 7 2005, 11:42 AM
Post #2


Administrator
Group Icon

Group: Admin
Posts: 1,479
Joined: 11-June 04
From: Somewhere in Time & Space.
Member No.: 1



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. smile.gif
Go to the top of the page
 
+Quote Post
dexter
post Apr 9 2005, 04:17 AM
Post #3


Advanced Member
*******

Group: Members
Posts: 142
Joined: 24-December 04
From: Queensland, Australia
Member No.: 2,902



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:

CODE

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


if you don't have

CODE

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.

CODE

// 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:

CODE

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:

CODE

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.

CODE

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...

CODE

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.

CODE

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. biggrin.gif

Good luck.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. C Code, Can U Solve This?(22)
  2. Simple C File Handling In Action(4)
  3. Prob With My C Code(4)
  4. Source Code For Paint Like Program Under Dos In C(2)
  5. Wolfenstein Source Code Now In Public Domain(3)
  6. A Telephone Directory Source Code In C(2)
  7. Help C++(2)
  8. C++ Roman Numeral Conversion Code Help(5)
  9. Most Efficient Code To Get Prime Numbers(7)


 



- Lo-Fi Version Time is now: 13th October 2008 - 08:13 AM