Hi everyone,
I get stuck with this homework. May be someone here can help me. I must combine two txt file, line by line.
My first datafile contain 1 column, contain float numbers and the other contain many columns, also float numbers (Here i attached both the txt files).
For example for the first line the result should be:
0.100 0.41901 0.6667 0.0122 0.2222 0.0252 0.1770 0.2757 0.0000 0.0000 0.5000 0.5000
Here my code:
#include <stdio.h>
int main() {
char c[78]; /* declare a char array */
char d[6];
char e[90];
FILE *file,*filep,*file2; /* declare a FILE pointer */
file = fopen("data.txt", "r");
file2 = fopen("data2.txt", "r");
filep = fopen("dataprint.txt","w+");
/* open a text file for reading */
while(fgets(c, 78, file)!=NULL && fgets (d,6,file2)!=NULL) {
concat (e,d,c);//fist attempt
fprintf(filep,"%s",e);
//fprintf(filep,"%s %s",d,c); //second attempt
}
printf("\n\nClosing file...\n");
fclose(file);
fclose(file2);
fclose(filep);
system("pause");
return 0;
}
As you can see above at my first attempt i tried to used concat, but when i tried to compile it, i got the error message like this:
C:\DOCUME~1\a\LOCALS~1\Temp\ccAldaaa.o(.text+0xdb) In function `main':
[Linker error] undefined reference to `concat'
C:\DOCUME~1\a\LOCALS~1\Temp\ccAldaaa.o(.text+0xdb) ld returned 1 exit status
Then i tried to second attempt using this line : fprintf(filep,"%s %s",d,c)
but the result not as i expected, there was space between line, like i put "\n\n" character on every line.
like this:
0.100 0.41901 0.6667 0.0122 0.2222 0.0252 0.1770 0.2757 0.0000 0.0000 0.5000 0.5000
0.156 0.52433 0.3333 0.1257 0.2778 0.0175 0.0619 0.0000 0.0000 0.0000 0.5000 1.0000
0.213 0.98677 0.3333 0.2022 0.4444 0.0198 0.0133 0.0000 0.0000 0.0000 0.5000 1.0000
actually i want the result like this:
0.100 0.41901 0.6667 0.0122 0.2222 0.0252 0.1770 0.2757 0.0000 0.0000 0.5000 0.5000
0.156 0.52433 0.3333 0.1257 0.2778 0.0175 0.0619 0.0000 0.0000 0.0000 0.5000 1.0000
0.213 0.98677 0.3333 0.2022 0.4444 0.0198 0.0133 0.0000 0.0000 0.0000 0.5000 1.0000
I hope someone here can help me.
regards,
headacheinC ;)