C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-22-2009, 12:40 AM   #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
Tom_Arch is offline   Reply With Quote
Old 06-22-2009, 01:30 AM   #2
Kiss the monkey.
 
CodeMonkey's Avatar
 
Join Date: Sep 2001
Posts: 810
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
CodeMonkey is offline   Reply With Quote
Old 06-22-2009, 04:36 PM   #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?
Tom_Arch is offline   Reply With Quote
Old 06-22-2009, 04:38 PM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 06-22-2009, 10:38 PM   #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
Tom_Arch is offline   Reply With Quote
Old 06-22-2009, 11:03 PM   #6
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 06-22-2009, 11:12 PM   #7
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
Also, you can use istream::read(char*, size_t) and get the length of the read from istream::gcount( ).
Sebastiani is offline   Reply With Quote
Old 06-23-2009, 02:21 AM   #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?
Tom_Arch is offline   Reply With Quote
Old 06-23-2009, 04:49 AM   #9
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> 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.
Sebastiani is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
opening empty file causes access violation trevordunstan C Programming 10 10-21-2008 11:19 PM
Formatting the contents of a text file dagorsul C++ Programming 2 04-29-2008 12:36 PM
Game Pointer Trouble? Drahcir C Programming 8 02-04-2006 02:53 AM
System drdroid C++ Programming 3 06-28-2002 10:12 PM


All times are GMT -6. The time now is 07:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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