-1

I started to learn C and I am writing a code where the user needs to type a string he wants to search in the csv file , and if the string was found , it will print the number of line the string was found at . and I am not exactly sure what is the best way to do that

mich
  • 1
  • 4
    Please provide a minimal attempt. You probably want to use fopen to open a file, fgets to get string, count lines, and search lines. see: http://stackoverflow.com/questions/3501338/c-read-file-line-by-line and: http://stackoverflow.com/questions/9206091/going-through-a-text-file-line-by-line-in-c – ChuckCottrill May 08 '17 at 21:05
  • Please provide some code here first which shows an attempt at solving this problem. – Nguai al May 08 '17 at 21:05
  • This sounds like 'run `grep -n` on the CSV file'. If that's not appropriate, you need to think about how you'll do it. Probably open the file, read lines, keeping a count, and searching each line (and closing the file at the end). Sounds pretty routine — `fopen()`, `fgets()`, `strstr()`, `printf()`, `fclose()`. [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) — See also MCVE ([MCVE]). – Jonathan Leffler May 08 '17 at 21:16

1 Answers1

0

You may read line by line using getline() until it returns -1 (EOF), keeping a counter on how many iterations you have done.

For each line you read, you can use strstr() to check if the string is present. If yes, just print the counter (that will correspond to the line).

Tu.Ma.
  • 1,325
  • 10
  • 27