help printing a file to screen

This is a discussion on help printing a file to screen within the C++ Programming forums, part of the General Programming Boards category; hey, i was having some troubles printing the contents of a file to screen..this is what ive got...for some reason ...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    Question help printing a file to screen

    hey,

    i was having some troubles printing the contents of a file to screen..this is what ive got...for some reason nothing get printed..

    Code:
    int parser::readFile(void) {
    
    	string filename="data.txt";
    	string line;
       
    	ifstream file(filename.c_str());
       
    	if(!file.is_open()) {
    
    		cout << "Error...file is not opened" << endl;
            return 0;
    	} 
    	
    	else
    		return 1;
    	
    
    	while (!file.eof()) {
    
    		getline(file, line);
    		cout << line << endl;
    	}
    
    	file.close();
    }
    thanks for any help

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by waxydock
    Code:
    int parser::readFile(void) {
    
    	string filename="data.txt";
    	string line;
       
    	ifstream file(filename.c_str());
       
    	if(!file.is_open()) {
    
    		cout << "Error...file is not opened" << endl;
            return 0;
    	} 
    	
    	else
    		return 1;
    	
    
    	while (!file.eof()) {
    
    		getline(file, line);
    		cout << line << endl;
    	}
    
    	file.close();
    }

    If the file can't be opened, you show an error. Your else statement means that if the file can be opened, then return 1 (thus exiting the function). Take out that whole else block.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Your function always returns before it is able to read from the file:
    Code:
    if(!file.is_open()) {
    
    	cout << "Error...file is not opened" << endl;
            return 0;
    } 
    	
    else
    	return 1;
    What other possibility is there? If the file is open, your function returns, and if the file isn't open, it returns.

    In addition, returning 0 is usually supposed to indicate all is well, e.g. that is why main() ends with return 0. If the file cannot be opened, you probably want to return something different from 0, e.g return 1. It doesn't really matter what number you return, and in any case you can do something like this:

    Code:
    int return_state;
    parser parser1;
    
    if(parser1.readFile() == 1) 
        cout<<"Unable to read from file\n";
    Last edited by 7stud; 03-26-2005 at 01:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM

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