comparing strings.

This is a discussion on comparing strings. within the C Programming forums, part of the General Programming Boards category; I am making a program that will check a documents nd print the number of times that it had the ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    comparing strings.

    I am making a program that will check a documents nd print the number of times that it had the word 'pets'.
    The program runs ok, but it prints: I got the word: <null>.
    The code is included please let me know what I am doing wrong.
    Thank you

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    
    int main (){
    	char c;
    	char inputText[500];
    	int countSame=0;
    
    	
    
    	FILE* inputFile;
    
    
    	inputFile=fopen("C:\\Documents and Settings\\User\\My Documents\\GPA.txt","r");
    
    	if (inputFile==NULL){
    		printf("Failed to open");
    		exit(-1);
    	}
    
    	while ((c=scanf(inputFile))!=EOF){
    	fscanf(inputFile,"%s",inputText);
    	printf("I got the word: %s\n",c);
    	}
    	
    
    		if( strcmp(inputText, "pets" ) == 0 ) {
    			countSame++;
    		}
    	
    
    	printf("There are %i matches for pets\n", countSame);
    	system("Pause");
    
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Code:
    	while ((c=scanf(inputFile))!=EOF){
    	fscanf(inputFile,"%s",inputText);
    	printf("I got the word: %s\n",c);
    	}
    You read into inputText, you output c. Bad.
    Also see http://cpwiki.sourceforge.net/Talk:Buffer_overrun
    You are reading from the file in an unsafe way.
    Indentation may require a little work, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    what should I have it output instead of c. strg?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    You're lying to printf. You're telling it you want to print a string, yet you pass along a character. And what's worse is that you are passing its value and not the address.
    To print characters, use &#37;c. Otherwise pass a string (char array).
    Also see http://cpwiki.sourceforge.net/A_pointer_on_pointers
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 11:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 10:20 AM
  4. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 01:10 PM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 11:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21