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?