0

C++でファイル操作を学んでいます。テストのために、この小さなコードを書きました。基本的に私がやりたいことは、ファイル処理を使用してユーザー アカウント プログラムを作成することです。if elseそのため、プログラムでorを使用whileして、ファイル内のデータを比較したいと考えています。

たとえば、ユーザーが自分のユーザー名を「john」と入力すると、プログラムはファイル内で john という名前を検索できる必要があり、存在する場合は、ユーザーがサインインしてパスワードを使用できるようにする必要があります。

私が書いたコードは単なるファイル書き込みテストです。実際のコードで私を助けてください。初心者なので変でしたらすみません。

ありがとうございました!

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<fstream>
#include<string.h>

using namespace std;
void main () {
  ofstream file;
  file.open("C:\tests1.txt", ios::trunc);

  char name[50];
  cout<<"\nEnter your name: ";
  cin>>name;
  file<<"Username: "<<name;
  file.close();
  cout<<"Thank you! Your name is now in the file";

  ifstream file("tests1.txt");
  if(file=="Username: Chinmay") {
    cout<<"If else successfull\n";
  } else {
    cout<<"If else failed\n";
  }
  _getch();
}

みんな私を助けてください!!

4

2 に答える 2

0
#include<iostream>
#include<conio.h>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
int main() {
    string name = "";
    string line = "";
    fstream f;
    f.open("a.txt");

    cout<<"Enter name"<<endl;
    cin>>name;
    if (f.is_open())
  {

      while (f.good() )
    {


         getline(f,line);
         if(line==name)
         {
            cout<<"You can log in";
                        //Or do whatever you please in here after the username is found in the file

         }
    }
    f.close();
  }
else 
  {
      cout << "Unable to open file"; 
  }

    getch();
    return 0;
}
于 2013-07-20T07:56:53.270 に答える