搜索在C名称匹配.csv文件 [英] Search a .csv file for name match in C
问题描述
我目前有一个包含三个字段.csv文件:用户名,密码类型。例如,我的文件看起来是这样的:
I currently have a .csv file containing three fields: user, password, type. For instance, my file looks like this:
michael, sun123, user
joseph, sierra7, user
isaac, apple2, sysop
我想从这些文件中读取并检查用户艾萨克在列表present。到目前为止,我能够打开该文件,并把我的文件的每个字符转换成一个名为数据库[]一维数组。我已经设置指着我的数据库[]数组的第一个字符的指针。我的问题是:如何管理文件中找到艾萨克?
I would like to read from such file and check if the user "isaac" is present in the list. So far, I was able to open the file and put every characters of my file into a 1D array called database[]. I have set a pointer pointing to the very first character of my database[] array. My question is: How do I manage to find "isaac" in the file?
我的想法是,以检查是否我找了用户的第一个字符在我的数组一个字符匹配。如果是这样,我开始只要我不打一个逗号来检查下一个字符。当我打一个逗号,然后我检查是否有退出密码,一个逗号和使用布尔回车前一种类型。是否有这样做的更简单的方法?
My idea was to check if the first character of the user I am looking for matches a character in my array. If so, I begin to check the next characters as long as I don't hit a comma. When I hit a comma, I then check if there exits a password, a comma and a type before a carriage return using boolean. Is there an easier way of doing this?
在此先感谢!
推荐答案
您可以使用的strstr的结果(数据库,艾萨克);
找到艾萨克在你的数据库[]
。
You can use the result of strstr(database, "isaac");
to find "isaac" in your database[]
.
您可以使用 strtok的
(或它是安全的变体,这里详细的 http://msdn.microsoft.com/en-us/library/ftsafwz3(v = VS.80)的.aspx )打破你的数据库[]
成更易于管理的项目(即标记化的逗号和换行符),为您解析的需求。
You can use strtok
(or it's safe variants, detailed here http://msdn.microsoft.com/en-us/library/ftsafwz3(v=vs.80).aspx) to break your database[]
up into more manageable items (ie. tokenize on commas and newlines) for your parsing needs.
的组合 strtok的
和的strstr
或 STRCMP
可能是你想要的东西。
A combination of strtok
and strstr
or strcmp
may be what you want.
这篇关于搜索在C名称匹配.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!