写大信息时fprintf的问题

时间:2014-08-11 09:40:49

标签: c arrays file fann

我正在尝试编写一个包含18000个项目的数组(该数组来自转换wav>数组),当我在文件中写入项目时,如果我不想写,则显示\n那,这是代码:

f = info.frames;
sr = info.samplerate;
c = info.channels;
num_items = f*c;
int arrayPrueba [num_items];

/* Allocate space for the data to be read, then read it. */
buf = (int *) malloc(num_items*sizeof(int));
num = sf_read_int(sf,buf,num_items);
sf_close(sf);
printf("Read %d items\n",num);

/* Write the data to filedata.out. */
out = fopen("filedata.data","a+");
fprintf(out,"13 18000 1\n");
int cont =0;

for (i = 0; i < num; i += c)
{
   for (j = 0; j < c; ++j)
        fprintf(out,"%d ",buf[i+j]);}
}
fprintf(out,"\n%d\n",pasos);
fclose(out);

以下是filedata.data的一部分,其中出现了跳线

    104333312 103415808 104202240 105250816 104595456 104202240 103022592 103415808 102367232 102236160 104202240 104071168 104464384 103677952 103284736 103809024 102760448 103415808 105644032 102629376 102629376 103940096 103022592 103284736 102891520 104726528 103677952 103284736 102891520 104071168 104202240 102498304 102760448 102367232 100007936 102498304 104071168 104202240 103153664 103940096 103153664 103022592 103284736 103940096 103677952 103546880 102629376 103546880 101974016 102498304 104071168 103546880 103677952 103940096 103284736 102105088 105119744 104071168 103677952 103415808 104071168 104726528 103546880
 103415808 103677952 103940096 105512960 104857600 103940096 103809024 102760448 103022592 103284736 103022592 103153664 104333312 102891520 103940096 104464384 103677952 103940096 102105088 103022592 102629376 104595456 103940096 102236160 102760448 102629376 103940096 102236160 104726528 102760448 102629376 102760448

它在103546880和103415808之间跳跃。

我需要它而没有\n分手,因为FANN训练的file.data并没有跳跃。

3 个答案:

答案 0 :(得分:2)

我无法想象C库会添加不需要的换行符。 正常 OS和文件系统(Windows和其他类Unix)也不应该。除非你使用一个非常罕见的操作系统或文件系统 - 如果你真的应该在你的问题中添加它,我认为当你打开文件以便编辑器读取它时,编辑器会添加不需要的换行符。

要确认,请使用适用于许多平台的优秀vim。它几乎可以读取所有内容,甚至是包含空字符的文件,并且可以转换为hexa以确保文件中的内容。

我刚刚创建了一个包含32000个元素的文件,每个元素包含10个字符(&#39; 104333312&#39;),vim说该行有320000个字符而不试图拆分它(当然折叠了,但正确计算为只有一行)。

答案 1 :(得分:0)

问题必须来自:

info.samplerate;

无论从哪里收集数据,都可能导致嵌入式换行。也许在采样中有些怪癖或者它可能超出了缓冲区,提示要写入新的缓冲区/数据行。在换行符之前您获得了多少数据条目?

答案 2 :(得分:-2)

用以下行替换 out = fopen(&#34; filedata.data&#34;,&#34; a +&#34;); 。 例如 out = fopen(&#34; filedata.data&#34;,&#34; a + b&#34;);

因此该文件不会有新行字符(\ n)。