使用(输入)从用户打印到屏幕

print from user to screen with (enter)

我正在托盘中从用户打印字符串到屏幕我遇到的问题是当用户插入(输入)键时程序转到新行并且用户仍然可以在屏幕上打印,但是当我插入(输入)程序转到第一行并覆盖旧词这里是我的代码

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int len;
char searsh_word[10];
char ch;
    printf("enter your strings\n");
while(ch!='EOF'){
    ch=getch();
    printf("%c",ch);
   }
puts("\nEnter the word you need to search for : ");
scanf("%s",searsh_word);
len=strlen(searsh_word);
printf("your word length is : %d",len);


return 0;
}

问题在于使用 getch() 您可以使用 getchar() 以获得更好的结果

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
FILE *file;
int main()
{
int len;
char searsh_word[10];
char ch;
file=fopen("test.txt","w");
if(!file){
        printf("Error opening file\n");
        exit(1);
}else{
    printf("enter your strings\n");
    }
    do{
    ch=getchar();
    fprintf(file,"%c",ch);
   }while(ch!='.');
   fclose(file);
puts("\nEnter the word you need to search for : ");
scanf("%s",searsh_word);
len=strlen(searsh_word);
printf("your word length is : %d",len);


return 0;
}