Splitting up a file

This is a discussion on Splitting up a file within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. On the project im working on, I have a file which is getting rather large, full of functions, ...

  1. #1
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428

    Splitting up a file

    Hello everyone. On the project im working on, I have a file which is getting rather large, full of functions, that I would like to split up into multiple files. Generally, this wouldn't be a problem at all, but I've run into something that I can't figure out. Each function in the one big file now uses one or more file streams for file I/O. When I put half of the functions into another file, I get the error that they couldn't find the objects (streams), because they were defines in the first file. If I put all the stream object delarations in a .h file, then include it to both of the now split up function files, it will give me redefinition errors. I've been trying lots of things, but can't figure it out. Anyone know how I can make it work? Thanks.

  2. #2
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    I've been having the same problem until today. I finally figured it out. What compiler are you using?

  3. #3
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Im using MSVC++ 6 Standard.

  4. #4
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Ok, I'm using VC++ 6.0 Enterprise, so the method should be the same, but I'm not sure if this is how you are supposed to do it, it was all guess and hope for no errors.

    I had made a poker game, blackjack game, and a menu screen. I wanted to be able to call the blackjack main from the menu. I renamed main blackjackmain() and I made a function prototype for it. Then I added the function prototype for blackjack main in my menu file, and then I was able to call it. To make all the files in the same project, I added C++ source files and pasted my code from earlier projects.

    This took me forever. It turns out that you don't need to use any include statements to link the files together, VC++ does it for you. If this doesn't work perfectly, just mess with it. good luck, hope this helps.

  5. #5
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Hey, thanks for the tip, although im not sure if its what im looking for. Its the actual file streams that are causing the problem, as I declare them like:

    ofstream blah(blh.txt);

    but I dont know how to be able to use 'blah' in different functions that are in multiple files. I may have misunderstood your responce, so if I did, sorry about that . Thanks.

  6. #6
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Declare blah in one file and declare extern blah in the others. I think. I'm not too sure, but maybe if someone that has more than 6 months of c++ expericence would step in, you would get a good answer. Sorry if I didn't explain it too well, I'm learning by trial and error.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    The only thing I can think of is to resort your includes:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include "headers_that_require_fstream.h"
    or if you have blah as a global
    Code:
    #include <iostream.h>
    #include <fstream.h>
      ofstream blah;
    #include "headers_that_require_blah.h"
    or I could be wrong
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  8. #8
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    K, well their global, so if I put the function declarations below the streams? Ill try it, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. 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