Thread: A function to return a string

  1. #16
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    OK, Code version #7800091 a.
    It recognises the white space. I was comparing a char* to a char and it didn't like it too much.
    Now I check the character before assigning it to the array, isspace was escaping after the first character, but I was probably using it wrong. Thanks though.
    I hope there are no more major mistakes in it, because it run like it should.
    Code:
    int GetWord(FILE *inp, char *string)
    {
    	int i=0;
    	char ch;
    
    	ch = fgetc ( inp );
    
    	do
    	{
    		 string[i] = ch;
    		 printf("%c ", string[i]);
    		 i++;
    		 ch = fgetc ( inp );
    	}
    	while ( ch != ' ' && ch != EOF);
    
    	printf("\n\ni = %d\n", i);
    
    	if (string[i-1] == NULL)
    		return 0;
    	else
    		return 1;
    
    }

    Thanks guys,




    and I'm not and idiot, I'm learning a new language.

  2. #17
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You're still making the same error quzah pointed out above.
    Quote Originally Posted by quzah
    A char can NEVER contain the EOF marker.
    Also, how does GetWord know that it won't overrun the string buffer?
    Quote Originally Posted by pianorain
    Something you may want to add is a buffer length to your function; you don't want to be running off the end of your buffer.
    What if the file you open is an empty file?
    Code:
    int GetWord(FILE *inp, char *string)
    {
    	int i=0;
    	char ch;
    
    	ch = fgetc ( inp ); //might be EOF
    
    	do
    	{
    		 string[i] = ch; //whoops, EOF in the string
    		 //snip
    And how are you null-terminating your string?

    See what you can make of this pseudo-code:
    Code:
    //returns true if string contains a word
    //returns false if hit EOF and string does not contain a word
    int GetWord(FILE *inp, char *string, int len)
    {
    	//start index at 0
    	//while we have room in the string
    		//get the character from the file
    		//if at the end of file, break
    		//if found a space, break
    		//put the character into the string
    		//increment the index into the string
    	//end while
    	//null-terminate the string
    	//return a meaningful value
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #18
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I test the input file in the main function, there are more functions that use the file later.
    I'm not sure how to test for EOF other then the -1 int value it returns, how to compare it to the char that I read in.
    The output string will always be no more then 25 chars long, so I used that to hadle the buffer problem.
    I know exactly what the files contain and their format. At the end of the file there is actually a string "EOF", and then the actual EOF marker. I've tried doing the following with all of these chahges:

    Code:
    int GetWord(FILE *inp, char *string)
    {
    	int i=0;
    	char ch;
    do
    	{
    		ch = fgetc ( inp );
    		printf("%c ", ch);
    
    		if (ch == ' ')
    			break;
    		if (strcmp(string, "EOF") == 0)
    			break;
    
    		string[i] = ch;
    		i++;
    	}
    	while (i<25);
    
    	string[i] = '\0';
    
    	return 1;

  4. #19
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
    if (strcmp(string, "EOF") == 0)
    Unless you're initalizing your buffer to 0 every time, this is going to do some rather weird things. strcmp is expecting a pair of null-terminated strings. "string" is not null-terminated yet...that happens later in your function.
    Quote Originally Posted by earth_angel
    I'm not sure how to test for EOF other then the -1 int value it returns
    You could read the character into an int, test it for EOF, and then put the int into the string.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by earth_angel
    I'm not sure how to test for EOF other then the -1 int value it returns, how to compare it to the char that I read in.
    FAQ > Explanations of... > Definition of EOF and how to use it effectively
    Last edited by Dave_Sinkula; 06-10-2005 at 10:28 AM. Reason: Added quote.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I've read the EOF explanation, I wasn't sure if an int would get demoted properly to a char when put into the string. but there are no problems yet.

    Thanks.

    A.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM