file streams

This is a discussion on file streams within the C++ Programming forums, part of the General Programming Boards category; Would anyone be able to help me with the following problem. I am trying to create a generic function, which ...

  1. #1
    Kate
    Guest

    file streams

    Would anyone be able to help me with the following problem. I am trying to create a generic function, which accepts a stream name and the file name to open. The function output an error if it can't open the file. I've written some sample code to try and illustrate what I mean. Any ideas would be v.greatful thanks.

    #include <iostream.h>
    #include <fstream.h>

    const int MAX_BUFFER=256;

    void OpenFile(ifstream InFile, char FileName[MAX_BUFFER]);

    int main()
    {
    char FileName[MAX_BUFFER]="Test.txt";

    ifstream InFile;

    OpenFile(InFile,FileName);
    getchar();

    return 0;
    }

    void OpenFile(ifstream InFile, char FileName[MAX_BUFFER])
    {
    InFile.open(FileName);

    if(Infile.fail())
    cout << "\nCould not open file.";
    }

  2. #2
    Kate
    Guest

    Help

    Can someone please help me on this, I can't make the streams global.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try it like this.

    #include <iostream.h>
    #include <fstream.h>

    const int MAX_BUFFER=256;

    void OpenFile(ifstream &InFile, char FileName[MAX_BUFFER]);

    int main()
    {
    char FileName[MAX_BUFFER]="Test.txt";

    ifstream InFile;

    OpenFile(InFile,FileName);
    getchar();

    return 0;
    }

    void OpenFile(ifstream &InFile, char FileName[MAX_BUFFER])
    {
    InFile.open(FileName);

    if(InFile.fail())
    cout << "\nCould not open file.";
    }

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    ..or this -

    Code:
    #include <iostream> 
    #include <fstream>
    
    using namespace std;
    
    const int MAX_BUFFER=256; 
    
    void OpenFile(ifstream &InFile, char FileName[MAX_BUFFER]); 
    
    int main() 
    { 
    	char FileName[MAX_BUFFER]="Test.txt"; 
    
    	ifstream InFile; 
    
    	OpenFile(InFile,FileName); 
    	getchar(); 
    
    	return 0; 
    } 
    
    void OpenFile(ifstream &InFile, char FileName[MAX_BUFFER]) 
    { 
    	InFile.open(FileName); 
    
    	if(!InFile.is_open()) 
    		cout << "\nCould not open file."; 
    }
    You should use the standard headers, otherwise you'll have to set flags to prevent automatic creation of the file.

  5. #5
    Kate
    Guest

    Thanks

    Thanks for your help. Just couple of questions so I can learn from this. Why does the stream name have to be passed by reference, and what does "using namespace std" do? Thanks for your help, again.

  6. #6
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    the best things in life are simple.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    If the file stream is not passed by reference, then you are passing the variable by copy. This means that the file stream variable is a copy of what you passed into the function and the code will open the file like you think it should. After the function exits back to the piece of code that called the function, this local version of the variable will be destroyed and the variable you passed into the function hasn't changed at all. The end result is the file stream variable doesn't point to an open file after the function call because it was destroyed. Passing by reference means you are actually passing in the object itself and not a copy of the object so any changes to the object made within the function remain in effect after the function has exited.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Kate
    Guest
    Thanks, i'm with you now.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    or you could use

    if(!InFile)
    cout << "\nCould not open file.";

    right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  2. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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