Search a .csv file for name match in C

2019-07-21 07:01发布

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 

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?

Thanks in advance!

1条回答
可以哭但决不认输i
2楼-- · 2019-07-21 07:19

You can use the result of strstr(database, "isaac"); to find "isaac" in your database[].

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.

A combination of strtok and strstr or strcmp may be what you want.

查看更多
登录 后发表回答