Search code examples
carraysfilefann

Problems with fprintf while writing large info


I'm trying to write an array of 18000 items (that array is from the transformation wav>array), and when I write the items in a file appears a \n when I don't want to write that, here is the code:

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);

Here is a part of filedata.data where appears that jump of line

    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

It jumps between 103546880 and 103415808.

I need it without \n breakup's because the file.data for FANN training and it can't have jumps.


Solution

  • I cannot imagine the C library adding an unwanted newline. Normal OS and filesystems (Windows and other Unix-like) should not either. Unless you are using a really uncommon operating system or file system -if yes your really should add it in your question-, I presume the unwanted newline was added by the editor when you opened the file to read it by the editor.

    To confirm, use the excellent vim which is disponible for many platforms. It can read almost everything, even files containing null characters and can convert to hexa to make sure what really is in a file.

    I've just created a file with 32000 elements of 10 characters each ('104333312 ') and vim says the line has 320000 characters without trying to split it (of course the line is folded, but correctly counted as only one line).