小编Dan*_*dam的帖子

我只能在新的文本文件中存储有限数量的行

我有很多不同的伪随机数生成器,用C编写,生成任意数量的随机数对(通过CLI),并将它们存储在(新)文本文件中:每列一对数字.我想将400.000.000数字存储在文本文件中,但是当我查看文件的行数时,它只有82.595.525行.这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "../Calculos/myfunctions.c"

void outputDevRandomOpenFile (FILE * from_file, FILE * to_file, unsigned long long how_many_pairs){

    unsigned long long i = 0LL;
    int seed;

    unsigned long long max_period = 2147483648LL;

    for (i = 0LL; i < how_many_pairs; i += 1LL){

        fread (&seed, sizeof(int), 1, from_file);
        fprintf (to_file, "%.10lf ", fabs (((double) seed) / ((double) max_period)));

        fread (&seed, sizeof(int), 1, from_file);
        fprintf (to_file, "%.10lf\n", fabs (((double) seed) / ((double) max_period)));
    }
}


int main …
Run Code Online (Sandbox Code Playgroud)

c shell storage file lines

5
推荐指数
1
解决办法
201
查看次数

将字符串传递给C中的函数

我有以下代码:

#include <stdio.h>

char * lookLine (FILE *fichero) 
{
    char p[25];
    fgets (p, sizeof (p), fichero);
    return p;
}

int main (void) {
    printf ("%s\n", lookLine (fopen ("suma.c", "r")));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

#??x?
Run Code Online (Sandbox Code Playgroud)

不太好.我打算打印出名为"suma.c"的文件的第一行.它应该打印出以下内容:

#include <stdio.h>
Run Code Online (Sandbox Code Playgroud)

不过,如果我将p字符串的内容打印到同一个lookFile函数中,它就可以了:

#include <stdio.h>

void lookLine (FILE * fichero) 
{
    char p[25];
    fgets (p, sizeof (p), fichero);
    printf ("%s\n", p);
}

int main (void) {
    lookLine (fopen ("suma.c", "r"));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我现在得到的输出是正确的:

#include <stdio.h>
Run Code Online (Sandbox Code Playgroud)

我的理由是:通过使用fgets我保存p数组中第一行"name.c"的字符串,然后返回其地址,该地址由 …

c arrays string fgets memory-address

0
推荐指数
1
解决办法
225
查看次数

标签 统计

c ×2

arrays ×1

fgets ×1

file ×1

lines ×1

memory-address ×1

shell ×1

storage ×1

string ×1