reading from a file and classes

This is a discussion on reading from a file and classes within the C++ Programming forums, part of the General Programming Boards category; well im stuck again and its because of classes again. i have the follwing program that works fine but i ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    reading from a file and classes

    well im stuck again and its because of classes again. i have the follwing program that works fine but i need to make a class and use the code as a function of that class
    Code:
    #include	<iostream.h>
    #include	<fstream.h>
    #include	<stdlib.h>
    
    display a file on the screen
    int	main(void)
    {
    	const int	FileNameLen = 30;	// Constant int
    	ifstream	infile;			// file object
    	char		filename[FileNameLen];	// Store the filename
    						// as a character string
    	char		inchar;		//Read a character at a time
    
    
    	// Get the file name
    	cout << "Enter file name ->" << '\t' ;
    	cin >> filename;
    	// Open the file
    	infile.open(filename);
    	
    	if (!infile)	{
    		cerr << "Cannot open " << filename << endl;
    		exit(1);
    	}
    
            // Read the file
    	while (!infile.eof())	{
    		inchar = infile.get();
                    cout << inchar;	//display it on screen
    	}
    	infile.close();	//Close the file	
    	return(0);
    }
    this is what i have so far but it gives me a few errors, and im totally lost, someone help me out?
    Code:
    #include	<iostream.h>
    #include	<fstream.h>
    #include	<stdlib.h>
    
    
    const int	FileNameLen = 30, ArrayLen =4;	// Constant integer
    
    class array
    {
    public:
    		int readarray();
    		array();
    private:
    	
    		ifstream	infile;			// file object
    		char		filename[FileNameLen];	
    						
    		char		inchar;		//Read a character at a time
    };
    array::array()
    {
    
    }
    // display the file 
    int	readarray()
    {
    	
    	// Get the file name
    	cout << "Enter file name ->" << '\t' ;
    	cin >> filename;
    	// Open the file
    	infile.open(filename);
    	// If you can't open the file, print error message
    	if (!infile)	{
    		cerr << "Cannot open " << filename << endl;
    		exit(1);
    	}
    
            // Read the file
    	while (!infile.eof())	{
    		inchar = infile.get();//This reads in
    					
                    cout << inchar;//Display it on screen
    	}
    	infile.close();	//Close the file	
    	return(0);
    }
    int main()
    {
    		array Numbers;
    
    		int nums = Numbers.readarray();
    		cout <<"The numbers in the array "<< nums << endl;
    
    	return 0;
    }
    When no one helps you out. Call google();

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    i have narrowed it down a bit more, any suggestions pelease, this is what i got so far
    Code:
    #include    <iostream.h>
    #include    <fstream.h>
    #include    <stdlib.h>
    
    const int    FILENAMELEN = 30, ArrayLen = 4;
    
    // The class array
    class    array    {
    public: // These functions are avaiable for use outside the class 
        inline int    getsize()    {return(size);}
        void        readarray(void);
        array();
    
    private:
        //Private - available only to member functions
        int    x[ArrayLen];
        int    size;
    };
    
    //initialization constructor 
    array::array(void)
    {
        size = 0;
    }
    
    void array::readarray(void)
    { 
        ifstream infile;
        char filename[FILENAMELEN];
        char inchar;
    
        cout<<"Enter filename->" <<'\t';
        cin>> filename;
        //openfile
        infile.openfile(filename);
        //if you cant open the file, print error message
        if (!infile)
        {
            cerr<< "Cannot open " <<filename << endl;
            exit(1);
        }
    
        //Read the file character
        while (!infile.eof())
        {
            inchar = infile.get(); //this reads in even whitespaces
            cout << inchar; //display it on screen
        }
        infile.close();
        return(0);
    }
    
    int    main()
    {
        array    myarray;
    
    
        myarray.readarray();
        return(0);
    }
    but im getting 1 error where i have commented
    Last edited by InvariantLoop; 02-12-2005 at 05:30 PM.
    When no one helps you out. Call google();

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    void array::readarray(void);
    {
    See it?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    If i remove the semicolon i get 5 errors instead of 1 that i was getting before. This is really getting on my nerves, im stuck on this the whole day. I cant even think anymore. Jesus help me lol

    edit: since you know more than i do i took your advice and removed the semicolon, i got 5 errors instead of 1 so i tryed to figure them out. Turns out i have typed FILENAMELEN using camel notation on the declaration and captial letters when i was using it. yet i still get 1 error
    Code:
    openfile' : is not a member of 'ifstream'
            c:\program files\microsoft visual studio\vc98\include\fstream.h(98) : see declaration of 'ifstream'
    edit: i have corrected the code, and im still getting the above error
    Last edited by InvariantLoop; 02-12-2005 at 05:32 PM.
    When no one helps you out. Call google();

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    1. your headers should look like:
    Code:
    #include    <iostream>
    #include    <fstream>
    #include    <cstdlib>
    you were using the old headers (with .h) hence the deprecated comment.

    2. ifstream doesn't have member openfile - it is simply called open.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    thats correct . i clicked the line under the error and i read it, then i clicked List members and looked all the memebers and only "open" was a valid assumption so i used it and it worked.

    Thanks a lot Andy, now i can work on the rest of this prog. rejoice!!
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text file to struct help needed!
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 10:37 AM
  2. Defining multiple classes in the same header file
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2008, 10:36 AM
  3. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. classes in an exe. objects from text file.
    By davebaggott in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2001, 02:55 AM

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