This code basically creates a txt file and add names and if users inputs new name it adds to txt file but it doesn't see the last letter like jack -> jac . Btw it saves names perfectly in txt file. So issue has to be at dosyaOku()
.This informations must be enough but thanks to stackoverflow I need to add some more details.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char allFile[1000][100],userName[100];
int i = 0;
int main()
{
addSomeNames();
dosyaOku();
isimAl();
isimKarsilastir();
dosyaOku();
return 0;
}
void isimKarsilastir()
{
int a,ayni = 0;
for (a=0 ; a < i; a++)
{
if (strcmp(allFile[a],userName) == 0)
{
printf("ayni\n");
ayni++;
}
else
{
printf("farkli\n");
}
}
if (ayni>0)
{
printf("Kullanici adi bulunmustur\n");
}
else
{
dosyaYaz();
}
}
void dosyaYaz()
{
FILE * ptrfile;
ptrfile = fopen("sondokunus.txt","a");
fprintf(ptrfile,"\n%s",userName);
fclose(ptrfile);
}
void isimAl()
{
printf("Isim giriniz: ");
gets(userName);
printf("Girdiginiz isim: %s",userName);
}
void dosyaOku()
{
FILE *ptrfile = NULL;
int *ptri;
ptri = &i;
*ptri = 0;
int top = 0;
ptrfile = fopen("sondokunus.txt", "r");
while(fgets(allFile[*ptri], 100, ptrfile))
{
allFile[*ptri][strlen(allFile[*ptri]) - 1] = '\0';
*ptri += 1;
}
top = *ptri;
printf("\n Dosyadaki isimler: \n");
for(*ptri = 0; *ptri < top; *ptri += 1)
{
printf("%s\n", allFile[*ptri]);
}
fclose(ptrfile);
}
void addSomeNames()
{
FILE * ptrfile;
ptrfile = fopen("sondokunus.txt", "w+");
char names[100][100];
strcpy(names[0],"mike");
strcpy(names[1],"joe");
strcpy(names[2],"jack");
for (int n=0;n <= 2; n++)
{
fprintf(ptrfile,"\n%s", names[n]);
}
fclose(ptrfile);
}