Hey im new in this..

I want to search the user input in file (by lines) but not all then with this line search on another file ( with the specific line) and show to the user.

Example:

file1.txt
=======
a
aa
aaa
aab
aac


file2.txt (corresponding md5 hashes of every text line)
======
0cc175b9c0f1b6a831c399e269772661
4124bc0a9335c27f086f24ba207a4912
47bce5c74f589f4867dbd57e9ca9f808
e62595ee98b585153dac87ce1ab69c3c
a9ced3dad556814ed46042de696e1849

========
Lets supposed to the user enter (want to crack) this hash: a9ced3dad556814ed46042de696e1849

im using

#!/bin/bash
linenum=$( grep -w -n a9ced3dad556814ed46042de696e1849 file2.txt | cut -f1 -d: )
sed -n "$linenum p" file1.txt

And works!

BUTTTT

Im using this commands in C with Pipes.. Here the code ...

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
    FILE *read_fp;
	int chars_read; 
        char searchash[1024] = "linenum=$(grep -n -w ";
	char buffer[BUFSIZ + 1]; 
	memset(buffer, '0', sizeof(buffer));  

	printf("Enter MD5 Hash: ");
	char hash;
	scanf("%s", &hash);

	strcat(searchash, &hash);
	strcat(searchash, " file2.txt | cut -f1 -d: )");


	read_fp = popen (searchash, "w");

	if (read_fp != NULL) {

		chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);

   pclose(read_fp);
}
    system("sed -n \"$linenum p\" file1.txt");
}

...anything will be helpful

Greetings.

Dani AI

Generated

Short summary and a safer pattern that avoids the fragility seen in the thread: started with a shell pipeline and then tried to reproduce it from C; cleaned that up and pointed out issues with buffer handling and using the wrong read functions. Calling out to the shell for the lookup (building a command that contains raw user input) is both error-prone and a command‑injection risk. A much more robust solution is to open the two files in C and stream them line-by-line in parallel: compare each MD5 string from file2 with the user input and print the matching line(s) from file1. That avoids popen/system, fixes newline/trimming problems, and handles multiple matches naturally.

Here is a compact, POSIX-friendly example that implements that streaming approach:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char *hash = NULL, *l1 = NULL, *l2 = NULL;
    size_t hcap = 0, c1 = 0, c2 = 0;
    ssize_t hl, r1, r2;

    printf("Enter MD5 hash: ");
    if ((hl = getline(&hash, &hcap, stdin)) <= 0) return 1;
    if (hash[hl-1] == '\n') hash[hl-1] = '\0';

    FILE *f1 = fopen("file1.txt","r"), *f2 = fopen("file2.txt","r");
    if (!f1 || !f2) { perror("open"); return 1; }

    while ((r1 = getline(&l1, &c1, f1)) != -1 &&
           (r2 = getline(&l2, &c2, f2)) != -1) {
        if (r2 > 0 && l2[r2-1] == '\n') l2[r2-1] = '\0';
        if (strcmp(l2, hash) == 0) {
            if (r1 > 0 && l1[r1-1] == '\n') l1[r1-1] = '\0';
            printf("%s -> %s\n", hash, l1);
        }
    }

    free(hash); free(l1); free(l2);
    fclose(f1); fclose(f2);
    return 0;
}

Notes and quick tips:

  • Trim newlines before comparing. CRLF will break strcmp on Windows-origin files.
  • If getline is not available, use fgets with a sufficiently large buffer and careful truncation.
  • For many repeated lookups, build an in-memory map (hash table) from file2→file1 once; that changes scans from O(n) per query to O(1).
  • Never concatenate raw user input into a shell command (popen/system) — sanitize or avoid the shell entirely.

Recommended Answers

All 4 Replies

See if this helps. BUFSIZ was not what you think. I replaced it with a hardcode value, changed it if you like.

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
    FILE *read_fp;
    FILE *read_sed;
	int chars_read; 
        char command[1024] = "grep -n -w ";
	char buffer[1024 + 1]; 
	memset(buffer, 0, sizeof(buffer));  

	printf("Enter MD5 Hash: ");
	char hash[500];
	scanf("%s", hash);

	strcat(command, hash);
	strcat(command, " file2.txt | cut -f1 -d:");

        /*printf("command: %s\n",command);*/
	read_fp = popen (command, "r");

    if (read_fp != NULL) {
       while ((chars_read = fread(buffer, sizeof(char),1024 , read_fp)) > 0 ){
          /*printf("buf: %s\n",buffer);*/
          sprintf(command,"sed -n \"%d p\" file1.txt",atoi(buffer));
          /*printf("command: %s\n",command);*/
	  read_sed = popen (command, "r");
          if ((chars_read = fread(buffer, sizeof(char), 1024, read_sed)) > 0 ) {
               printf("%s -> %s",hash,buffer);
          }
          pclose(read_sed);
       }
       pclose(read_fp);
    }
    return 0;
}

It Works Thanks a lot for your help!

Greetings.

If you ever get more than one result from the grep command, you will want to change the fread to fgets. Right now it might not work too well if two lines come back.
If you sure this can't happen, you should change it anyway.
I should have posted it with fgets.

If you ever get more than one result from the grep command, you will want to change the fread to fgets. Right now it might not work too well if two lines come back.
If you sure this can't happen, you should change it anyway.
I should have posted it with fgets.

yep it's true I saw some garbage... I changed it thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.