Reading from a file in Xcode/using cin

This is a discussion on Reading from a file in Xcode/using cin within the C++ Programming forums, part of the General Programming Boards category; Hey all, In Xcode, do you have to put the text file somewhere special for it to be read? My ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    Reading from a file in Xcode/using cin

    Hey all,

    In Xcode, do you have to put the text file somewhere special for it to be read? My file opening code runs but is always .fail().

    Also how can I make an ifstream from cin?

    Thanks for any help

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    I don't know about Xcode. Check to see where the executable file is being placed. Some (most) IDEs run the compiled program in a different environment than that in which the executable file is, but if you can find the file, you can run it wherever you like.

    As for your second question, you can't make an ifstream from cin because ifstream is derived from istream, which is what cin is. I think it is more likely that you mean something else entirely, so please restate your question.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Well if there is no file specified in argv[] I want to read from std input instead. How can that be implemented?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Exactly what is your command that you run?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Ok the program is a search program which searches a file for the first command argument, then prints out any line with that in it. If there's only one argument, it uses cin.

    salami testdata.txt

    is the argument line I'm trying to do now, and I've no idea how I would implement it if it was only

    salami

    thanks for looking

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Code:
    void myInputFunction ( ifstream &in );
    
    int main ( int argc, char *argv ) {
      if ( argc < 2 ) {
        myInputFunction( cin );
      } else {
        ifstream myfile(argv[1]);
        myInputFunction( myfile );
      }
    }
    Use a function which takes a reference to an ifstream, then choose where that ifstream comes from.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    Also, you can use istream::read(char*, size_t) and get the length of the read from istream::gcount( ).
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Alright so cin is a reference to an ifstream? Would I have to open the file before I pass it to myInputFunction? because if the open command was in myInputFunction, would that work with cin?

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    >> Alright so cin is a reference to an ifstream?

    I didn't spot that earlier, but no, cin is actually an instance of an object that is derived from an istream (not ifstream).

    >> Would I have to open the file before I pass it to myInputFunction?

    Yes. Just be sure to change the parameter to a reference to an istream, of course.

    >> because if the open command was in myInputFunction, would that work with cin?

    Nope.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21