Thread: Reading from a file

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Reading from a file

    Hi

    I am trying to read a file and then print out information about that file using a function.

    Here is the code i have so far:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    ifstream in;
    
    void NoOfChars();
    
    
    int main()
    {
    	string file;
    
    	cout << "Enter the file name you wish to open: " << endl;
    	cin >> file;
    
    	cout << "The file you wish to open is: " << file <<endl; 
    
    	in.open(file);
    
    	if (in.fail())
    	{
    		cout << "File failed to open" << endl;
    	}
    
    // Then print out the information here
    
                    cout<<"Number of Chars: "<< NoOfChars(in)<<endl;
    
    }
    
    
    
    void NoOfChars()
    {
    
    //Function code here
    
    return chars;
    
    }

    My question is, do i have the right idea?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's a start. A couple notes though:
    - You can't pass parameters to a function that doesn't take any.
    - You can't return a value from a function that returns void.
    - If you fail to open the file, you should exit the application instead of just printing a message.
    - Avoid global variables where possible. In this case, "in" should be declared in the main function.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Code:
    in.open(file);
    Since file is a std::string you must convert the std::string to a C-string for this call to open:

    Code:
    in.open(file.c_str());
    Code:
    void NoOfChars()
    {
    
    //Function code here
    
    return chars;
    
    }
    Since you have defined your function to return nothing (void) you can not return anything. Also your function prototype, function definition, and function call must all agree on the number and type of parameters, and the return type. You have defined this function to take no arguments but in your function call you are trying to pass a parameter.

    Jim

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Cheers for the replys. The function should have the prototype int NoOfChars()

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    I have solved the problem. Thanks
    Last edited by tomelk31; 03-09-2011 at 06:48 AM.

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. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM