Thread: Spliting among 2 files

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Spliting among 2 files

    I have a file of 10 different names. It is read into a program and the names are split into 2 files.

    Here is the code:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <vector>
    #include <fstream>
    #include <string>
    
    int main()
    {
    
          ifstream names;
          names.open ("D:\\Documents and Settings\\Suchy\\Desktop\\names.txt");
          if (! names)
          {
            cout << "File not opened" << endl;
          }
    
          int amount = 0;
          string loaded_names; // holds names form file
          for (int i = 0 ; names >> loaded_names ; i++)
          {
            amount ++ ; // counts # of names in file
          }
    
          cout << "There are " << amount << " names in the file" << endl;
    
          int half = amount/2;
    
          cout << "Output1 will get " << half << " files and Output2 will get "
               << half << " files " << endl;
    
           ofstream Output1 , Output2;
    
           Output1.open ("D:\\Documents and Settings\\Suchy\\Desktop\\Output1.txt");
           for (int i = 0 ; i < half ; i ++)
           {
               Output1 << loaded_names << " ";
           }
    
           Output2.open ("D:\\Documents and Settings\\Suchy\\Desktop\\Output2.txt");
           for (int i = amount ; i > half ; i --)
           {
               Output2 << loaded_names << " ";
           }
    
          system("PAUSE");
          return 0;
    }
    The problem is that only the last name in the file (Frank is transfered into the 2 Output files. (They look like this after program runs: Frank Frank Frank Frank Frank )

    Why is it that only the last name gets distributed?
    Last edited by Suchy; 11-08-2006 at 01:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  5. spliting large files
    By stormbringer in forum C Programming
    Replies: 1
    Last Post: 11-19-2002, 05:46 AM