c中stdin的多行字符串输入
multiline string input from stdin in c
我在我的 C 程序中尝试使用此代码换行的多行输入形式标准输入
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#define DEFAULT_INPUT_LENGTH 20
char * readMessage(FILE* file);
void writeChar(char* string, char c);
int main()
{
printf("Message:\n");
char * msg = readMessage(stdin);
printf("Input: %s\n", msg);
free(msg);
return 0;
};
char * readMessage(FILE* file)
{
char *input = malloc(DEFAULT_INPUT_LENGTH);
int inputCounter = 0;
int n = 1;
char c;
while(!feof(stdin))
{
c=fgetc(file);
inputCounter++;
if (inputCounter == DEFAULT_INPUT_LENGTH * n)
{
n++;
int chars = DEFAULT_INPUT_LENGTH * n;
input = realloc(input, chars);
}
writeChar(input, c);
}
return input;
}
void writeChar(char* in, char c)
{
int i;
for(i = 0; ; i++)
{
if (in[i] == '[=10=]')
{
in[i] = c;
break;
}
}
}
但是当我在 Linux 上尝试 CTRL+D 或在 Windows 上尝试 CTRL+Z 时,输入没有结束。
输入示例如下:
asfer
dfdfd
sffdfl
在此示例中,如果我在最后一个 l 字符后尝试 CTRl+D 输入未结束,我必须使用 enter 和 CTRL+D
在最后一行换行后提示 CTRL+Z 和 ENTER 和
试试这个
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_INPUT_LENGTH 20
char * readMessage(FILE* file);
void writeChar(char* string, char c);
int main(void){
printf("Message:\n");
char * msg = readMessage(stdin);
printf("Input: %s\n", msg);
free(msg);
return 0;
}
char * readMessage(FILE* file){
char *input = malloc(DEFAULT_INPUT_LENGTH);
int inputCounter = 0;
int n = 1;
int c;//!
*input = 0;//!
while((c=fgetc(file)) != EOF){//!
inputCounter++;
if (inputCounter == DEFAULT_INPUT_LENGTH * n){
n++;
int chars = DEFAULT_INPUT_LENGTH * n;
input = realloc(input, chars);
}
writeChar(input, c);
}
return input;
}
void writeChar(char* in, char c){
int i;
for(i = 0; ; i++){
if (in[i] == '[=10=]'){
in[i] = c;
in[i+1] = 0;//!
break;
}
}
}
我在我的 C 程序中尝试使用此代码换行的多行输入形式标准输入
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#define DEFAULT_INPUT_LENGTH 20
char * readMessage(FILE* file);
void writeChar(char* string, char c);
int main()
{
printf("Message:\n");
char * msg = readMessage(stdin);
printf("Input: %s\n", msg);
free(msg);
return 0;
};
char * readMessage(FILE* file)
{
char *input = malloc(DEFAULT_INPUT_LENGTH);
int inputCounter = 0;
int n = 1;
char c;
while(!feof(stdin))
{
c=fgetc(file);
inputCounter++;
if (inputCounter == DEFAULT_INPUT_LENGTH * n)
{
n++;
int chars = DEFAULT_INPUT_LENGTH * n;
input = realloc(input, chars);
}
writeChar(input, c);
}
return input;
}
void writeChar(char* in, char c)
{
int i;
for(i = 0; ; i++)
{
if (in[i] == '[=10=]')
{
in[i] = c;
break;
}
}
}
但是当我在 Linux 上尝试 CTRL+D 或在 Windows 上尝试 CTRL+Z 时,输入没有结束。
输入示例如下:
asfer
dfdfd
sffdfl
在此示例中,如果我在最后一个 l 字符后尝试 CTRl+D 输入未结束,我必须使用 enter 和 CTRL+D
在最后一行换行后提示 CTRL+Z 和 ENTER 和
试试这个
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_INPUT_LENGTH 20
char * readMessage(FILE* file);
void writeChar(char* string, char c);
int main(void){
printf("Message:\n");
char * msg = readMessage(stdin);
printf("Input: %s\n", msg);
free(msg);
return 0;
}
char * readMessage(FILE* file){
char *input = malloc(DEFAULT_INPUT_LENGTH);
int inputCounter = 0;
int n = 1;
int c;//!
*input = 0;//!
while((c=fgetc(file)) != EOF){//!
inputCounter++;
if (inputCounter == DEFAULT_INPUT_LENGTH * n){
n++;
int chars = DEFAULT_INPUT_LENGTH * n;
input = realloc(input, chars);
}
writeChar(input, c);
}
return input;
}
void writeChar(char* in, char c){
int i;
for(i = 0; ; i++){
if (in[i] == '[=10=]'){
in[i] = c;
in[i+1] = 0;//!
break;
}
}
}