Adding Myfileexists Function To My Basic Code?

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

Adding Myfileexists Function To My Basic Code?

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

 

 

 


Reply

OpaQue
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

Reply

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

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : adding myfileexists function code

  1. Simple C File Handling In Action - Small code snipet which covers most of basic file handling and navigat (3)
    Yesterday I suddenly got a lot of work. The same work we try to push off, yes you are right all
    formalities to get the code review incorporated and update all source code files with code review
    headers. Imagine if you need to open 300 files one by one and append code review headers at the
    end. Since most files are reviewed in groups of 20 to 30 files. We require one header to be placed
    in say 20 to 30 files. To simplify I went back to my class assignment days and wrote this small c
    utility to open all files passed on command line and open attach code review headers an...
  2. Most Efficient Code To Get Prime Numbers - (7)
  3. C Code, Can U Solve This? - Another interesting problem (22)
    Hello, Look at the code given below CODE void fun(void) { /* Put your code here so
    that main does not print 20 */ } int main() { int i = 20; fun();
    printf("i = %d\n", i); return 0; } 1. You are allowed to put your code
    ONLY in fun. 2. You are not allowed to change even one character in main. 3. You should not use
    functions like exit(),abort() in the function fun. How do I manipulate and change the value of i?
    Have fun......
  4. A Telephone Directory Source Code In C - (2)
    Well i had made this project some years before. It is a telephone directory which has a very good
    User Interface operatable with scroll key. This code can be usefull in learning about file handling
    and User Interface. CODE #include <stdio.h> #include <box.h> #include
    <conio.h> #include <dos.h> #include <stdlib.h> #include <bios.h>
    #include <graphics.h> FILE *fp; addnew2(char [],char []);
    prnd(); deleteit(); info(); pl(); checkcmd();
    showall(...
  5. Source Code For Paint Like Program Under Dos In C - (2)
    this is a dos program that you have been waiting for you learn from it you how to do graphic
    programing under dos if you understand it your then will be able to do any kind of windows GUI in
    dos it make icons and use them it deal with graphic file youwill also learn how to use mouse have
    nice time ...
  6. C++ Roman Numeral Conversion Code Help - If-else Problem (5)
    thanks...
  7. Help C++ - write a function (2)
    Hello! I will be thankfull if you could hlelp me wrtie a function which will read in a phone
    number from the user and return it to the caller ( in C++) Thanks!...
  8. Wolfenstein Source Code Now In Public Domain - (3)
    the Wolfenstein game which we enjoy it alot becouse of its highly graphic technique now u can
    learn alot from it and become profissional programer becouse now its source code in Public Domain
    get the source code now and make your way to become profissional programer get it at
    Wolfenstein source code ...
  9. Prob With My C Code - (4)
    Hi Guys! this is a wonderful place.. although i wasnt a member till now i have referred to
    these forms quite a few times for my programming solns /biggrin.gif" style="vertical-align:middle"
    emoid=":D" border="0" alt="biggrin.gif" /> My problem is ::: I am writing a Code in C using
    Bloodshed Dev C++ 4.9.9.2 on Windows XP. I wanted to open an executable file from my code. After
    using the solution mentioned on these forums, I used QUOTE system("myprg"); It works
    fine.. But the prob is that i want my C program to execute this command and then return to t...
  10. Code Documentation - A better way? (1)
    This is not so much a question, actually, but a bit of a plug for an interesting tool I've
    discovered recently. Anyway. How does everyone else document their code? Are in-source comments
    sufficient? Do you write extra detailed documentation outside of the code? Or do you generate
    external documentation from the code? Since I've been doing Java recently, I was in awe when I
    discovered javadoc. An incredible tool that allows you to generate external documentation for your
    code from your comments in-source. The syntax is reasonably simple, allowing the comme...
  11. Where Can I Get Full Souce Code Of Turbo C++ ? - where can i get full souce code of Turbo (2)
    where can i get full souce code of Turbo C++ ? Al... it must be complete... complete... includes and
    libraries... all.. pls.. help me.. coz i cant compile my programs.......



Looking for adding, myfileexists, function, basic, code

Searching Video's for adding, myfileexists, function, basic, code
advertisement



Adding Myfileexists Function To My Basic Code?



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE