Passing file stream as function parameter

This is a discussion on Passing file stream as function parameter within the C++ Programming forums, part of the General Programming Boards category; I am writing a programme to open a file, do some reformating of the data and write the result to ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    10

    Passing file stream as function parameter

    I am writing a programme to open a file, do some reformating of the data and write the result to an output file. Within function main the input file name is deconstructed and used to create the output file name, which is opened as an ofstream. Some header is written to this file, but the bulk of the writing, including a little bit of arithmetical calculation, I want to do in a function which will be periodically called from the main function.

    The problem is that I don't know how to pass the name of the output stream to the function. ofstream is declared in main, so its scope is limited to main and it is therefore not available as an object in the output writing function. I have tried adding an extra parameter to the function of type ofstream, but this creates lots of compiler errors in ios_base.h (I am using the Dev C++ compiler).

    Grateful for any advice on how to do this.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Can you paste some of what you tried, and indicate what you need to pass around?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Here is the function which I want to use to write to the output file
    (line_elements is a structure which is defined globally)

    Code:
      int  ann_bln_out (line_elements point1, line_elements point2, line_elements point3, \
      ofstream out_file_stream)
      // Point 1 is symbol loc, points 2 and 3 are used to calculate orthogonal offset
    {
       // Declare variables for calculation
       float bln_len = map_scale * 0.15/200*1.2;
       float dx, dy, hyp;
       float end_x_coord, end_y_coord;
       
       // Calculation of end coordinates
       
       dx = point3.x_coord - point2.x_coord;
       dy = point3.y_coord - point2.y_coord;
       hyp = sqrt(dx*dx+dy*dy);
       end_x_coord = bln_len * dy/hyp + point1.x_coord;
       end_y_coord = bln_len * dx/hyp + point1.y_coord;
       
       
       // Output to annotation file
       out_file_stream  << "2, 0 " << point1.sp_no << endl \
       << point1.x_coord << "," << point1.y_coord << endl \
       << end_x_coord << "," << end_y_coord << endl; 
    
      return(0);
    
    }
    Here are the parts of main where the output stream is created and the function is called

    Code:
      ofstream out_files_ann_bln (out_files_ann_bln);
    
       // Output first point to annotation file
       ann_bln_out (last_line, last_line, current_line, out_files_ann_bln);
    (last_line and current_line are variables of type line_elements)

    The error this creates in the compiler is:

    In copy constructor ` std::basic_ios<char, std::char_traits<char> >::basic_ios(const ` std::ios_base::ios_base(const std::ios_base&)' is private within this context

    I'm afraid I don't know enough of C++ to understand this or correct it.

    Grateful for your assistance.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Try passing your file stream by reference instead.
    Code:
    int  ann_bln_out (line_elements point1, line_elements point2, line_elements point3, \
      ofstream& out_file_stream)
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    You could try passing the ofstream object by reference.

    Code:
    int  ann_bln_out (line_elements point1, line_elements point2, line_elements point3, ofstream &out_file_stream)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    A reference to the stream looks like a good bet.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Many thanks for you rhelp.

    I would never have worked out this simple correction in a million years of looking at online tutorials...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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