LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C prog, null strings question (https://www.linuxquestions.org/questions/programming-9/c-prog-null-strings-question-666532/)

hk20 08-30-2008 03:07 PM

C prog, null strings question
 
Hello, I am having a problem determining when a string is empty:

ex: input file

"Test1"
"Test2"
""
"Test4"
"Test5"
""
"Test7"

How can I detect those 2 empty strings?

Note: The input has quotes around the letters.


#define QUOTES ""
int i;

while(fscanf(testStream, "%s", &testnum[i]) == 1)
i++;

for(i=0; i<20; i++)
{
result = strcmp(QUOTES, testnum[i]);
if(result == 0)
printf("found string\n"); //print i number of times quotes found.
}


the above only detects 5 strings, not the other 2.

Any ideas? Thank You.

ErV 08-30-2008 03:50 PM

about this:
Code:

#define QUOTES ""
This is empty string (length is zero), not string containing two quotes.
If you want two quotes, then you declare string like this:
Code:

const char* str = "\"\"";
or
Code:

#define QUOTES "\"\""
You can also try loading strings line-by-line using fgets.

hk20 08-30-2008 03:57 PM

Thanks for the help! That fixed it :)


All times are GMT -5. The time now is 08:09 PM.