Thread: Trouble with file pointer

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    22

    Question Trouble with file pointer

    Hi!

    I am trying to create a file pointer so I can read characters from a text file. I am not sure that I am doing this correctly. I have been reading a book I have, but I have not quite grasped this concept. The part in red is the section I am having difficulty:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
            
    using namespace std;
            
    void welcome (void);
     
    void getfile (FILE *);
    
    int main (void){
            
            FILE fptr;
            
            welcome();
            
            getfile(&fptr);
            
            return(0);
            
    }       
            
     
     
    void welcome(void){
    
            cout << "\n\nLetter Counter\n\nWelcome to the letter counter. This ";
            cout << "program will prompt you for an input file of\ntext. It ";
            cout << "will read the text and report the number of times each ";
            cout << "letter of the\nalphabet is used. It will also display a ";
            cout << "visual graph.\n\n";
    
            return;
    
    }
    
    void getfile(FILE *fptr){
    
            string filename;
    
            cout << "Please provide filename for input file: ";
    
            cin >> filename;
    
            ifstream *fptr.open(filename(), ios::in);
    
            return;
    
    }
    Thanks for any help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can use either FILE or fstream/ifstream/ofstream, but not both.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    22

    Smile

    I am close, but after these alterations I still get one error. Am I correct in thinking that FILE is used in C and fstream is C++ "edition" or however I should word it? Thank you very much for the help thus far.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void welcome (void);
    
    void getfile (ifstream *);
            
    int main (void){
            
            ifstream fptr;
            
            welcome();
    
            getfile(&fptr);
            
            return(0);
            
    }
    
    
    
    void welcome(void){
    
            cout << "\n\nLetter Counter\n\nWelcome to the letter counter. This ";
            cout << "program will prompt you for an input file of\ntext. It ";
            cout << "will read the text and report the number of times each ";
            cout << "letter of the\nalphabet is used. It will also display a ";
            cout << "visual graph.\n\n";
    
            return;
    
    }
    
    void getfile(ifstream *fptr){
    
            string filename;
            
            cout << "Please provide filename for input file: ";
            
            cin >> filename;
            
            ifstream *fptr.open(filename(), ios::in);
            
            return;
            
    }
    This is the error message:

    letter_counter.cc: In function 'void getfile(std::ifstream*)':
    letter_counter.cc:71: error: expected initializer before '.' token

    I am using g++ for a compiler if that assists any.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't pass a pointer to an ifstream, I think; or at least anyway, why would you? Pass a reference. You also don't want to declare a new variable fptr inside your function (there on line four of your function) when you're passing a variable fptr to your function.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    How do you pass a reference?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    void getfile(ifstream &fptr)
    (Spacing deliberately kept to annoy Elysia. )

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    If I pass a reference can I still pass back the location of the file so it can be utilized in another function? Later I will make another function that will read the characters. Is there a different way I should be approaching this?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It would appear you should read what a reference is.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    So a reference can be thought of as a type of pointer with restrictions? The reference does not pass the memory address, rather it directly accesses the value?

    I have tried using a reference but I got the same error message.

    Code:
    void getfile(ifstream &fptr){ 
    
            string filename;
            
            cout << "Please provide filename for input file: ";
            
            cin >> filename;
            
            ifstream fptr.open(filename(), ios::in);
            
            return;
            
    }
    Thanks for the help!

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Check your function prototype, make sure you use pass by reference there, also in your function there is no need to declare fptr as an ifstream object; you already did that in your main().

    Code:
    void getfile(ifstream &fptr)
    {
    
        char *filename;
        cout << "Please provide filename for input file: ";
        cin >> filename;
    
        fptr.open(filename, ios::in);
    }
    EDIT:

    Also, if I remember correctly ifstream requires the file name to be a standard c string.
    Last edited by Phyxashun; 01-25-2009 at 02:27 AM. Reason: Forgot to mention something!

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    So now I can compile which is a lot better then where I was. So I am curious; why does the variable "filename" have to be a pointer?

    I get a segmentation fault when I attempt to open a .txt or .dat file in the same directory as the program. What could be causing this?

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by nirvana21 View Post
    So now I can compile which is a lot better then where I was. So I am curious; why does the variable "filename" have to be a pointer?
    Because the constructor for an ifstream object takes a char* as a parameter for the filename.

    Code:
    ifstream ( );
    explicit ifstream ( const char * filename, ios_base::openmode mode = ios_base::in );

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    Excellent, everything has worked out. THANKS!

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Phyxashun View Post
    Check your function prototype, make sure you use pass by reference there, also in your function there is no need to declare fptr as an ifstream object; you already did that in your main().

    Code:
    void getfile(ifstream &fptr)
    {
    
        char *filename;
        cout << "Please provide filename for input file: ";
        cin >> filename;
    
        fptr.open(filename, ios::in);
    }
    EDIT:

    Also, if I remember correctly ifstream requires the file name to be a standard c string.
    NO!
    This is undefined behavior!
    Using pointers without allocating them
    You should use std::string and call the c_str() member function to get the C-style string that the constructor expects.

    (Note the position of the & )
    Code:
    void getfile(ifstream& fptr)
    {
    
        std::string filename;
        cout << "Please provide filename for input file: ";
        cin >> filename;
    
        fptr.open(filename.c_str(), ios::in);
    }
    And remember that to use a variable, you simply type its name. Never type its type.
    Thus, this is wrong:
    istream fptr.open(filename.c_str(), ios::in);
    And this is right:
    fptr.open(filename.c_str(), ios::in);

    Also realize that cin >> stops reading when encountering a space!
    To remedy that, use std::getline:
    Code:
    void getfile(ifstream& fptr)
    {
    
        std::string filename;
        cout << "Please provide filename for input file: ";
        std::getline(std::cin, filename);
    
        fptr.open(filename.c_str(), ios::in);
    }
    Oh and while we're at it: Don't remove parameter names
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    Alright, well I am gonna ask a few questions about this because I have just recently started C++; I hope you don't mind Elysia.

    void getfile(ifstream& fptr)
    1. Why does it matter that the '&' symbol is connected to ifstream rather then fptr?

    std::string filename;
    2. Why do I need to say std::, I thought saying 'using namespace std' allowed me to not type that?

    fptr.open(filename.c_str(), ios::in);
    3. I saw .c_str() used in my book but it was unclear whats it purpose is. I also saw .data() and I am currently using that. What is the difference between the two and what is the purpose of the () at the end of them?

    Oh and while we're at it: Don't remove parameter names
    4. It is not required but a good idea? Sort of like a comment in the code?



    Thanks, I have been reading about C++ but this is really my first go at trying to write a decent program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 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. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 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

Tags for this Thread