Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-30-2006, 10:15 AM
Lop's Avatar   
Lop Lop is offline
Speaks fluent binary
 
Join Date: May 2006
Posts: 1,135
Rep Power: 16
Lop is on a distinguished road
Default Check if a file exists

Is there a function to check if a file exists?
__________________
Lop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-30-2006, 12:03 PM
icepack's Avatar   
icepack icepack is offline
Programmer
 
Join Date: Jul 2006
Location: North Carolina
Posts: 109
Rep Power: 8
icepack is on a distinguished road
Send a message via AIM to icepack
Default

Not so much a function, but a way
Say you are trying to open a file to write to
you'd need to #include <fstream>

then to open the file use:
Code:
ofstream myFile("local location of file");
but you always have to check if that file actually exists, or else it's be useless. so
Code:
if (! myFile)
{
cout << "Error\n";
return -1;
}
is that what you wanted? i imagine you could just use that even if you didn't want to write to the file.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-31-2006, 08:22 PM
BuildPromoteWin's Avatar   
BuildPromoteWin BuildPromoteWin is offline
Newbie
 
Join Date: Jul 2006
Posts: 10
Rep Power: 0
BuildPromoteWin is on a distinguished road
Default

You could also use the WIN API call "_stat()" to check on file status. it returns a -1 on error / file not found

Code:
int _stat( const char *path, struct _stat *buffer );
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-07-2006, 07:39 PM
Frantic's Avatar   
Frantic Frantic is offline
Learning Programmer
 
Join Date: May 2006
Posts: 91
Rep Power: 9
Frantic is on a distinguished road
Default

Never heard of the WIN API. How do I add that to my code?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-07-2006, 10:03 PM
TkTech TkTech is offline
 
Join Date: Jun 2006
Posts: 896
Last Blog:
Having trouble with yo...
Rep Power: 20
TkTech is on a distinguished road
Send a message via MSN to TkTech
Default

Frantic, if youve never heard of it, then just stick to standard c++

Code:
#include <fstream>

void main( void )
{
  ifstream fin("myfile.ini" , ios::in );
  ofstream fout;
  if( fin.fail() ) //File error
 {
    //So create the file
     fout.open("myfile.ini" , ios::out | ios::trunc );
     //File it with the default text/binary
     fout << "Whateveryouwantasdefault";
    //Close and clear the file write buffer
     fout.close();
     fout.clear();
    //And reopen the file
     fin.open("myfile.ini" , ios::in );
  }
   //Now do your normal operations as if nothing ever happend
   fin.read((char *)&bla , sizeof(blastruct));
   fin.close();
}
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: ionFiles - Joomla Simple File Download Jordan Community Projects 302 08-30-2008 04:49 PM
Windows XP Tricks & Tips!!!!..new ones. pranky Tutorials 9 08-23-2008 03:22 PM
Check if a file exists? Lop Pascal/Delphi 3 01-07-2007 12:54 AM
Check if a File Exists NeedHelp C# Programming 4 08-07-2006 05:20 PM
Check if a URL Exists Chan PHP Forum 1 07-13-2006 12:54 PM


All times are GMT -5. The time now is 03:03 AM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
LogicKills ........ 20.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads