Spliting among 2 files

This is a discussion on Spliting among 2 files within the C++ Programming forums, part of the General Programming Boards category; I have a file of 10 different names. It is read into a program and the names are split into ...

  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 12:38 AM.

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Because these lines:

    Output1 << loaded_names << " ";
    Output2 << loaded_names << " ";

    Just output the same string over and over and over. loaded_names isn't changing inside those loops.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> #include <iostream.h>
    Is there a reason you're using the non-standard header here, but not for the other headers? Also, the std namespace is missing, which you should need for string and ofstream anyway. Even if that code somehow compiles for you, you should consider correcting it to proper, standard C++ so that others can compile and run your code easily and help you with your issues.

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    if (! names)
    {
        cout << "File not opened" << endl;
    }
    Do you really wish to continue if this happens?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    for (int i = 0 ; names >> loaded_names ; i++)
    {
        amount ++ ; // counts # of names in file
    }
    This only stores a single name in the variable loaded_names (the last name from the file is what stays in loaded_names after the loop is done). You need a container (vector perhaps) to store all occurances of the various names that you will read in from the file.
    "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

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    Thanks for the tips, as for all the #include <iostream.h> I will get a compile error in Dev 4 when I ommit the .h on some libraries, but not on all of them. I also use this file for practice, before I worked on vectors so I just leave in all the #includes even If I am not currently using one, ex vectors in this case..

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You probably need a new copy of Dev-C++... 4.9.9.2 is the most recent release. You should also have using namespace std or prefix stuff in the standard library with std::.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

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, 10:46 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01: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, 04:46 AM

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