Reading file in child process in C

bf16

So basically, I'm trying to read a file and see if any of the lines matches with a given string in a child process that I've created. The answer is then transmitted to the parent process that prints it out. My problem is that my code only works if I input the last line in my txt file. Any other line is reported as not being present in the file.

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<string.h>

void Login()
{
char name[20];
int pfd[2];
pid_t pid;

pipe(pfd);

if((pid=fork()) == -1)
{
 exit(1);
}

if (pid==0)
{
 close(pfd[0]);
 FILE *fp = fopen("loginuser.txt", "r");
 printf("name:");
 scanf("%s", name);
 int ok = 0;
 if (fp!=NULL)
{
 char line[20];
 while (fgets(line,20,fp) !=NULL)
{
 if (strcmp(name,line)==0) ok = 1;
}
 fclose(fp);
}
 char s1[]="logged in!";
 char s2[]="Not found!";
 if(ok==1) write (pfd[1],s1,strlen(s1));
 else write(pfd[1],s2,strlen(s2));
 exit(0);
}
else
{ 
 close(pfd[1]);
 int n;
 char reada[20];
 n=read (pfd[0],reada,sizeof(reada));
 printf("%s",reada);
}
}

int main (int argc, char *argv[])
{
 Login();
 return 0;
}

loginuser.txt

aaabs
fas
ttt
aloo

Inputing anything other than aloo ,in this case, won't give the correct result. I also have two extra characters after the parent prints something. Any ideea what I've done wrong and why it's not working as intended ?

Mekap

Fgets return the endtrail caracter in your line, meaning that you were actually comparing aaabs to aaabs\n, thus failing strcmp. If you do this instead :

      while (fgets(line,20,fp) !=NULL)
        {
          size_t i = strlen(line) - 1;
          if (line[i] == '\n')
            line[i] = '\0';

          if (strcmp(name,line)==0)ok = 1;
        }

It'll remove each '\n' with a '\0' instead, cleaning your buffer from endlines.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reading file in child process in C

From Dev

Child process not reading pipe

From Dev

Reading in child process

From Dev

Opening file in child process

From Dev

How to detect file activities of exec'ed child process on Linux in C?

From Dev

Reading data from child process one by one

From Dev

The file used by parent and child process

From Dev

Redirect a file stream of a child process

From Dev

Waiting for child process to terminate, or not - C

From Dev

C Programing : Parent & Child Process

From Dev

Memory usage of child process in C

From Dev

Creating child process in C linux

From Dev

File reading in C language

From Dev

reading from a file . C

From Dev

Writing and Reading to a file in C

From Dev

C reading a renamed file

From Dev

Reading a .CSV file in C

From Dev

Reading and printing a file in C

From Dev

C reading a file with null in it

From Dev

reading in a file and CryptUnprotectData in C

From Dev

Reading a file line by line and store it in a vector, process it

From Dev

Ruby: Reading from a file written to by the system process

From Dev

is it possible to process file reading and parsing in R

From Dev

Pipe issue about reading data from multi-child process

From Dev

C reading and writing with multiple child processes

From Dev

Redirecting stdout/stderr of a child process to a file

From Dev

"Error spawning child process: No such file or directory" Xcode

From Dev

"Failed to execute child process (no such file or directory)"

From Dev

Passing file or stdin to boost process child ambiguously