私はテキストファイルの文字列を与えられており、それらを互いに比較しなければなりません - 最初の文字列とその下位の文字列を比較し、2番目の文字列と比較して比較したいそれ以下のすべてで、そうである。問題は、コードを書く方法がわかりません。ファイルからの文字列の比較
0
A
答えて
1
ネストループを使用すると、期待通りの結果が得られます。
#include <iostream>
#include <fstream>
#include <vector> //include this to use vector
using namespace std;
int main() {
//to take input from the file
ifstream fin;
//to read the same strings into 2 arrays so we can loop it appropriately
//by taking one string and comparing it to all below it.
vector <string> line1;
vector <string> line2;
//to hold a line of string
string temp;
//replace this with with your file
fin.open("hello.txt");
//to check if file cannot be opened or does not exist
if(!fin.is_open()) {
cout << "file could not be opened";
}
//strings are inserted into element of these 2 vectors
//(Internally, vectors use a dynamically allocated array to store their elements in adjacent memory locations)
//that is why i decided to use vectors. Also, using the push_back method
//to insert the strings into both arrays means we don't have to specify the size of the array
while (getline(fin, temp)) {
line1.push_back(temp);
line2.push_back(temp);
}
//nested loop is used to make sure one string is used to operate
//on all the strings in the file and move to the next to do same
//and so on...
for (unsigned int i = 0; i < line1.size(); i++) {
for (unsigned int j = 0; j < line2.size(); j++) {
//you can compare first string with all below here however you want to do it
//I just did this so you see how it behaves
cout << line1[i] << " = " << line2[j] << endl;
}
}
return 0;
}
0
それはgrepのようにCMDのLinuxを使用しているための最も簡単な方法:
// 1の方法
grep -w -v -f file1.log file.2 > mach.log
// 2ウェイ
grep -w -f file1.log file.2 > mach.log
あなたはmustnフラグを忘れないでください。平均:
-w、 --word-regexp 単語全体を構成する一致を含む行だけを選択します。テストでは、一致する部分文字列が行の先頭にあるか、または単語でない構成文字の前にある必要があります。 同様に、行の終わりにあるか、または単語に含まれていない構成文字が続く必要があります。単語構成文字は、英字、数字、アンダースコアです。
-v、--invert-match 不一致の行を選択するには、一致の意味を反転します。
-f FILE、--file = FILE FILEからパターンを1行に1つずつ取得します。このオプションを複数回使用するか、-e(--regexp)オプションと組み合わせて使用すると、指定されたすべてのパターンを検索します。空のファイルにはパターンがありません。したがって、 は何も一致しません。
関連する問題
- 1. ファイルからの入力文字列と入力文字列の比較
- 2. ファイルからの文字列読み込みと文字列リテラルの比較
- 3. 文字列比較
- 4. 比較文字列
- 5. 文字列の比較 - Java
- 6. Pythonの文字列比較
- 7. PHPの文字列比較
- 8. 文字列/パスの比較
- 9. PHPの文字列比較
- 10. DateTime、文字列の比較
- 11. Python文字列の比較
- 12. Java:文字列の比較
- 13. Javaの文字列比較
- 14. C++文字列の比較
- 15. グローブ文字列の比較
- 16. MD5文字列の比較
- 17. 文字列の比較C#
- 18. Ajaxの文字列比較
- 19. 文字列のブール比較
- 20. DXF文字列の比較
- 21. C++文字列の比較
- 22. C#の文字列比較#
- 23. C++文字列の比較
- 24. SAS文字列の比較
- 25. Qtの文字列比較
- 26. BASH:ファイルからの文字列、プログラムからの文字列の比較、ifステートメントは常に真です
- 27. if文の文字列の比較
- 28. Bashの文字列比較の構文
- 29. 回文用の文字列の比較
- 30. 文字列とPHPの文字列を比較しますか?
ここに質問を投稿する前に、いくつかの調査を行う必要があります。 Web上には多くのチュートリアルがありますので、 'C++ read text file':http://www.cplusplus.com/doc/tutorial/files/を検索し、文字列と' strcmp'をループ内で比較してください。 – Bhoke
私はこれらを読んでいる、事は - 最初の文字列を他の文字列と比較すると、どのようにして2番目の文字列に戻るのですか?ポインタをそこに取得するには、seekp()またはseekg()私が立ち往生した場所はかなりです – unfi
まず何かを始める必要があります。 **では、こちらに来てください。 –