Atwrkhrdlywrking

This is a discussion on Atwrkhrdlywrking within the C++ Programming forums, part of the General Programming Boards category; //HOW DO YOU MAKE IT SO THE FILE WILL OUTPUT SOMETHING //IF THE FILE IS EMPTY #include <iostream> #include <fstream> ...

  1. #1
    WORKBREAK
    Guest

    Atwrkhrdlywrking

    //HOW DO YOU MAKE IT SO THE FILE WILL OUTPUT SOMETHING
    //IF THE FILE IS EMPTY
    #include <iostream>
    #include <fstream>

    using namespace std;

    main()
    {

    int sum = 0;
    int x;
    ifstream inFile;
    inFile.open("input.txt");


    if(!inFile)//can file be opened
    {
    cout<<"Sorry cannot open input file. Program terminating";
    exit(0);
    }



    //if(inFile.eof()) //file exists but is empty
    //{
    // cout<<"Your file is empty";
    // exit(0);
    //}


    while (inFile>>x)








    sum = sum + x;
    cout<<endl;
    cout<<sum;
    inFile.close();

    return 0;

    }

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    Try to read something, if you hit EOF right away then print that the file is empty.

Popular pages Recent additions subscribe to a feed

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