hey, I am a bit puzzle right now. I have a text file I must search to find a number 0
where am I going wrong?
Code:while(feof(fptr)) { fscanf(fptr,"%s",zeros); if (zeros == "0") { z++; } }
hey, I am a bit puzzle right now. I have a text file I must search to find a number 0
where am I going wrong?
Code:while(feof(fptr)) { fscanf(fptr,"%s",zeros); if (zeros == "0") { z++; } }
Because zeros == "0" can never be true. If you want to compare the contents of two strings, use the string compare function strcmp.
while(feof(fptr))
FAQ > Explanations of... > Why it's bad to use feof() to control a loop
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
You can find the reference for strcmp() here.
Do note, however, that strcmp() will return NULL when the strings given are identical.
> Do note, however, that strcmp() will return NULL when the strings given are identical.
Zero actually, not NULL.
Other string functions return a NULL pointer in some cases, but strcmp() returns 0, something >0 or something <0
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
fgets leaves \n char in the buffer - so strcmp will never give positive resultYou'd want something like this, then
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Been a while since I've done file I/O, can you tell?
Yes, vart is correct. You'll have to strip the \n from the string. There should be something about it in the FAQ, IIRC.
thanks guy I got it done