If you want to learn to use that function then fine you go ahead, but this OP is having enough problems with basic file handling, I don't recommend trying to use advanced concepts unless absolutly necessary.
Oh, and strtok_r() calls can be nested while strtok() calls cannot.
Two things, first strtok_r() is not a standard function, and may not be available with all compilers. Second the only major difference between the two is that you must provide the buffer for use with strtok_r().
The strtok_r() function is a reentrant version strtok(). The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string.
This post has been edited by jimblumberg: 07 May 2012 - 07:42 AM
Hello... Now I touching this project again. How I can read all files to just one variables in fgets and how I limit my string length in fgets? It will read whole character in file right?
And anyway, I remember someone told me to not using fflush(stdin) because it will makes undefined behaviour. But how I can remove buffer from my input if not using this? Because in my program if I try to stop my buffer with getchar() it works for sometimes like if I just input my data once. If I input it twice getchar() won't work and it will vanishing my 1st char.
I don't understand your question. You may want to restate clearly your problem. Maybe show a small program that illustrates your question/problem.
How I can read all files to just one variables in fgets and how I limit my string length in fgets?
The fgets ( char * str, int num, FILE * stream ) function reads at most num characters from the file stream and places them into the C-string str. All characters will be processed until either num characters have been retrieved or the end of line character is encountered. The end of line character is included in your str if it was encountered. You will need to remove the end of line character manually. Usually like:
// Replace the last character of your string with the end of string character.
str[strlen(str)-1] = '\0';
It will read whole character in file right?
If your file has more than one line you will need some kind of loop to process all the lines contained in the file.
But how I can remove buffer from my input if not using this? Because in my program if I try to stop my buffer with getchar() it works for sometimes like if I just input my data once. If I input it twice getchar() won't work and it will vanishing my 1st char.
If you are trying to insure there are no characters left in your input buffer then you can remove and discard any remaining characters with something like:
while ((c = getchar()) != '\n') ;
The above code will retrieve and discard all characters until the end of line character is encountered.
When switching between input modes, scanf() to fgets() you will usually need to remove the end of line character because scanf() leaves this character in the input buffer which will mess up the read of a character. It is sometimes easier to always retrieve input with fgets() then use sscanf() to process this string extracting your variables out of the retrieved string.