Tuesday, July 28, 2009

How do i write this function in c programming i have no idea how to start?

fgets_generalized which takes in same arguments as fgets except for another argument (which is the last


argument), a single char variable (call it term), which will be the termination character. That is, it will read


in input, until term is encountered. This could be over multiple lines, unlike fgets which reads in only a


single line. The termination character must also be present as the last character of the string. In the main() function, obtain the termination character from the user and then use fgets generalized to get


the string from the user

How do i write this function in c programming i have no idea how to start?
The format for the fgets function is:





char *fgets(char *string, int n, FILE *stream);





The format for the fgets_generalized function should be:





char *fgets_generalized(char *string, int n, FILE *stream, int ch);





where "ch" is the terminating character.





The following code should help you out. The idea is to use the function or the family of functions "getchar"





char *fgets_generalized(char *string, int n, FILE *stream, int ch)


{


int i, chRead;





/*read in characters from "stream" until terminator is found,


the required number of characters has been read or an error


occurs*/


for( i = 0; (i %26lt; n - 1) %26amp;%26amp; ((chRead = getc(stream)) != EOF) %26amp;%26amp; (chRead != ch); i++)


string[i] = (char)chRead;





string[i] = (char)ch; /*close the string, you may remove this


line if you don't want to close the string, or modify it to close


the string with the NULL terminator*/





return string;


}





Hope this helped. If you need some premade code of C++ you can visit my website negyware.110mb.com
Reply:You can check next character in ahead to see whether it is end of line, paragraph or there is no character after that. for example, your text like this..." Hello My name is John Smith." if you are getting first character by function fgets, which is "H" then define another fgets function inside the function itself to check next character on the line input. If it is end of then string, line or paragraph then check next character to that. Based on this logic you can define your break condition in C. Try to read to the following link that might give you more information of better use and understanding to solve your problem

crab apple

No comments:

Post a Comment