开发者

C - fscanf() - Reading Data Into an Array

开发者 https://www.devze.com 2023-03-21 10:56 出处:网络
I\'m getting a strange error when reading my data into an array. My goal is to read a file that has a single column of numbers into an array, line-by-line.

I'm getting a strange error when reading my data into an array. My goal is to read a file that has a single column of numbers into an array, line-by-line.

#include <stdio.h>

int main() {
    int numArray = [20];
    int i = 0;

    FILE *infile;
    infile = fopen("numbers", "r");

    while开发者_高级运维(!feof(infile))
    {
        fscanf(infile,"%d",&numArray[i]);
        i++;
    }

    fclose(infile);
    return 0; }

This is my compile error:

sort_algorithms.c: In function ‘main’: sort_algorithms.c:6: error: expected expression before ‘[’ token sort_algorithms.c:16: error: subscripted value is neither array nor pointer


the correct way to declare an array in c is like this:

int numArray[20];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号