The magic is in the static , which retains the contents of the string memory even after the function completes. (You can consider it as an extension of the scope of a variable.)
This code takes each character every time, then concatenates them into a string and saves it to a file:
#include <stdio.h> #include <conio.h> char* strbsmallah () { static char input[50]; char position =0, letter; scanf("%c",&letter); while (letter != '~') { // press '~' to end your text input [position]=letter; ++position; scanf("%c",&letter); } input[position]='\0'; char *y; y = (char*) &input; //printf("%s\n ",y); return y; } int main() { printf("\n"); FILE *fp; fp = fopen("bsmallah.txt", "w+"); fprintf(fp, strbsmallah()); while (!_kbhit()); return 0; }
Mohamed el shenawy
source share