Thread: char by char

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    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
    Only the strong survives.

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    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
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Use the strcmp() function to compare the two strings.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    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
    Last edited by xlordt; 10-22-2003 at 12:38 AM.
    Only the strong survives.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    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
    Only the strong survives.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    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
    Only the strong survives.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM