Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-13-2004, 11:25 AM   #1
Registered: Oct 2003
Location: Pico
Distribution: SUSE 9.1
Posts: 83

Rep: Reputation: 15
c++ check if file exists

Are there any apis that check whether a file is existant from a string you pass in?
Old 03-13-2004, 12:24 PM   #2
LQ Newbie
Registered: Mar 2004
Distribution: SuSE 9.0
Posts: 4

Rep: Reputation: 0
just open the file for reading, if this succeeds, then the file exists, something like this:
FILE* fp = fopen(path, "r");
if (fp) {
    // file exists
} else {
    // file doesn't exist

Last edited by LAMP_User; 03-13-2004 at 12:28 PM.
Old 03-13-2004, 12:37 PM   #3
Mega Man X
LQ Guru
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 64
A file? Like... to check if a file exists when you try to open or write to it? If so, try something like this:

using namespace std;

int main()
    char fileName[80];
    char buffer[255];
    cout << "Give file name: ";
    cin >> fileName;

    ifstream fin(fileName);
    if (fin)  // check to see if file exists
        cout << "This file has:\n";
        char ch;
        while (fin.get(ch))
            cout << ch;
        cout << "\n End of file \n";

    cout << "Opening file" << fileName << " in append mode...\n";

    ofstream fout (fileName,ios::app);
    if (!fout)  // if you try to open the file for writing and it fails, then
        cout << " Impossible to open " << fileName << "for writing...\n";

    cout << "\n Enter text to the file: ";
    fout << buffer << "\n";

    // try to re-open the file read-only;
    if(!fin)    // if fails, then:
        cout << "Error opening file " << fileName << " for reading...\n";

    cout << "Now the file contains:\n";
    char ch;
    while (fin.get(ch))
        cout << ch;
    cout << "\n End of the file reached, closing program...\n";

    return 0;
Note: I've no idea if it will compile... I've not tested it myself, I just wrote it now . But you will get the idea...


Last edited by Mega Man X; 03-13-2004 at 12:48 PM.
Old 03-15-2004, 12:08 AM   #4
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Rep: Reputation: 30
i do not have the exact syntax in front of me, but if i remember correctly that the open file function in output stream will return a value of 1 or 0 if/ifnot successful. the syntax above works, but you can use the test in alot other situations.

Last edited by xviddivxoggmp3; 03-15-2004 at 12:10 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
file not found by glob? - file exists? rgdatit Linux - Newbie 2 07-29-2005 02:16 AM
ksh check if file exists (using wildcard) problem r18044 Linux - Newbie 5 02-22-2005 07:52 AM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
Linker problem: can't find a file, but the file exists atlep Programming 5 08-16-2004 06:15 AM
file exists? raven Programming 7 10-26-2003 07:44 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:19 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration