C 从from中的多行输入获取数据时出现问题。/{base}<;输入文件

C 从from中的多行输入获取数据时出现问题。/{base}<;输入文件,c,command-line,multiline,C,Command Line,Multiline,我有一个问题,我试着去研究它,但却找不到我想要的答案 我试图在C语言中以这种方式接收多行文本文件 ./{base_name}

我有一个问题,我试着去研究它,但却找不到我想要的答案

我试图在C语言中以这种方式接收多行文本文件

./{base_name} input.txt包含

54
37
100
123
1
54
我已经做到了这一点

scanf("%[^\t]",s);
但是我想得到每个数字,并可能通过使用atoi()将C字符串解析为整数数组。我很不确定怎么做

现在我的代码是这样的

int main(int argc, char *argv[]){
   char str[1000];
   scanf("%[^\t]",str);
   printf("%s\n", str);
}
是否有任何方法可以使用argc和**argv来实现这一点,或者以另一种简洁的方式获取输入并生成一个int数组

我正在编译:

gcc -w --std=gnu99 -O3 -o {base_name} {file_name}.c -lm
最好的办法是将它作为一个.txt文件并使用fopen等。但是作业要求我将输入作为。/{base_name}
我得到了一个有效的答案。除此之外,我还有一段代码,它也工作得很好。但是,我们建议使用可接受的答案,因为它定义了输入中数组的大小。非常感谢你的帮助

#include <stdio.h>
int main(){
    char s[100];
    int array[100];
    while(scanf("%[^\t]",s)==1){
        printf("%s",s);
    }
    return 0;
}
#包括
int main(){
chars[100];
整数数组[100];
而(scanf(“%[^\t]”,s)==1){
printf(“%s”,s);
}
返回0;
}

为什么不直接将输入读取为整数

//test.c
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[]){
   int n;
   int i;
   int *arr;

   /*Considering first value as number of elements */
   /*Else you can declare an array of fixed size */
   fscanf(stdin,"%d",&n);
   arr = malloc(sizeof(int)*n);

   for(i=0;i<n;i++)
    fscanf(stdin,"%d",&arr[i]);


   for(i=0;i<n;i++)
    printf("%d ",arr[i]);

    free(arr);
    return 0;
}
//test.c
#包括
#包括
int main(int argc,char*argv[]){
int n;
int i;
int*arr;
/*将第一个值视为元素数*/
/*否则可以声明一个固定大小的数组*/
fscanf(标准输入、%d、&n);
arr=malloc(sizeof(int)*n);

对于(i=0;i为什么不直接将输入读取为整数

//test.c
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[]){
   int n;
   int i;
   int *arr;

   /*Considering first value as number of elements */
   /*Else you can declare an array of fixed size */
   fscanf(stdin,"%d",&n);
   arr = malloc(sizeof(int)*n);

   for(i=0;i<n;i++)
    fscanf(stdin,"%d",&arr[i]);


   for(i=0;i<n;i++)
    printf("%d ",arr[i]);

    free(arr);
    return 0;
}
//test.c
#包括
#包括
int main(int argc,char*argv[]){
int n;
int i;
int*arr;
/*将第一个值视为元素数*/
/*否则可以声明一个固定大小的数组*/
fscanf(标准输入、%d、&n);
arr=malloc(sizeof(int)*n);

对于(i=0;i您可以尝试以下代码:

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

int main(int argc, char *argv) {
   FILE *file;
   int numbers[100] /* array with the numbers */, i /8 index */, j;

   if (!(file = fopen(argv[1], "r")) return -1; /* open the file name the user inputs as argument for read */

   i = 0; /* first place in the array */
   do /* read the whole file */ {
      fscanf(" %d", &(numbers[i]); /* the space in the format string discards preceding whitespace characters */
      i++; /* next position */
   }

   for (j = 0; j < i; j++) printf("%d", numbers[j]); /* print the array for the user */

   return 0;
}
#包括
#包括
int main(int argc,char*argv){
文件*文件;
整数[100]/*数组,带有数字*/,i/8索引*/,j;
如果(!(file=fopen(argv[1],“r”))返回-1;/*打开用户输入的文件名作为参数进行读取*/
i=0;/*数组中的第一位*/
执行/*读取整个文件*/{
fscanf(“%d”,&(数字[i]);/*格式字符串中的空格将丢弃前面的空白字符*/
i++;/*下一个位置*/
}
对于(j=0;j
您可以尝试以下代码:

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

int main(int argc, char *argv) {
   FILE *file;
   int numbers[100] /* array with the numbers */, i /8 index */, j;

   if (!(file = fopen(argv[1], "r")) return -1; /* open the file name the user inputs as argument for read */

   i = 0; /* first place in the array */
   do /* read the whole file */ {
      fscanf(" %d", &(numbers[i]); /* the space in the format string discards preceding whitespace characters */
      i++; /* next position */
   }

   for (j = 0; j < i; j++) printf("%d", numbers[j]); /* print the array for the user */

   return 0;
}
#包括
#包括
int main(int argc,char*argv){
文件*文件;
整数[100]/*数组,带有数字*/,i/8索引*/,j;
如果(!(file=fopen(argv[1],“r”))返回-1;/*打开用户输入的文件名作为参数进行读取*/
i=0;/*数组中的第一位*/
执行/*读取整个文件*/{
fscanf(“%d”,&(数字[i]);/*格式字符串中的空格将丢弃前面的空白字符*/
i++;/*下一个位置*/
}
对于(j=0;j
您的需求要求您从控制台(stdin)读取输入。您不应该使用fopen。代码看起来不错。您的需求要求您从控制台(stdin)读取输入。你不应该使用fopen。代码看起来不错。非常感谢你的帮助。不知道发生了什么突然它开始喷出。37 100 123 1 54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0。据我所知,它似乎是54,这是第一个数字,而不是6。我似乎再也无法打印出第一个元素了。fscanf(标准输入,“%d”,&n);只给54,这是第一个输入谢谢你的帮助不知道发生了什么突然它开始喷出。37100 123 1540 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0ar正如我所看到的,它似乎是54,这是第一个数字,而不是6I。我似乎再也不能打印出第一个元素了。fscanf(stdin,“%d”,&n);只给出54,这是第一个输入