読む必要があるテキスト ファイルがあり、各行は IP、URL、そして最後に日付であり、すべて空白で区切られています。クラス オブジェクト「Visitor」の適切な変数に割り当てられた各情報を取得したいと考えています。それらを保存するために、それらの配列があります。私の問題は、テキストファイルと行を調べようとするときです...すべてのテキストの間に空白が入り続けます。
class Visitors{
public:
string IP;
string URL;
string dateAccessed;
};
int main(){
Visitors hits[N];
string filename, theLine;
ifstream infile;
cout << "Enter file name (with extension):" << flush;
while(true){
string infilename;
getline(cin, infilename);
infile.open(infilename.c_str());
if(infile) break;
cout << "Invalid file. Please enter valid file name: " << flush;
}
cout << "\n";
while(!infile.eof()){
getline(infile, theLine);
istringstream iss(theLine);
do{
string ip;
string url;
string date;
iss >> ip;
iss >> url;
iss >> date;
if(ip != "\n"){
cout << "The IP: " << ip << endl;
}
if(url != "\n"){
cout << "The URL: " << url << endl;
}
if(date != "\n"){
cout << "The DA: " << date << endl;
}
}while(iss);
}
return 0;
}
if ステートメントを使用して、単なる「改行」であるすべての文字列を取得して無視しようとしましたが、うまくいかなかったため、それらを無視する方法が完全にはわかりません。また、情報のいずれかが間違っているかどうかを確認するためのチェックを追加したいと考えています (日付が 2/2/4 文字ではない、URL に www. がないなど)。
これは、私の問題をよりよく示すためのサンプル出力です...
Enter file name (with extension):hits.txt
The IP: 192.168.1.101
The URL: www.cs.stonybrook.edu
The DA: 01/01/2013
The IP:
The URL:
The DA:
The IP: 192.168.1.101
The URL: www.cs.stonybrook.edu
The DA: 01/01/2013
The IP:
The URL:
The DA:
The IP: 123.112.15.151
The URL: www.cs.stonybrook.edu
The DA: 01/01/2013
編集: わかりましたので、各行を調べて分割し、オブジェクト配列のクラス変数に属する場所に応じて文字列を追加する方法を理解しました。ここでの問題は、各行の各文字列のエラーをチェックすることです (たとえば、日付が不可能であるか、IP の数字の 1 つが 256 であるなど)。そのエラーを発見したら、次の行にスキップして同じチェックを行い、すべてがうまくいけば、配列内の適切な位置でクラス変数を初期化します。これが、私がやろうとしていることを理解するための私のコードです...
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#define N 50
using namespace std;
class Visitors{
public:
string IP;
string URL;
string dateAccessed;
};
int main(){
Visitors hits[N];
string infilename, filename, ip, url, date;
ifstream infile;
int i = 0;
cout << "Enter file name (with extension):" << flush;
while(true){
infilename = "";
getline(cin, infilename);
infile.open(infilename.c_str());
if(infile) break;
cout << "Invalid file. Please enter valid file name: " << flush;
}
cout << "Loading " << infilename << "..." << endl;;
cout << "\n";
while(infile.good()){
string line;
getline(infile, line);
stringstream ss(line);
if(ss >> ip >> url >> date){
cout << "The IP: " << ip << endl;
hits[i].IP = ip;
cout << "The URL: " << url << endl;
hits[i].URL = url;
cout << "The DA: " << date << endl;
hits[i].dateAccessed = date;
i++;
}
else{
cerr << "error" << std::endl;
}
/*
if(ip.length() > 15 || ip.length() < 7){
cout << "Found a record with an invalid IP format (not XXX.XXX.XXX.XXX)...ignoring entry";
}
//if(any of the numbers in the IP address are great then 255)
//INVALID IP...IGNORE ENTRY
else{
cout << "The IP: " << ip << endl;
hits[i].IP = ip;
}
//if(url doesnt start with www. or doesnt end with .xxx)
//INVALID URL...IGNORE ENTRY
else{
cout << "The URL: " << url << endl;
hits[i].URL = url;
}
//if(date.length != 10)
//INVALID DATE FORMAT...IGNORE ENTRY
//if(first 2 numbers in date arent between 01 and 12
//OR if second 2 numbers arent between 01 and 31 depending on month OR etc.)
//INVALID....IGNORE ENTRY
else{
cout << "The DA: " << date << endl;
hits[i].dateAccessed = date;
}
i++;*/
}
return 0;
}
明らかに、実際にプログラムでどのように編成またはまとめられているわけではありませんが、それは私が達成しようとしていることの一般的な考えです. 私の最大の問題は、配列内の私の位置を邪魔せずにファイル内の行をスキップする方法、またはすべての行にエラーがある場合、すべての行をキャッチすることです。