473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Reading data under Linux

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
Oct 23 '06 #1
6 2209
arne
315 Expert 100+
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?
Oct 23 '06 #2
which distribution of linux are u using. the code looks to ok. and what compiler are u using

ssharish
Oct 23 '06 #3
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?
Oct 23 '06 #4
arne
315 Expert 100+
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.
Oct 23 '06 #5
for compilation i used the following, is it wrong?

g++ -Wall -D__LINUX_OSS__ -o filename filename.cpp
Oct 23 '06 #6
arne
315 Expert 100+
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?
Oct 23 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
0
by: Andy | last post by:
Hi, In the code below (not pretty I know but it's an early version :-P) I'm having problems reading the data object back in. If I move the reading code to immediately after the section where it...
2
by: Satish Chimakurthi | last post by:
Hi all, This is in continuation of my email today regarding reading data files. I am stuck with one more problem now. Here are my two data files: a. fluid_grid.dat 1.00000000000000 ...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
15
by: djj858 | last post by:
Another newbie question: How do I begin reading data, but starting from the xth line down a list? In other words, how do I skip the first lines and not read in those values?
1
by: Roob | last post by:
I am learning C# and was wandering if there is a straightforward way to read a binary file created by C++. For example, I have a data structure as follows: typedef struct _data time_t date;...
0
by: Boris9577 | last post by:
Hello everyone, I need help with reading data from database. I am working with Visual C++ and MS SQL 2005. This is the problem: I have tried to read some values from data base (on a way described...
1
by: Diego F. | last post by:
Hello. I'm having problems reading data from a socket. In my test, I see the maximum size to get data from the socket is 8192 bytes. In my tests, if I send less that 8192, the readen data have...
1
by: =?Utf-8?B?U2hlZXMgQWJpZGk=?= | last post by:
I read an article on the link: http://support.microsoft.com/default.aspx?scid=kb;en-us;306572 related to reading data from Excel using OLEDB The topic's heading is: How to query and display excel...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.