Member Avatar for mochno

Hello All.

After couple hours reading posts here and all over the web, i wasnt able to find example of code to "rewrite it" my way.

All i need is:

1. read the file (txt),
2. scanf for string,
3. compare each line for this string,
4. if found printf this line.

I know how to load the file and print it. But just cannot found the way to compare string user will type (using scanf?) and then print only that line.

file *fd;
  char symbols; 
  fd = fopen("file.txt", "r");
  if (fd == NULL) {
    printf("Cannot open file");
  } else {
    while( !feof(fd) ) {
      fscanf(fd, "%c", &symbols);
      printf("%c", symbols);
    }
    
    fclose(fd);
    getch();
    return main();
  }

i read about tokens, strcn but as i am newbie (couple days) i dunno how to implement them. will keep searching but if some one could help, that would be nice. I pretty sure this is super easy for all of you..

Recommended Answers

All 2 Replies

Member Avatar for v3ga

Ur code is 1 character at a time, u must take one line by line.
use stcmp to compare

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.