LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 04-11-2003, 09:45 PM   #1
cxel91a
Member
 
Registered: May 2001
Posts: 61

Rep: Reputation: 15
Test for file


I'm trying to create a mini program to copy
a file using C++ on a linux system. Can
someone tell me how to verify that a
file is present before calling ifstream object.
 
Old 04-11-2003, 10:42 PM   #2
rmartine
Member
 
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618

Rep: Reputation: 30
Use the open() command. If I remember correctly if you chose not to create the non-existent file it will return a -1.

Good Luck
 
Old 04-12-2003, 11:13 AM   #3
cxel91a
Member
 
Registered: May 2001
Posts: 61

Original Poster
Rep: Reputation: 15
Tried it

That only works for ofstream object creating files, not ifstream.
 
Old 04-12-2003, 01:25 PM   #4
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Code:
#include<iostream>
#include<fstream>
#include<string>

int main() {
  std::ofstream outfile("outfilename");
  std::ifstream infile("infilename");

  if ( ! infile ) {
  std::cerr << "Error message - no file" << std::endl;
  return -1;
  }

  if(! outfile) {
  std::cerr << "Error message" << std::endl;
  return -2;
  }

  std::string word;
  while ( infile >> word ) outfile << word << ' ';

  return 0;
}

Last edited by GtkUser; 04-12-2003 at 01:26 PM.
 
Old 04-12-2003, 01:57 PM   #5
cxel91a
Member
 
Registered: May 2001
Posts: 61

Original Poster
Rep: Reputation: 15
I had that part of it. The goal is to get the src file from the user. If a non-existing file was given the output was an empty file.
I want to try and test for an actual file on the system before passing in the file name.

I'm looking for the syntax to perform the similar function to

if [ -z fileName ]; then
$action

Last edited by cxel91a; 04-12-2003 at 02:08 PM.
 
Old 04-14-2003, 07:55 AM   #6
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
You should use the system call stat for that. Read the man pages to find out how to use it.
 
Old 04-15-2003, 11:23 AM   #7
cxel91a
Member
 
Registered: May 2001
Posts: 61

Original Poster
Rep: Reputation: 15
Thanks
 
  


Reply



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 test to make sure files aren't damaged/corrupt bluedevlx Linux - Newbie 1 11-04-2004 12:11 PM
File Test to make sure files aren't corrupt/damaged bluedevlx Programming 2 11-04-2004 11:50 AM
test.c:1:19: Stdio.h: No such file or directory :( wogga Linux - Software 1 06-11-2004 02:57 PM
Install Tie::File - where to put Test/More.pm? waffe Linux - General 0 11-25-2003 09:31 PM
bash script test file operators... bulliver Programming 3 10-17-2003 12:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration