2016-04-04 4 views
-2

文字列の上にある行を削除するにはどうすればよいですか?たとえば、私はテキストファイルを開いて編集しています。私のプログラムの仕組みは、ユーザーが教師の名前を入力することです。 "Mr. Doe"その後、検索された名前から次の7行が削除されます。 "Courses"と "endInstructor"の間のすべてを探して削除しますが、検索された名前の上にある2つの文字列は "loginpw123"と "startInstructor"です。文字列の上の行を削除するには

私の質問は、それがすべての教授のために異なっていて、リストに約20個のプロファイルがあるので、他のものがすべて削除された後、それらの2行を削除する方法です。

int main() 
{ 
    ifstream myfile("instructor.csv"); 
    ofstream myfile2("outfile.csv"); 
    string line; 
    string dropInstructor; 
    bool notFound = false; 

    cout << "Enter Instructor to drop by name: "; 
    getline(cin, dropInstructor); 

    while (getline(myfile, line)) 
    { 
     if (line != dropInstructor) 
     { 
      myfile2 << line << endl; 
     } 

     if (line == dropInstructor) 
     { 
      myfile2 << ""; 
      for (int i = 0; i < 7; i++) 
      { 
       getline(myfile, line); 
       myfile2 << ""; 
      } 
      getline(myfile, line); 
      if (line == "Courses:") 
      { 
       myfile2 << ""; 
       while (getline(myfile, line)) 
       { 
        if (line == "endInstructor") 
        { 
         myfile2 << ""; 
         break; 
        } 
        myfile2 << "tempp"; 
       } 

       /*for (int i = 0; i < 4; i++) 
       { 
        getline(myfile, line); 
        myfile2 << ""; 
       }*/ 
      } 
     } 
    } 

    /*if (!notFound) 
    { 
     cout << "Not found" << endl; 
     cin.get(); 
     // client ->sendToClient("Not Found"); 
    }*/ 

    myfile.close(); 
    myfile2.close(); 

    remove("instructor.csv"); 
    rename("outfile.csv", "instructor.csv"); 

} 

テキストファイル:

loginpw123 
startInstructor 
Mr. John Doe 
755 Teacher St 
532 791 3761 
[email protected] 
392817 
03/02/1988 
Male 
777777 
Courses: 
Courseinfo1 
Courseinfo2 
Courseinfo3 
Courseinfo4 
Courseinfo5 
endInstructor 
+0

1.リードライン:

はこのような何かを試してみてください。 2.行を削除します。 3.ベクトルの内容をファイルに書き戻し、元の内容全体を置き換えます。 –

+0

あなたの具体的なC++の質問は何ですか?これを実装するための論理アルゴリズムを既に開発していて、その一部をC++に変換する方法がわからない場合は、これまでに書いたコードをすべて投稿して、欠落しているものを説明する必要があります。これを実装するための論理的アルゴリズムを思いつく方法がわからない場合、あなたの質問はC++とは関係ありません。 –

+0

@SamVarshavchik私はすでにコードを持っていますが、私は今それを更新します。コードを投稿してはいけないとわかったので、私のためにやり遂げたかったように思われたくなかった。しかし、今更新する予定です。 – DavidA

答えて

1

あなたは彼らが属するかどうかを判断する前にあなたが新しいファイルに最初の2行をコピーしている。ここ

コードです削除されている教授(startProfessorの上にある行がstartProfessor/endProfessorの間にないのはなぜですか?)最初の行を一時的にメモリに保存してから教授を確認し、一致しない場合は教授に関連するすべての行を新しいファイルに書き込む必要があります。文字列のベクトルに

int main() 
{ 
    ifstream myfile("instructor.csv"); 
    ofstream myfile2("outfile.csv"); 

    string line, lineAboveProfessor; 
    string dropInstructor; 
    bool ignore = false, found = false; 

    cout << "Enter Instructor to drop by name: "; 
    getline(cin, dropInstructor); 

    while (getline(myfile, line)) 
    { 
     if (line == "startInstructor") 
     { 
      if (!getline(myfile, line)) 
       break; 

      ignore = (line == dropProfessor); 
      if (ignore) 
      { 
       found = true; 
      } 
      else 
      { 
       myfile2 << lineAboveProfessor << endl 
         << "startInstructor" << endl 
         << line << endl; 
      } 

      while (getline(myfile, line)) 
      { 
       if (!ignore) 
        myfile2 << line << endl; 

       if (line == "endProfessor") 
       { 
        if (!ignore) 
         myfile2 << endl; 
        break; 
       } 
      } 

      lineAboveProfessor = ""; 
     } 
     else 
     { 
      lineAboveProfessor = line; 
     } 
    } 

    /*if (!found) 
    { 
     cout << "Not found" << endl; 
     cin.get(); 
     // client ->sendToClient("Not Found"); 
    }*/ 

    if (myfile1.eof() && myfile2.good()) 
    { 
     myfile.close(); 
     myfile2.close(); 

     remove("instructor.csv"); 
     rename("outfile.csv", "instructor.csv");  
    } 
    else 
    { 
     myfile.close(); 
     myfile2.close(); 

     remove("outfile.csv"); 
    } 
} 
+0

ありがとう、あなたの方法は働いている。メモリ内の行。私はコースと学生を落とすために同じ方法をしなければならないので、同じことをするでしょう。ありがとう! – DavidA

+0

私はあなたが私を助けたものを実装しましたが、ユーザープロファイルの間にスペースが必要です。myfile2 << endlを追加しようとしました。 2番目のwhileループの後に、1つではなく2つの改行を作成します。どのように私はこれを修正することができますどのような考え?ありがとう! – DavidA

+0

@DavidA内部のwhileループの内側でなければなりません。私は私の例を更新しました。 –

関連する問題