File streams

This is a discussion on File streams within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a function that will accept any given handle for a stream, e.g Code: void OpenFile(ifstream [name], ...

  1. #1
    zog&cog
    Guest

    File streams

    I'm trying to write a function that will accept any given handle for a stream, e.g

    Code:
    void OpenFile(ifstream [name], char FileName[256]);
    
    #include <iostream.h>
    
    int main()
    {
      const char FILE_STREAM[]="InFile", FILE_NAME[]="Test.txt";
    
      OpenFile(FILE_STREAM,FILE_NAME);
    
      getchar();
     
      return 0;
    }
    
    void OpenFile(ifstream [name], char FileName[256])
    {
      ifstream name;
    
      name.open(FileName);
    
      if(!name)
        cout << "Could not open the file.";
    }
    Any ideas?

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Include fstream.

    Why do you need the stream name (not file but the ifstream name) to be decided by the user? It will have no effect on the program.

  3. #3
    zog&amp;cog
    Guest
    i just want to write a genric function that can have multiple handles passed. I don't want to be declaring streams in other functions.

  4. #4
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    How would you encrypt a file like this*I got something like this, I want to encrypt it so that no one can *ahem*cheat*ahem* on my RPG*???

  5. #5
    zog&amp;cog
    Guest

    Unhappy

    would some one be able to help me on this, i would be v.greatful

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    Check out the greatest File streams tutorial at:
    http://www.cpp-home.com/FileIO_tutorial.php

    It explains everything!
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i just want to write a genric function that can have multiple handles passed.
    You want a function that can take any number of fstream objects?

    >It explains everything!
    Except how to avoid undefined behavior.

    -Prelude
    My best code is written with the delete key.

  8. #8
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    For the undefined behavior, just use the try and catch blocks.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >For the undefined behavior, just use the try and catch blocks.
    Actually, I was referring to the use of void main. And for the record, undefined behavior is no less undefined by wrapping it in exception tests.

    -Prelude
    My best code is written with the delete key.

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