-
NULL problem??
The below code is from my program. The variable 'input' is a line from a text file.
I'm trying to check if the line exists or not I am currently using NULL but it doesn't seem to work??
Am I correct using the NULL command or is there a specific command for checking whether that line exists within a text file??
Thanks
-
You would be better served using int strcmp( string1, string2 ).
It returns 0 if the two strings are the same.
-
NULL is exactly the same as 0. It's not a command.
-
Write a function that takes a string as an argument and returns 1 if the string exists, 0 if it does not.
Inside the function, run a loop that tests each character with the isgraph() function. isgraph is found in ctype.h
Hope that helps!
-
Thanks that has helped but how do I check if a string exists or not?
-
Are you trying to check for a blank line? If so, maybe:
Code:
bool is_blank_line(char *input)
{
int i;
if (strlen(input) == 0)
return 1;
else
{
for (i=0; i<strlen(input); i++)
{
if (!isspace(input[i]))
return 0;
}
}
return 1;
}
//In your main() function:
#include <ctype.h>
.
.
if (is_blank_line(input))
cout << "line is blank.\n";
else
cout << "line is NOT blank.\n";