I am just dropping by like the wind, in the mood for some relaxing code. I'll attend to unsolved posts later.
This part I wouldn't reccomend:
Code:
while (!file.eof() )
Actually it *might* be wrong to do such a thing. I'll explain.
The EOF-bit is set when the file reaches PAST the end-of-file! So the while loop checks one time too often for the true end of file. That is not persé wrong. The program still works. In fact, with simple file i/o operations there is nothing wrong with this.
But in general...
The eof-bit might not always be set! Therefore, this kind of testing in a loop is not ok.
If the input is mapped to a device, then it is not possible for C++ to predict what the last input was.
I'll get to the solution in a moment. First another thing.
Code:
ifstream file (argc[1], ios::in);
if (!file.is_open())
In the first line, ios::in is not needed right now, since it is the default parameter for istream anyway. Just a heads up.
In the second line, testing explicitly with is_open() is not needed right now too. That means
style-wise it is not needed. Since the handle to istream already knows the name of the file, we can use the overloaded operator!() for the ifstream class.
See code below for "solution" to these issues. The app by itself deals only with files as presented in the very first post by Caleb.Carlton, wich assumes correct "IP:port" format. Tabs, spaces etc will be dealt with correctly as well. An IP like "000" isn't usually defined by many programs, but both 0.0.0.0:port as well as 000.000.000.00:port will work.
You could implement features to check for correct ip:port formats but that wasn't asked by the poster, so I didn't.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using std::cout;
using std::endl;
using std::ifstream;
using std::string;
using std::getline;
using std::size_t;
int main(int argv, char* argc[])
{
if(argv != 2)
{
cout << "Usage: " << argc[0] << " <filename>" << endl;
return -1;
}
ifstream file(argc[1]);
if (!file)
{
cout << "Error: opening file " << argc[1] << endl;
return -1;
}
string ipNumber;
while ( file>>ipNumber )
{
size_t pos;
pos = ipNumber.rfind(":");
ipNumber.erase(pos, ipNumber.size()-1);
cout << ipNumber << endl;
}
//If file-reading loop exits before an EOF has been reached, something is wrong. if(!file.eof())
{
cout<<"ERROR: Something went wrong during processing of file."<<endl;
}
file.close();
return 0;
}