Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何获取文件中的元素数?_C_Command Line_Args - Fatal编程技术网

C 如何获取文件中的元素数?

C 如何获取文件中的元素数?,c,command-line,args,C,Command Line,Args,现在,我有 int main(int argc, char *argv[]) { if (argc != 3) { printf("Invalid number of command line parameters, exiting...\n"); exit(1); } int *numReadings; load_readings(argv[1], numReadings); return 0; } int

现在,我有

int main(int argc, char *argv[]) {
    if (argc != 3) {
        printf("Invalid number of command line parameters, exiting...\n");
        exit(1);
    }

    int *numReadings;
    load_readings(argv[1], numReadings);

    return 0;
}

    int *load_readings(char fileName[], int *numReadings) {
        FILE *in = NULL;
        in = fopen(fileName, "r");

        if (in == NULL) {
            printf("Unable to open a file named \"%s\", exiting...\n", fileName);
            fclose(in);
            exit(4);
        }

        printf("%s\n", fileName);
        int size = atoi(fileName);
        printf("Size is %d\n", size);
        int *data = (int *) calloc(size, sizeof(int));

        int i;
        for (i = 0; i < size; i++)
            fscanf(in, "%d", (data + i));
        }
    }
intmain(intargc,char*argv[]){
如果(argc!=3){
printf(“命令行参数数量无效,正在退出…\n”);
出口(1);
}
整数*numReadings;
加载_读数(argv[1],numreads);
返回0;
}
int*load_读数(字符文件名[],int*numReadings){
文件*in=NULL;
in=fopen(文件名,“r”);
if(in==NULL){
printf(“无法打开名为\%s\”的文件,正在退出…\n”,文件名);
fclose(in);
出口(4);
}
printf(“%s\n”,文件名);
int size=atoi(文件名);
printf(“大小为%d\n”,大小);
int*data=(int*)calloc(size,sizeof(int));
int i;
对于(i=0;i
当我执行size=atoi(fileName)时,它返回0。在包括这个网站在内的多个网站上,我看到人们使用“atoi(argv[1]),但我的网站经常返回0。我的sample.txt文件有一组由空格分隔的3位数字。我的印象是,一旦我得到正确的大小,它下面的所有东西都会工作。

只有当
string\u值
可以转换为整数时,
int size=atoi(“1”)
size
的值将为
1
。但是如果
int size=atoi(“asd”)
,该值将为
0
,因为
asd
无法转换为整数

如果要获取文件大小,可以使用
struct stat

只有当
string\u值
可以转换为整数时,
atoi(string\u值)
才会返回数字,例如
int size=atoi(“1”)
size
的值将为
1
。但是如果
int size=atoi(“asd”)
,该值将为
0
,因为
asd
无法转换为整数


如果要获取文件大小,可以使用
struct stat

atoi
不告诉
size
,它只是将
string
转换为
integer

以了解您需要查找的文件的
size
,然后询问位置:

    fseek(fp, 0L, SEEK_END); // seek to end of file
    size = ftell(fp);    //get current file pointer
   fseek(f, 0, SEEK_SET); // seek back to beginning of file

atoi
不会告诉
size
,它只是将
string
转换为
integer

以了解您需要查找到文件末尾的文件的
size
,然后询问位置:

    fseek(fp, 0L, SEEK_END); // seek to end of file
    size = ftell(fp);    //get current file pointer
   fseek(f, 0, SEEK_SET); // seek back to beginning of file
atoi()
将字符串转换为整数,似乎您想知道文件大小,或者具体地说,文件需要分配多少个内存,如果数据看起来完全像
123…
3位数字,用空格分隔,然后使用
stat()
返回文件大小(以字节为单位),可以除以4:

struct stat st;
/* find the file size in bytes */
/* should check for errors */
fstat(fileName, &st);

/* divide the number of bytes by 4 to get the number of numbers */
int size = st.st_size / 4; 

/* just in case the last number doesn't have a space */
size += (st.st_size % 4) != 0; 

/* allocate memory  */
int *data = calloc(size, sizeof(int));
注意:我真的不喜欢这个解决方案,你应该分配一个初始缓冲区,比如说100个整数,如果你需要更多,你应该使用
realloc()
atoi()
将一个字符串转换成整数,看起来你想知道文件的大小,或者具体地说,文件需要分配多少内存,如果数据看起来与用空格分隔的3位数字完全相同,则使用返回文件大小(字节)的
stat()
,您只需除以4即可:

struct stat st;
/* find the file size in bytes */
/* should check for errors */
fstat(fileName, &st);

/* divide the number of bytes by 4 to get the number of numbers */
int size = st.st_size / 4; 

/* just in case the last number doesn't have a space */
size += (st.st_size % 4) != 0; 

/* allocate memory  */
int *data = calloc(size, sizeof(int));

注意:我真的不喜欢这个解决方案,你应该分配一个初始缓冲区,比如说100个整数,如果你需要更多,你应该使用
realloc()

我想你对
atoi
的功能感到困惑-它只是将一个表示为字符串的整数转换为一个实际的整数。因此,除非您的文件名类似于“12345”,否则不会有多大作用。请参阅函数。获取“文件中元素数”的另一种方法是:
wc filename
。祝你好运。我想你对
atoi
的功能感到困惑-它只是将一个表示为字符串的整数转换为一个实际的整数。因此,除非您的文件名类似于“12345”,否则不会有多大作用。请参阅函数。获取“文件中元素数”的另一种方法是:
wc filename
。祝你好运。我不相信我想要“stat”,因为我想要文件中的元素数。(例如,我的文件有48个三位数),但我并不总是知道它是48。@user1799147如果你有文件的总大小,以及文件中每个元素的大小,就很容易计算元素数。我明白了,您可以尝试使用
fseek
ftell
函数。我认为我不需要“stat”,因为我需要文件中的元素数。(例如,我的文件有48个三位数),但我并不总是知道它是48。@user1799147如果您有文件的总大小以及文件中每个元素的大小,计算元素的数量很容易。我明白了,你可以尝试使用
fseek
ftell
函数。我最后使用了这个函数,然后将ftell(fp)除以4。ThanksI最终使用这个函数,然后将ftell(fp)除以4。谢谢