Quote Originally Posted by Prelude View Post
I'd be willing to bet that if you run in debug mode, the failure will be when i reaches 1000. You're indexing the array before checking the validity of the index. A loop along these lines would work better:
Code:
while ((ch = _getch()) != 'y' && i < lim - 1)
{
    str[i++] = (char)ch;
}

str[i] = '\0';
This allows sufficient room for the typed characters, plus the terminating '\0', and excludes your stop character from the string. Personally, I'd favor using EOF instead of a potentially valid character to stop input, because at present you cannot write 'y' to your file in any meaningful way.

This snippet isn't very robust, but I didn't want to complicate things for you just yet.
The code I want checked is this (see first post)
Code:
int ch;
...
 while (ch != 'y'){
    ch = _getch();
    fprintf(fp, ch);
}
replace the str array to textfile