For some reason, no matter what text I write as input, the program will always return Succes, even if it's not in the .txt with the list of users,each separated by '\n'
My main issue is the " char b[20]; strcpy(b,inputUser); " line. If I comment it, the program will printf the inputUser badly, even thought that line has NOTHING to do with the rest of the code (was planning to use it to cut the '\n' from the userinput)
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
bool found=0;
int txtFD=open("users.txt",O_RDONLY);
char letter[2]; strcpy(letter,"");
char user[20]; strcpy(user,"");
char inputUser[20]; strcpy(inputUser,"");
read(0, inputUser,20);
char b[20]; strcpy(b,inputUser);
while( read(txtFD, letter, 1) && found==0 )
{
if(letter[0] == '\n') //am citit un user intreg, vad daca asta e
{
strcat(user,letter);
if( strstr(inputUser, user) == 0 )
{
cout<<"am gasit "<< inputUser <<" in lista.\n";
found=1;
}
strcpy(user,"");
}
else
strcat(user,letter);
}
if(found)
cout<<"Succes!\n";
else cout<<"Failure!\n";
}