简体   繁体   English

如何制作扫描用户输入(文本)并将其保存在动态字符串上的C程序

[英]How can i make C program that scans user's input(text) and save it on a dynamic string

I want to read input from user(text) using a C program and here is my code: 我想使用C程序读取用户(文本)的输入,这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    int i=0,x=0;
    char *c;
    c[i]=(char*)malloc(sizeof(char));
    while(1){
        c[i]=getc(stdin);
        if(c[i]=='\n')
            break;
        i++;
        realloc(c, i+1 );
    }
    c[i]='\0';
    //printf("\n%d",strlen(c));
    printf("\n\n%s",c);
return 0;
}

This program when it compiles there is 1 warning at c[i]=(char*)malloc(sizeof(char)); 编译时该程序在c[i]=(char*)malloc(sizeof(char));有1个警告c[i]=(char*)malloc(sizeof(char)); :

warning: assignment makes integer from pointer without a cast [enabled by default] 警告:赋值在没有强制转换的情况下从指针生成整数[默认启用]

This program works succesfully but if i remove x=0 from the code there is: 该程序成功运行,但如果我从代码中删除x=0 ,则:

Segmentation fault (core dumped) 分段故障(核心转储)

What should i change on this code so it can work without warnings or a useless random variable like x=0 to work. 我应该对此代码进行更改,以便它可以在没有警告或无效的随机变量(如x=0下工作。

Thank you! 谢谢!

Simply replace this 只需更换它

   c[i]=(char*)malloc(sizeof(char));

with this 有了这个

   c = (char*)malloc(sizeof(char));

and remove cast, you don't need it in C . 并删除强制转换,你不需要在C

   c = malloc(sizeof(char));

Try this 尝试这个

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    int i=0;
    char *c=(char*)malloc(sizeof(char));
    while(1){
        c[i]=getc(stdin);
        if(c[i]=='\n')
            break;
        i++;
    }
    c[i]='\0';
    printf("\n\n%s",c);
    return 0;
}

As said by @Dabo, adjust the assignment. 正如@Dabo所说,调整作业。

c = malloc(sizeof(char));

Following are additional suggestions: 以下是其他建议:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    // Use size_t rather than int for index
    size_t i=0;
    char *c;
    c = malloc(1);
    if (c == NULL) return 1; // Out of memory  
    while(1){
        // To detect EOF condition, use type `int` for get() result
        int ch = getc(stdin);
        if(ch == EOF || ch == '\n') {
            break;
        }
        c[i++] = ch;
        // Important, test & save new pointer 
        char *c2 = realloc(c, i+1 );
        if (c2 == NULL) return 1; // Out of memory  
        c = c2;
    }
    c[i] = '\0';
    // Use %zu when printing size_t variables
    printf("\n%zu",strlen(c));
    printf("\n\n%s",c);
    // Good practice to allocated free memory
    free(c);
   return 0;
}

Edit: fixed 编辑:修复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C中,我编写了一个使用system()调用exe的程序。 exe要求用户输入。 如何让我的程序提供输入? - In C, I wrote a program to call an exe using system(). The exe asks for user input. How can I make my program provide the input? c - 我怎样才能使这个程序的 win 语句递归? - c - How can I make this program's win statements recursive? 如何修改 c 程序,以便在用户输入非浮点输入时,向用户显示“无效输入”? - How can I modify the c program such that if the user enters a non-float input,“invalid input” is shown to the user? 如何在C中使用用户输入来使字符串函数? - How to make string function with user input in C? 我正在尝试制作一个可以根据用户在 C 中的选择运行不同功能的程序 - I'm trying to make a program that can run different functions depending on the user's choice in C 动态Python用户输入到单独的C程序 - Dynamic python user input to a seperate C program 如何使C程序停止任何用户输入的循环? - How to make a C program stop a loop from any user input? 我怎样才能使程序请求用户输入以关闭它或从头开始启动它 - how can i make the program request user input to either close it or start it from the beginning 如何基于用户在C中的输入来停止程序 - How to stop a program based on user's input in C C套接字程序-更新标准输出的读数时如何接受用户输入? - C Socket program - How can I accept user input while updating reading from stdout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM