LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-13-2004, 11:25 AM   #1
Genjix
Member
 
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
LAMP_User
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:
Code:
FILE* fp = fopen(path, "r");
if (fp) {
    // file exists
    fclose(fp);
} 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
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

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

Code:
#include<fstream>
#include<iostream>
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";
    }
    fin.close();

    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";
        return(1);
    }

    cout << "\n Enter text to the file: ";
    cin.ignore(1,'\n');
    cin.getline(buffer,255);
    fout << buffer << "\n";
    fout.close();

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

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

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

Regards!

Last edited by Mega Man X; 03-13-2004 at 12:48 PM.
 
Old 03-15-2004, 12:08 AM   #4
xviddivxoggmp3
Member
 
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.
 
  


Reply


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


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

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