Ecco come ho modificato il sorgente:
codice:
#include <stdio.h>
#include <stdlib.h>
int main ( int argc, char * argv[] )
{
FILE *fda, *fdb, *fdout;
char *res1, *res2;
char bufa[200];
char bufb[200];
if ( argc != 4 ) {
printf("usare %s file1 file2 outfile\n",argv[0]);
return 1;
}
fda = fopen (argv[1], "r");
if ( fda == NULL) {
perror("Errore in apertura del file");
exit(1);
}
fdb = fopen (argv[2], "r");
if ( fdb == NULL) {
perror("Errore in apertura del file");
exit(1);
}
fdout = fopen (argv[3], "a");
while (1)
{
res1=fgets(bufa, 200, fda);
if( res1==NULL )
break;
res2=fgets(bufb, 200, fdb);
if( res2==NULL )
break;
fprintf( fdout ,"%s%s\n", res1, res2);
}
fclose ( fda);
fclose ( fdb);
fclose ( fdout);
return 0;
}
Il problema è che dati i due file:
file1:
a b c
file2:
1 2 3
Ora ottengo:
a b c
1 2 3
d e f
4 5 6
Mentre io desidero:
a b c 1 2 3
d e f 4 5 6
Il problema è che fgets() legge fino a \n incluso e quindi lo stampa. Come faccio a leggere la linea fino a \n escluso??