Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在寻找一种方法来确定某个文件是否包含某个字符串。它可以是系统调用或 C 函数,没关系。
我尝试使用 grep,但它没有返回任何内容
//name is the directory entry name char grepcmd[150]; strcpy(grepcmd,"grep -c hello "); strcat(grepcmd, name); int status = system(grepcmd);
你做得很好。如果您给定的文件名称包含字符串status,则应为零。否则它应该是非零值。namehello
status
name
hello
如果您准备进行系统调用,那么只需mmap()文件并调用类似 strnstr(). (您将无法调用 real strnstr(),因为它会停\0在您文件中的任何位置,因此您必须自己编写。)
mmap()
strnstr()
\0