-
char by char
Ok what i want to start coding is a program that will open a file for reading.. so that i can compare it with the users inputs...
the user will be able to enter upto 256 char's.. but im a bit confused on how to compare the users input with the file, char by char. Can anyone help me out on this please
-
You can get the users input using fgets. Then just compare the string the user gave one char at a time by keeping an index and moving through the string. You can get the next char from an open file by using fgetc.
Code:
for(int index=0;index<256;index++){
if(fgetc(infile)==UserInput[index])
//Do something
}
- Sean
-
Use the strcmp() function to compare the two strings.
-
hey guys.. thanx for the reply.. I have done what you told me to do but i keep getting the WRONG part of my code.. can anyone tell me what im doing wrong?
Code:
int main( )
{
int index;
int c;
char buff[MAX];
char buf[MAX];
char *file = "1.txt";
FILE *fp;
if(( fp = fopen( file, "r" )) == NULL )
{
printf( "-error: could not find file" );
exit( 1 );
}
printf( "Enter numbers:\n" );
fgets( buf, MAX, stdin );
// while(!feof( fp ))
// {
fgets( buff, MAX, fp );
for( index = 0; index < MAX; index++ )
{
if( fgetc( fp ) == buf[index] )
{
printf( "%s", buff );
break;
}else{
printf( "WRONG\n" );
break;
}
}
//}
getchar( );
return 0;
}
btw i found this site that maybe usefull to others :)
c functions thought maybe it would be good to share it
-
Quote:
Originally posted by xlordt
hey guys.. thanx for the reply.. I have done what you told me to do but i keep getting the WRONG part of my code.. can anyone tell me what im doing wrong?
Code:
int main( )
{
int index;
int c;
char buff[MAX];
char buf[MAX];
char *file = "1.txt";
FILE *fp;
if(( fp = fopen( file, "r" )) == NULL )
{
printf( "-error: could not find file" );
exit( 1 );
}
printf( "Enter numbers:\n" );
fgets( buf, MAX, stdin );
// while(!feof( fp ))
// {
This statement reads a line from the input file
fgets( buff, MAX, fp );
for( index = 0; index < MAX; index++ )
{
This statement reads a character from the input file and
compares that character to characters from your string
of numbers entered above. What about the line you read
just before the for()?
I'll bet you want to compare the buffer buff and not fgetc()
if( fgetc( fp ) == buf[index] )
{
printf( "%s", buff );
break;
}else{
printf( "WRONG\n" );
break;
}
}
//}
getchar( );
return 0;
}
btw i found this site that maybe usefull to others :)
c functions thought maybe it would be good to share it
-
Ok i got it working now.. :) thanx.. mind for one more questions..
what im tring to do.. is once they have inputed the wrong, numbers or letters... i what the programm to show me the letter or number that i have inputed wrong.. is this possialbe?
here is the fix of my code
Code:
int main( )
{
int index;
int c;
char buff[MAX];
char buf[MAX];
char *file = "1.txt";
FILE *fp;
if(( fp = fopen( file, "r" )) == NULL )
{
printf( "-error: could not find file" );
exit( 1 );
}
printf( "Enter numbers:\n" );
fgets( buf, MAX, stdin );
fgets( buff, MAX, fp );
for( index = 0; index < MAX; index++ )
{
if( buff[index] == buf[index] )
{
if(!( strcmp( buf, buff )) == 0 )
{
printf( "-error: Could not compare inputs" );
break;
}else{
printf( "%s", buff );
break;
}
}else{
printf( "WRONG\n" );
break;
}
}
getchar( );
return 0;
}
Please remember.. that im still a newbie here :( tring to get up to your level
-
Quote:
Originally posted by xlordt
Ok i got it working now.. :) thanx.. mind for one more questions..
what im tring to do.. is once they have inputed the wrong, numbers or letters... i what the programm to show me the letter or number that i have inputed wrong.. is this possialbe?
here is the fix of my code
Code:
int main( )
{
int index;
int c;
char buff[MAX];
char buf[MAX];
char *file = "1.txt";
FILE *fp;
if(( fp = fopen( file, "r" )) == NULL )
{
printf( "-error: could not find file" );
exit( 1 );
}
printf( "Enter numbers:\n" );
fgets( buf, MAX, stdin );
fgets( buff, MAX, fp );
This loop seems to be the loop to check each input
character with each file character
for( index = 0; index < MAX; index++ )
{
.. and is verified by this comparison
if( buff[index] == buf[index] )
{
Then why, each time thru the loop, after
checking each character, you compare the entire string?
This comparison is done MAX times. Isn't once enough
to compare the whole string?
if(!( strcmp( buf, buff )) == 0 )
{
printf( "-error: Could not compare inputs" );
break;
}else{
printf( "%s", buff );
break;
}
}else{
printf( "WRONG\n" );
break;
}
}
getchar( );
return 0;
}
Suggestion -- Take the strcmp outside the for loop. If the strings compare, you are done. If they don't, then go thru the for() loop.
At the first miscompare, you know exactly how many characters compare because INDEX tells you -- print that many spaces and a special char to point to the error.
Also, why go thru the loop MAX times? Use strlen() to find out the length of one of the strings and use that in the for loop.
And close your file.
-
thanx man.. i will give it a try again soon.. you have been a great help.. right now im having a bad day at work :( so.. i sure hope i can get this working when i get home