Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 为什么不是';我的代码是否生成了1到N之间的随机数字序列?没有生成任何文件。我的代码中有什么错误?我做错了什么?_C_File_Random - Fatal编程技术网

C 为什么不是';我的代码是否生成了1到N之间的随机数字序列?没有生成任何文件。我的代码中有什么错误?我做错了什么?

C 为什么不是';我的代码是否生成了1到N之间的随机数字序列?没有生成任何文件。我的代码中有什么错误?我做错了什么?,c,file,random,C,File,Random,该程序的目标是生成1到N之间的随机数字序列,其中N作为参数传递给程序,并将结果序列写入文件 我的文件没有生成。我做错了什么?我的代码中有错误吗?我的代码有问题吗?我输出的文件正确吗 /*01*/ // /*02*/ // random_sequence_v6.c /*03*/ // Generate a random sequence of all numbers between 1 to N /*04*/ // /*05*/ #include "stdio.h" /*06*

该程序的目标是生成1到N之间的随机数字序列,其中N作为参数传递给程序,并将结果序列写入文件

我的文件没有生成。我做错了什么?我的代码中有错误吗?我的代码有问题吗?我输出的文件正确吗

/*01*/ //
/*02*/ // random_sequence_v6.c
/*03*/ // Generate a random sequence of all numbers between 1 to N
/*04*/ //
/*05*/ #include "stdio.h"
/*06*/ #include "stdint.h"
/*07*/ #include "stdlib.h"
/*08*/ #include "stdint.h"
/*09*/ #include "sys/types.h"
/*10*/ #include "sys/stat.h"
/*11*/ #include "fcntl.h"
/*12*/ #include "assert.h"
/*13*/ #include "inttypes.h"
/*14*/
/*15*/ typedef uint64_t value_t;
/*16*/
/*17*/ value_t* generate_sequence(int num_values)
/*18*/ {
/*19*/     assert(num_values > 0);
/*20*/     value_t* data = calloc(num_values, sizeof(int));
/*21*/     for (int i = 0; i <= num_values; i++) {
/*22*/        data[i] = i;
/*23*/     }
/*24*/     return data;
/*25*/ }
/*26*/
/*27*/ int random_value(int min, int max)
/*28*/ {
/*29*/     int random_number;
/*30*/     do {
/*31*/         random_number = rand();
/*32*/     } while ((random_number <= min) || (random_number >= max));
           return random_number;
/*33*/ }
/*34*/
/*35*/ void randomize_sequence(value_t* sequence, int num_values)
/*36*/ {
/*37*/     // Fisher-Yates
/*38*/     for(int i = 0; i < num_values-2; i++) {
/*39*/         int random_index = random_value(i, num_values-1);
/*40*/         // Swap them
               int temp = sequence[i];
/*41*/         sequence[i] = sequence[random_index];
/*42*/         sequence[random_index] = temp;
/*43*/     }
/*44*/ }
/*45*/
/*46*/ int main(int argc, char* argv[])
/*47*/ {
/*48*/     int num_values = strtoul(argv[1], NULL, 10);
/*49*/     value_t* pValues = generate_sequence(num_values);
/*50*/
/*51*/     randomize_sequence(pValues, num_values);
/*52*/
/*53*/     // Record results
/*54*/     FILE *fd = fopen("results.txt", "w+");
/*55*/     for (int i = 0; i < num_values; i++) {
/*56*/         fprintf("%i = %"PRIu64"\n", i, pValues[i]);
/*57*/     }
/*58*/     fclose(fd);
/*59*/
/*60*/     return EXIT_SUCCESS;
/*71*/ }

/*01*///
/*02*///random\u sequence\u v6.c
/*03*///生成1到N之间所有数字的随机序列
/*04*/ //
/*05*/#包括“stdio.h”
/*06*/#包括“标准力h”
/*07*/#包括“stdlib.h”
/*08*/#包括“标准力h”
/*09*/#包括“sys/types.h”
/*10*/#包括“sys/stat.h”
/*11*/#包括“fcntl.h”
/*12*/#包括“assert.h”
/*13*/#包括“inttypes.h”
/*14*/
/*15*/typedef uint64\u t值;
/*16*/
/*17*/value\u t*生成序列(int num\u值)
/*18*/ {
/*19*/断言(数值>0);
/*20*/value_t*data=calloc(num_值,sizeof(int));

/*21*/for(int i=0;i发布的代码包含许多严重问题。具体而言:

gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled2.c" -o "untitled2.o" 

untitled2.c: In function ‘generate_sequence’:
untitled2.c:20:35: warning: conversion to ‘size_t {aka long unsigned int}’ from ‘int’ may change the sign of the result [-Wsign-conversion]
 /*20*/     value_t* data = calloc(num_values, sizeof(int));
                                   ^~~~~~~~~~

untitled2.c:22:25: warning: conversion to ‘value_t {aka long unsigned int}’ from ‘int’ may change the sign of the result [-Wsign-conversion]
 /*22*/        data[i] = i;
                         ^

untitled2.c: In function ‘randomize_sequence’:
untitled2.c:42:27: warning: conversion to ‘int’ from ‘value_t {aka long unsigned int}’ may alter its value [-Wconversion]
                int temp = sequence[i];
                           ^~~~~~~~

untitled2.c:44:41: warning: conversion to ‘value_t {aka long unsigned int}’ from ‘int’ may change the sign of the result [-Wsign-conversion]
 /*42*/         sequence[random_index] = temp;
                                         ^~~~

untitled2.c: In function ‘main’:
untitled2.c:50:29: warning: conversion to ‘int’ from ‘long unsigned int’ may alter its value [-Wconversion]
 /*48*/     int num_values = strtoul(argv[1], NULL, 10);
                             ^~~~~~~

untitled2.c:58:24: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
 /*56*/         fprintf("%i = %"PRIu64"\n", i, pValues[i]);
                        ^~~~~~~~

In file included from untitled2.c:5:0:
/usr/include/stdio.h:312:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘char *’
 extern int fprintf (FILE *__restrict __stream,
            ^~~~~~~

untitled2.c:58:44: warning: passing argument 2 of ‘fprintf’ makes pointer from integer without a cast [-Wint-conversion]
 /*56*/         fprintf("%i = %"PRIu64"\n", i, pValues[i]);
                                            ^

In file included from untitled2.c:5:0:
/usr/include/stdio.h:312:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
 extern int fprintf (FILE *__restrict __stream,
            ^~~~~~~

untitled2.c:48:21: warning: unused parameter ‘argc’ [-Wunused-parameter]
 /*46*/ int main(int argc, char* argv[])
                     ^~~~

Compilation finished successfully.
请注意最后一条消息:
编译成功完成。
这只意味着编译器对每个问题应用了一些“变通方法”,但并不意味着“变通方法”是正确的

请更正您的代码,使其能够干净地编译,然后在您的问题上发布
EDIT

一些提示:

  • 在未首先检查
    argc
    以确保用户实际输入了预期的命令行参数的情况下,切勿访问超出
    argv[0]
  • fprintf()
    的语法是
    intfprintf(文件*流,常量字符*格式,…);
  • strtoul()
    的语法是
    unsigned long int strtoul(const char*nptr,char**endptr,int base);
  • 此外,关于:

    FILE *fd = fopen("results.txt", "w+");
    
    始终检查(!=NULL)返回值。如果失败(即==NULL),则调用

    perror( "fopen failed" );
    

    因此,系统认为错误发生在(
    stderr

    for(int i=0;i错误分配)的错误消息和文本原因。
    value\t*data=calloc(num\u values,sizeof(int));
    -->value\t*data=calloc(num\u values,sizeof*data);
    fprintf(%i=%PRIu64“\n”,i,pValues[i])
    -->
    fprintf(fd,%i=%“PRIu64”\n,i,pValues[i]);
    您还可以将整个
    do while
    循环替换为
    random\u number=(rand()%(max-min+1))+min;
    您每次都可以保证在您的范围内有一个值。这会使它变得更快。在互联网站上找到它