LinuxQuestions.org
Visit Jeremy's Blog.
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 08-16-2004, 11:14 PM   #1
MDBlueIce
Member
 
Registered: Aug 2004
Distribution: Suse Linux 9.1
Posts: 47

Rep: Reputation: 15
Opening Input files with C++ in Linux


I am new to linux and wanted to test out the followong C++ code:
---------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{

ifstream inFile ("input.txt");

if (inFile.fail())
{
cout << "Could Not Open File\n";
}

else
{
cout << "File opened succesfully\n";
}

---------------------------------------------------------------------------------------------------------------
I compiled the program with g++, putting it's ouptut file in my home directory. I also created a new text file in my home directory with KDE (running SuSe 9.1) in my home directory. When i run the program in the shell the output is .....
Could Not Open File
Does anyone know why the file failed to open? In windows, if a file was in the same directory as that in which the program is run, the full path of the file is not needed to open it. Is the situation different in Linux? Or is there another problem which I am unaware of? Thanx for the help.
 
Old 08-17-2004, 01:07 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
In Linux if the file is in the same directory you shoud write
./filename
You have to add ./ before giving the filename.

Also to check if this error is due to the incorrect path you just check it by giving the absolute path aslo.
/home/filename
 
Old 08-17-2004, 01:58 AM   #3
cedar
Member
 
Registered: Feb 2004
Location: Colorado Springs, CO
Distribution: Ubuntu, Fedora, PCLinux, MEPIS, still miss Libranet
Posts: 162

Rep: Reputation: 30
I was always taught to use:

ifstream inFile;

inFile.open ("input.txt");

Try that and let us know how it works.

cheers
 
Old 08-17-2004, 03:53 AM   #4
MDBlueIce
Member
 
Registered: Aug 2004
Distribution: Suse Linux 9.1
Posts: 47

Original Poster
Rep: Reputation: 15
I changed the code to......

if ("./input.txt")

But still the same results occur in the executable
Any other ideas? I greatly appreciate it.
 
Old 08-17-2004, 03:56 AM   #5
MDBlueIce
Member
 
Registered: Aug 2004
Distribution: Suse Linux 9.1
Posts: 47

Original Poster
Rep: Reputation: 15
I figured it out!!!! The extension {.txt} is not needed in linux!! sweet!!! thanx for the help guys.
 
Old 08-17-2004, 04:20 AM   #6
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Good!
For the future keep in mind. IN LINUX THERE IS NO CONCEPT OF EXTENSIONS.
if you made a file file.txt, and you set the permissions that it is an executable, it will act as executeable. similarly a file file.exe can be a text file if the permissions are like that. So just forget there is something like extension in linux.

Wish you all the best
 
Old 08-30-2004, 05:38 PM   #7
escale
LQ Newbie
 
Registered: Apr 2004
Location: Southwest US
Distribution: Got RH9 (Shrike) from Distro that came with Red Hat Linux 9 Bible - Christopher Negus
Posts: 8

Rep: Reputation: 0
For those who may follow:

This searched for file "./input.txt" in my working directory. This code worked on my g++ compiler:

code:
Quote:
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
char filename1[] = "./input.txt";

ifstream inFile (filename1);

if (inFile.fail())
{
cout << "Could Not Open File: " << filename1 << "\n";
}

else
{
cout << "File: " << filename1 << " exists and can be opened.\n";
}
return 0;
}


and the code that Cedar suggested worked when I used it this way. This code finds "./fstream2.cpp" in my working directory:

Quote:
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
char filename1[] = "./fstream2.cpp";

ifstream inFile;
inFile.open (filename1);

if (inFile.fail())
{
cout << "Could Not Open File: " << filename1 << "\n";
}

else
{
cout << "File: " << filename1 << " exists and can be opened.\n";
}
return 0;
}
I too am new at C++ and after reading this post I wanted to share what I learned by applying the tips I researched here. I noticed that this snippet of code could be changed to a function that could return a numeric value that indicates if a file exists in the directory. (1) if it exists or (0) if it does not. This code also worked for me. Although I don't believe it is the most elegant and there exists the possibility that there is a similar function in the std library to do this. Here it is anyway:

Quote:
#include <iostream>
#include <fstream>
using namespace std;

int FileExists( char *filename);

int main ()
{
char filename[] = "./fileexists.cpp";

if (FileExists(filename) == 0)
{
cout << "Could Not Open File: " << filename << "\n";
}
else
{
cout << "File: " << filename << " exists and can be opened.\n";
}
return 0;
}

int FileExists( char *filename)
{
ifstream inFile (filename);
if (inFile.fail())
return 0;
else
return 1;
cout << "Function fileexists encounted a line of code that should ";
cout << "have been impossible to reach.\n \n";
}
GL to all coders,
--- Escale

Last edited by escale; 08-30-2004 at 05:40 PM.
 
  


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
Opening .mpp files in linux jayakrishnan Linux - General 6 07-10-2009 06:55 PM
opening linux file system files in windows 95?? timefortea General 6 08-26-2005 01:28 PM
opening mpp files in linux sailu_mvn LinuxQuestions.org Member Success Stories 0 05-17-2005 07:03 AM
Opening or Executing files in linux from a windows Server rmarvin Linux - Networking 7 01-17-2005 10:32 AM
Opening M$ Access files in linux (need program) enigma Z Linux - Software 1 10-08-2003 07:01 PM

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

All times are GMT -5. The time now is 02:39 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