Reading data under Linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paultawk
    New Member
    • Oct 2006
    • 10

    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("ex ample.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
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    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("ex ample.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?

    Comment

    • ssharish
      New Member
      • Oct 2006
      • 13

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

      ssharish

      Comment

      • paultawk
        New Member
        • Oct 2006
        • 10

        #4
        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?

        Comment

        • arne
          Recognized Expert Contributor
          • Oct 2006
          • 315

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

          Comment

          • paultawk
            New Member
            • Oct 2006
            • 10

            #6
            for compilation i used the following, is it wrong?

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

            Comment

            • arne
              Recognized Expert Contributor
              • Oct 2006
              • 315

              #7
              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?

              Comment

              Working...