Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading data under Linux

Newbie
 
Join Date: Oct 2006
Posts: 10
#1: Oct 23 '06
I have written a prog to read data from files but it only works under windows.
Can anyone tell me how to read data under Linux?

I would need somehting similar to the following windows code, but that works under Linux:

#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib> // for exit function
// This program reads values from the file 'example.dat'
// and echoes them to the display until a negative value
// is read.
int main()
{
ifstream indata; // indata is like cin
int num; // variable for input value
indata.open("example.dat"); // opens the file
if(!indata) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
indata >> num;
while ( !indata.eof() ) { // keep reading until end-of-file
cout << "The next number is " << num << endl;
indata >> num; // sets EOF flag if no value found
}
indata.close();
cout << "End-of-file reached.." << endl;
return 0;
}

best regards

arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#2: Oct 23 '06

re: Reading data under Linux


Quote:

Originally Posted by paultawk

I have written a prog to read data from files but it only works under windows.
Can anyone tell me how to read data under Linux?

I would need somehting similar to the following windows code, but that works under Linux:

#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib> // for exit function
// This program reads values from the file 'example.dat'
// and echoes them to the display until a negative value
// is read.
int main()
{
ifstream indata; // indata is like cin
int num; // variable for input value
indata.open("example.dat"); // opens the file
if(!indata) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
indata >> num;
while ( !indata.eof() ) { // keep reading until end-of-file
cout << "The next number is " << num << endl;
indata >> num; // sets EOF flag if no value found
}
indata.close();
cout << "End-of-file reached.." << endl;
return 0;
}

best regards


It works fine on my Linux machine. Any specific problem?
Newbie
 
Join Date: Oct 2006
Posts: 13
#3: Oct 23 '06

re: Reading data under Linux


which distribution of linux are u using. the code looks to ok. and what compiler are u using

ssharish
Newbie
 
Join Date: Oct 2006
Posts: 10
#4: Oct 23 '06

re: Reading data under Linux


we i'm using the g++ compiler, and the problem is that it wasnt able to locate the headers such as iostream and ifstream. Any ideas?
arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#5: Oct 23 '06

re: Reading data under Linux


Quote:

Originally Posted by paultawk

we i'm using the g++ compiler, and the problem is that it wasnt able to locate the headers such as iostream and ifstream. Any ideas?

Invoke 'g++ -v' to find out from where g++ tries to get its includes. In the output look for something like

--with-gxx-include-dir=/usr/include/c++/4.0.2

Go there and look if the headers are there or not.
Newbie
 
Join Date: Oct 2006
Posts: 10
#6: Oct 23 '06

re: Reading data under Linux


for compilation i used the following, is it wrong?

g++ -Wall -D__LINUX_OSS__ -o filename filename.cpp
arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#7: Oct 23 '06

re: Reading data under Linux


Quote:

Originally Posted by paultawk

for compilation i used the following, is it wrong?

g++ -Wall -D__LINUX_OSS__ -o filename filename.cpp

Looks good to me. If you don't use/need it somewhere in your code, you can omit the -D __LINUX ... part. Or do you have any specific reason you use it?

Did you find your headers?
Reply