Reading from text file to int vector

i am working on generating vector from file of integers. the broblem that i need to read one by one integer. the integers are random constructed between 0 to 5000. here is what i have: thanks
//****************************************************************************
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <locale>
#include <time.h>



using namespace std;

int main()
{
string filename = "filename.txt";
int num, i,x ,j, max, y, temp;
string answer, ans;
srand(0);

int arrys[1000];

string line;
ifstream inFile;
ofstream outFile;
// aske the use for file name;
cout <<"Please enter File name:" <<endl;
cin >> filename ;

// now open the file for writing

outFile.open(filename.c_str());

//chacking if the file is exist and can be open nomally.

if (outFile.fail())
{
cout <<"File was not succesfully open " <<endl;
cout << "Please check if this file is not exist?"<<endl;
}

//Chacking if the file is exist and can be open nomally
// if does exist it will give the option to ovewrite or not.

if (!outFile.fail ())////////////////////////////////////

{
cout <<"File succesfully open for writing" <<endl;
cout << "Would you like to over-witing it (Y / N) ?"<<endl;
cin >> answer;

if (answer == "n")
{
cout << "the exist file will not overwitten"<<endl;
exit(1);
}
if (answer == "y" || answer == "Y")
{
cout <<"File succesfully open for writing" <<endl;
cout <<"the exist file will be overwitten" <<endl;
cout <<"\nPress any Character to start the program" <<endl;

srand(0);

for(i = 0 ; i < 1000 ; ++i)
{
num = rand() % 5000;
cin >> num;
arrys[i]= num;
}

cout<<"\n";

// sortAr(arrys, 1000); // function to sort data before it sent out

for(i = 0 ; i < 1000 ; ++i) // display the numbers inorder

{
outFile << left <<setw(5) <<arrys[i]<<left <<setw(5);
}

outFile.close();

////////*****************************************************
inFile.open(filename.c_str());
cout<< "This is the containt of file.txt"<<endl;
while (getline(inFile, line)) // as lone the file is proccess
// print to console all element in file
cout<<line<<endl;
system("PAUSE");
////********************************************************

inFile.open(filename.c_str());

char c;
int size = 1000;
string nums[size];
for ( i = 0; i < size; i++)
{
inFile.read(&c, 1);
if (inFile.good())
{
nums[i] = c;
cout<< nums[i]<< " #"<< i<<endl;
while( inFile.fail() )
cout << "Could not connect, try again: "<<endl;
inFile.open( filename.c_str() );
system("PAUSE");
}
else
cout << "Could not read byte #" << i << endl;
// inFile.clear();
}
//binary_tree_node<int>* root;
inFile.close();
}
system("PAUSE");
return 0;
}
}
Change getline(inFile, line) to inFile >> x;
Then cout << x << endl;

getline uses '\n' as its delimiter or stop. So it reads a line until '\n' then saves it to your variable. The inFile >> x will go to the first white space character and input it into variable x.
Topic archived. No new replies allowed.