这几天一直被一个bug折磨,最终发现是stdin缓存了‘\n’,因此fgets()函数无法正常执行,解决的方法是在调用fgets()之前将缓存清空。VC中可以使用fflush(stdin)的方法,但在gcc中,fflush(stdin)的作用并不是清空stdin缓存,因此需要我们手动来做,代码如下:
代码说明:
这段代码的目的是读入矩阵的行、列数,然后让用户一次性输入整个矩阵。问题出在get_row_col()函数中的scanf上,解决方式是在fgets中加入清除缓存的循环。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFFSPACE 4 #define BUFFSIZE 4096 void get_row_col(int*, int*); void print_matrix(int*, int , int); int check_input(int*, int); int main(int ac, char*av[]){ int count, row_num, col_num; int *matrix; char *buff, *char_input; get_row_col(&row_num, &col_num); matrix = (int*)malloc(sizeof(int)*row_num*col_num); buff = (char*)malloc(sizeof(char)*row_num*col_num*BUFFSPACE); printf("please input your matrix\n"); char c; //清除stdin中的缓存 while ( (c = getchar()) != '\n' && c != EOF ) ; if( fgets(buff, BUFFSIZE, stdin) == NULL ) printf("An error occurs when reading user input\n"); int num_input = 0; count = 0; char_input = strtok(buff, " "); while( char_input != NULL ){ num_input = atoi(char_input); matrix[count] = num_input; if( check_input(matrix, num_input) == -1) ;//to be done count++; char_input = strtok(NULL, " "); } if( count != (row_num*col_num) ){ printf("your input did not conform to your declared matrix, please run this program again\n"); exit(1); }else{ print_matrix(matrix, row_num, col_num); } free(matrix); free(buff); return 0; } void get_row_col(int *row_num, int *col_num){ printf("Please input the number of rows\n"); scanf("%d", row_num); printf("Please input the number of cols\n"); scanf("%d", col_num); //问题在这里,用户回车时输入了一个'\n',但scanf并没有将其从缓存中读出,从而使其留在了缓存中 } void print_matrix(int *matrix, int row_num, int col_num){ int i, j; int k = 0; printf("Your %d * %d matrix is: \n", row_num, col_num); for(i=0; i<row_num; i++){ for(j=0; j<col_num;j++){ printf( "%d\t", *(matrix+k) ); k++; } printf("\n"); } } int check_input(int *matrix, int num_input){ return 0; }
您还没有登录,请您登录后再发表评论
解决mtk6572平台modem编译错误! Please install the GCC Cross-Compiler on correct path: tools/GCC/4.6.2/linux/bin/arm-none-eabi-gcc
解决mtk6737平台modem编译错误! Please install the GCC Cross-Compiler on correct path: tools/GCC/4.6.2/linux/bin/arm-none-eabi-gcc
gcc: error trying to exec 'cc1': execvp: No such file or directory
使用arm-linux-gcc 出现一下问题: rm-linux-gcc src/Output.c -o src/Output.o -I./inc -c /usr/local/arm/5.4.0/usr/bin/../libexec/gcc/arm-none-linux-gnueabi/5.4.0/cc1: error while loading shared libraries...
在Linux系统中,尤其是服务器环境,离线安装软件包是非常常见的需求。本教程将详细介绍如何在CentOS 7上离线安装GCC、PCRE-devel、OpenSSL-devel和Zlib-devel这些重要的开发工具包,这些都是构建和编译Nginx web...
Configured with: /scratch/maciej/arm-linux-2014.05-rel/src/gcc-4.8-2014.05/configure --build=i686-pc-linux-gnu --host=i686-mingw32 --target=arm-none-linux-gnueabi ...... Thread model: posix gcc ...
tar xvf /root/gcc-src/mpfr-3.1.4.tar.gz -C /root/gcc-4.8.5/ 或者 tar xvf /root/gcc-src/mpfr-3.1.4.tar.bz2 -C gcc-4.8.5/ mv /root/gcc-4.8.5/cloog-0.18.4 /root/gcc-4.8.5/cloog mv /root/gcc-4.8.5/gmp-...
本文件为arm-linux-gcc-4.3.2.tgz的part2 arm-linux-gcc-4.3.2.tgz.ab,需要与arm-linux-gcc-4.3.2.tgz的part1 arm-linux-gcc-4.3.2.tgz.aa 合并成一个文件arm-linux-gcc-4.3.2.tgz 安装方法: 0. cat arm-linux-gcc-...
Configured with: /opt/crosstool/crosstool-0.28/build/arm-linux/gcc-3.4.1-glibc-2.3.2/gcc-3.4.1/configure --target=arm-linux --host=i686-host_pc-linux-gnu --prefix=/opt/crosstool/arm-linux/gcc-3.4.1-...
It includes the GNU Compiler (GCC) and is available free of charge directly from Arm for embedded software development on Windows, Linux and Mac OS X operating systems. Follow the links on this page ...
标题中的"gcc-arm-none-eabi-9-2019-q4-major-win32.zip"是一个针对Windows操作系统的GCC交叉编译工具链压缩包。这个工具链是专门为ARM架构的微控制器和嵌入式系统设计的,使得开发者可以在运行Windows的个人电脑上...
tar -xvf gcc-rpms.tar.gz ``` ## rpm安装gcc ```bash rpm -Uvh *.rpm --nodeps --force ``` ### 验证 ```bash gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-...
sudo ln -s /usr/local/gcc-2.95/lib/libstdc++-3-libc6.3-2-2.10.0.so /usr/lib/libstdc++-3-libc6.3-2-2.10.0.so sudo ln -s /usr/lib/222libstdc++-3-libc6.3-2-2.10.0.so /usr/lib/libstdc++-libc6.3-2.so.3 ...
gcc-4.8.5-36.el7.x86_64.rpm glibc-devel-2.17-260.el7.x86_64 openssl-1.0.2k-16.el7.x86_64 openssl-devel-1.0.2k-16.el7.x86_64 openssl-libs-1.0.2k-16.el7.x86_64 pcre-devel-8.32-17.el7.x86_64 zlib-1.2.7-...
./configure --prefix=/usr/local/gcc-7.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.1.2 --with-mpfr=/usr/local/mpfr-3.1.6 --...
编译uboot时出现/bin/sh: 1: /opt/buildroot-gcc342/bin/mipsel-linux-as: not found 是因为系统是64位的,按照以下文档安装几个包即可解决
linux平台gcc编译器,gcc-arm-none-eabi-9-2020-q2-update-x64-linux.tar.bz2,gcc-arm-none-eabi-9-2020-q2-update-x64-linux.tar.bz2,只要5分,只要5分
最新版本的ARM交叉编译器。下载地址: https://releases.linaro.org/components/toolchain/binaries/latest-7/arm-linux-gnueabi/
相关推荐
解决mtk6572平台modem编译错误! Please install the GCC Cross-Compiler on correct path: tools/GCC/4.6.2/linux/bin/arm-none-eabi-gcc
解决mtk6737平台modem编译错误! Please install the GCC Cross-Compiler on correct path: tools/GCC/4.6.2/linux/bin/arm-none-eabi-gcc
gcc: error trying to exec 'cc1': execvp: No such file or directory
使用arm-linux-gcc 出现一下问题: rm-linux-gcc src/Output.c -o src/Output.o -I./inc -c /usr/local/arm/5.4.0/usr/bin/../libexec/gcc/arm-none-linux-gnueabi/5.4.0/cc1: error while loading shared libraries...
在Linux系统中,尤其是服务器环境,离线安装软件包是非常常见的需求。本教程将详细介绍如何在CentOS 7上离线安装GCC、PCRE-devel、OpenSSL-devel和Zlib-devel这些重要的开发工具包,这些都是构建和编译Nginx web...
Configured with: /scratch/maciej/arm-linux-2014.05-rel/src/gcc-4.8-2014.05/configure --build=i686-pc-linux-gnu --host=i686-mingw32 --target=arm-none-linux-gnueabi ...... Thread model: posix gcc ...
tar xvf /root/gcc-src/mpfr-3.1.4.tar.gz -C /root/gcc-4.8.5/ 或者 tar xvf /root/gcc-src/mpfr-3.1.4.tar.bz2 -C gcc-4.8.5/ mv /root/gcc-4.8.5/cloog-0.18.4 /root/gcc-4.8.5/cloog mv /root/gcc-4.8.5/gmp-...
本文件为arm-linux-gcc-4.3.2.tgz的part2 arm-linux-gcc-4.3.2.tgz.ab,需要与arm-linux-gcc-4.3.2.tgz的part1 arm-linux-gcc-4.3.2.tgz.aa 合并成一个文件arm-linux-gcc-4.3.2.tgz 安装方法: 0. cat arm-linux-gcc-...
Configured with: /opt/crosstool/crosstool-0.28/build/arm-linux/gcc-3.4.1-glibc-2.3.2/gcc-3.4.1/configure --target=arm-linux --host=i686-host_pc-linux-gnu --prefix=/opt/crosstool/arm-linux/gcc-3.4.1-...
It includes the GNU Compiler (GCC) and is available free of charge directly from Arm for embedded software development on Windows, Linux and Mac OS X operating systems. Follow the links on this page ...
标题中的"gcc-arm-none-eabi-9-2019-q4-major-win32.zip"是一个针对Windows操作系统的GCC交叉编译工具链压缩包。这个工具链是专门为ARM架构的微控制器和嵌入式系统设计的,使得开发者可以在运行Windows的个人电脑上...
Configured with: /opt/crosstool/crosstool-0.28/build/arm-linux/gcc-3.4.1-glibc-2.3.2/gcc-3.4.1/configure --target=arm-linux --host=i686-host_pc-linux-gnu --prefix=/opt/crosstool/arm-linux/gcc-3.4.1-...
tar -xvf gcc-rpms.tar.gz ``` ## rpm安装gcc ```bash rpm -Uvh *.rpm --nodeps --force ``` ### 验证 ```bash gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-...
sudo ln -s /usr/local/gcc-2.95/lib/libstdc++-3-libc6.3-2-2.10.0.so /usr/lib/libstdc++-3-libc6.3-2-2.10.0.so sudo ln -s /usr/lib/222libstdc++-3-libc6.3-2-2.10.0.so /usr/lib/libstdc++-libc6.3-2.so.3 ...
gcc-4.8.5-36.el7.x86_64.rpm glibc-devel-2.17-260.el7.x86_64 openssl-1.0.2k-16.el7.x86_64 openssl-devel-1.0.2k-16.el7.x86_64 openssl-libs-1.0.2k-16.el7.x86_64 pcre-devel-8.32-17.el7.x86_64 zlib-1.2.7-...
./configure --prefix=/usr/local/gcc-7.2.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-6.1.2 --with-mpfr=/usr/local/mpfr-3.1.6 --...
Configured with: /opt/crosstool/crosstool-0.28/build/arm-linux/gcc-3.4.1-glibc-2.3.2/gcc-3.4.1/configure --target=arm-linux --host=i686-host_pc-linux-gnu --prefix=/opt/crosstool/arm-linux/gcc-3.4.1-...
编译uboot时出现/bin/sh: 1: /opt/buildroot-gcc342/bin/mipsel-linux-as: not found 是因为系统是64位的,按照以下文档安装几个包即可解决
linux平台gcc编译器,gcc-arm-none-eabi-9-2020-q2-update-x64-linux.tar.bz2,gcc-arm-none-eabi-9-2020-q2-update-x64-linux.tar.bz2,只要5分,只要5分
最新版本的ARM交叉编译器。下载地址: https://releases.linaro.org/components/toolchain/binaries/latest-7/arm-linux-gnueabi/