#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    1
    Rep Power
    0

    help, trying to get total number from input file


    Hello there,

    I am reading a input file into an array. Assume I don't know how many number in that file. How do I get total number from a input file?


    Here is my code:

    ifstream input_file;
    ofstream output_file;

    input_file.open("datafile1.txt"); // Attach input_file to a real file

    if(!input_file) // See if real file exists
    {
    cout << "Input file does not exist.\n";
    return 1;
    }
    else
    {
    for( i=0; i<size; i++)
    input_file >> a[i]; // Enter the array values

    input_file.close(); // close the file after reading data
    quicksort(a, size);
    output_file.open("test.dat"); // Open the real file for output

    for(i=0; i<size; i++)
    output_file << a[i] << " "; // output it
    output_file << endl;

    output_file.close(); // Close the output file when done

    where the "size" should be the total number of the input file.

    Thanks in advance
  2. #2
  3. No Profile Picture
    Dinesh_P_V
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    India
    Posts
    259
    Rep Power
    0

    help, trying to get total number from input file


    Read the file till end of file has been reached and count the loop index which will give you the size of the array

    HTH
    -Murugesan

IMN logo majestic logo threadwatch logo seochat tools logo