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

Attachments
0.41901 0.6667 0.0122 0.2222 0.0252 0.1770 0.2757 0.0000 0.0000 0.5000 0.5000
0.52433 0.3333 0.1257 0.2778 0.0175 0.0619 0.0000 0.0000 0.0000 0.5000 1.0000
0.98677 0.3333 0.2022 0.4444 0.0198 0.0133 0.0000 0.0000 0.0000 0.5000 1.0000
0.74703 0.3333 0.1406 0.3333 0.2414 0.3363 0.1481 0.0000 0.0000 1.0000 1.0000
0.89259 0.3333 0.3051 0.1667 0.3482 0.6018 0.1893 0.0000 0.0000 0.0000 1.0000
0.59746 0.3333 0.2238 0.2778 0.1911 0.3230 0.6049 0.0000 0.0000 0.0000 1.0000
1.00000 0.3333 0.4352 0.2222 0.0117 0.3407 0.1029 0.0000 0.0000 1.0000 1.0000
0.21974 0.3333 0.3457 0.1667 0.3880 0.5929 0.2140 0.5000 0.0000 0.0000 0.5000
0.11235 0.6667 0.0147 0.3333 0.1175 0.0088 0.5226 0.0000 0.0000 0.0000 0.0000
0.00000 0.6667 0.7317 0.4444 1.0000 0.9602 0.6173 0.0000 0.0000 0.0000 0.0000
0.56414 0.3333 0.3040 0.3333 0.2859 0.3009 0.0000 0.0000 0.0000 0.5000 1.0000
0.00757 0.3333 0.3573 0.1667 0.3054 0.5221 0.3704 0.0000 0.0000 0.0000 0.0000
0.38345 1.0000 0.2929 1.0000 0.8077 0.5442 0.0000 0.5000 0.0000 0.5000 0.5000
0.42526 0.6667 0.0063 0.4444 0.0652 0.5133 0.6255 0.0000 0.0000 0.5000 0.5000
0.39805 0.3333 0.0771 0.0000 0.0392 0.0000 1.0000 1.0000 0.0000 1.0000 0.5000
0.67262 0.6667 0.0025 0.4444 0.0675 0.2743 0.1358 0.0000 0.0000 1.0000 1.0000
0.84934 0.3333 0.5259 0.2778 0.2501 0.1726 0.0000 0.0000 0.0000 1.0000 1.0000
0.100
0.156
0.213
0.269
0.325
0.381
0.438
0.494
0.550
0.606
0.663
0.719
0.775
0.831
0.887
0.944
1.000

Of course, you have linker error because nobody (except you) knows such library function as concat (probably you want strcat).

The second attempt failed because fgets function puts the last '\n' character in the buffer (read library function specifications more carefully):

Input buffer after successful fgets:
<line contents><'\n'><'\0'>

So you must overwrite (or erase) this newline character when concatenate two lines to form output line.
Probably you want separate two output line parts with blank character. So replace the 1st line trailing newline with blank, for example:

int csz = strlen(c) - 1;
if (csz >= 0 && c[csz] == '\n')
    c[csz] = ' ';
/* or c[csz] = '\0'; to erase it at all */

Now use fprintf with double %s %s specifiers (see commented line in your snippet)...

Apropos, declare longer input buffers for file lines (at least 256, for example). Always test open result: if (file == NULL) { ERROR! } ...

Of course ArkM explained your problem and I have added few statements suggested by ArkM.

#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  */

  int i;
  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) {
       i=strlen(c);
       c[i-1]='\0';
       fprintf(filep,"%s%s",d,c); //second attempt
    }
    printf("\n\nClosing file...\n");
    fclose(file);
    fclose(file2);
    fclose(filep);
    return 0;
  }

Thank for ArkM and Adatapos.
First, i do made mistake by using concat. ArkM you are right it should be used strcat. :$

Second, the carriage return character or "\n" was eliminated thank for Adatapos :) , however there was slight mistake, it began from second line to the rest. It contain blank space, as you can see in the attachment.

I wonder how to get rid of this slight mistake. :-/

Attachments
0.100 0.41901 0.6667 0.0122 0.2222 0.0252 0.1770 0.2757 0.0000 0.0000 0.5000 0.500
 0.156 0.52433 0.3333 0.1257 0.2778 0.0175 0.0619 0.0000 0.0000 0.0000 0.5000 1.000
 0.213 0.98677 0.3333 0.2022 0.4444 0.0198 0.0133 0.0000 0.0000 0.0000 0.5000 1.000
 0.269 0.74703 0.3333 0.1406 0.3333 0.2414 0.3363 0.1481 0.0000 0.0000 1.0000 1.000
 0.325 0.89259 0.3333 0.3051 0.1667 0.3482 0.6018 0.1893 0.0000 0.0000 0.0000 1.000
 0.381 0.59746 0.3333 0.2238 0.2778 0.1911 0.3230 0.6049 0.0000 0.0000 0.0000 1.000
 0.438 1.00000 0.3333 0.4352 0.2222 0.0117 0.3407 0.1029 0.0000 0.0000 1.0000 1.000
 0.494 0.21974 0.3333 0.3457 0.1667 0.3880 0.5929 0.2140 0.5000 0.0000 0.0000 0.500
 0.550 0.11235 0.6667 0.0147 0.3333 0.1175 0.0088 0.5226 0.0000 0.0000 0.0000 0.000
 0.606 0.00000 0.6667 0.7317 0.4444 1.0000 0.9602 0.6173 0.0000 0.0000 0.0000 0.000
 0.663 0.56414 0.3333 0.3040 0.3333 0.2859 0.3009 0.0000 0.0000 0.0000 0.5000 1.000
 0.719 0.00757 0.3333 0.3573 0.1667 0.3054 0.5221 0.3704 0.0000 0.0000 0.0000 0.000
 0.775 0.38345 1.0000 0.2929 1.0000 0.8077 0.5442 0.0000 0.5000 0.0000 0.5000 0.500
 0.831 0.42526 0.6667 0.0063 0.4444 0.0652 0.5133 0.6255 0.0000 0.0000 0.5000 0.500
 0.887 0.39805 0.3333 0.0771 0.0000 0.0392 0.0000 1.0000 1.0000 0.0000 1.0000 0.500
 0.944 0.67262 0.6667 0.0025 0.4444 0.0675 0.2743 0.1358 0.0000 0.0000 1.0000 1.000
 1.000 0.84934 0.3333 0.5259 0.2778 0.2501 0.1726 0.0000 0.0000 0.0000 1.0000 1.000

Remove the space in following statement:

It is your code:

fprintf(filep,"%s %s",d,c); //second attempt

Correction

fprintf(filep,"%s%s",d,c); //second attempt

Than's again adatapost,

actually i tried it before i reply again to this forum, the blank space in front of second line and the rest is no longer exist , but other slight problem arise.

As you can see below (or in the attachment), there no blank space between first column that came from data2.txt and second column that came from first column of data.txt

0.1000.41901 0.6667 0.0122 0.2222 0.0252 0.1770 0.2757 0.0000 0.0000 0.5000 0.500
0.1560.52433 0.3333 0.1257 0.2778 0.0175 0.0619 0.0000 0.0000 0.0000 0.5000 1.000
0.2130.98677 0.3333 0.2022 0.4444 0.0198 0.0133 0.0000 0.0000 0.0000 0.5000 1.000
0.2690.74703 0.3333 0.1406 0.3333 0.2414 0.3363 0.1481 0.0000 0.0000 1.0000 1.000
0.3250.89259 0.3333 0.3051 0.1667 0.3482 0.6018 0.1893 0.0000 0.0000 0.0000 1.000

Attachments
0.1000.41901 0.6667 0.0122 0.2222 0.0252 0.1770 0.2757 0.0000 0.0000 0.5000 0.500
0.1560.52433 0.3333 0.1257 0.2778 0.0175 0.0619 0.0000 0.0000 0.0000 0.5000 1.000
0.2130.98677 0.3333 0.2022 0.4444 0.0198 0.0133 0.0000 0.0000 0.0000 0.5000 1.000
0.2690.74703 0.3333 0.1406 0.3333 0.2414 0.3363 0.1481 0.0000 0.0000 1.0000 1.000
0.3250.89259 0.3333 0.3051 0.1667 0.3482 0.6018 0.1893 0.0000 0.0000 0.0000 1.000
0.3810.59746 0.3333 0.2238 0.2778 0.1911 0.3230 0.6049 0.0000 0.0000 0.0000 1.000
0.4381.00000 0.3333 0.4352 0.2222 0.0117 0.3407 0.1029 0.0000 0.0000 1.0000 1.000
0.4940.21974 0.3333 0.3457 0.1667 0.3880 0.5929 0.2140 0.5000 0.0000 0.0000 0.500
0.5500.11235 0.6667 0.0147 0.3333 0.1175 0.0088 0.5226 0.0000 0.0000 0.0000 0.000
0.6060.00000 0.6667 0.7317 0.4444 1.0000 0.9602 0.6173 0.0000 0.0000 0.0000 0.000
0.6630.56414 0.3333 0.3040 0.3333 0.2859 0.3009 0.0000 0.0000 0.0000 0.5000 1.000
0.7190.00757 0.3333 0.3573 0.1667 0.3054 0.5221 0.3704 0.0000 0.0000 0.0000 0.000
0.7750.38345 1.0000 0.2929 1.0000 0.8077 0.5442 0.0000 0.5000 0.0000 0.5000 0.500
0.8310.42526 0.6667 0.0063 0.4444 0.0652 0.5133 0.6255 0.0000 0.0000 0.5000 0.500
0.8870.39805 0.3333 0.0771 0.0000 0.0392 0.0000 1.0000 1.0000 0.0000 1.0000 0.500
0.9440.67262 0.6667 0.0025 0.4444 0.0675 0.2743 0.1358 0.0000 0.0000 1.0000 1.000
1.0000.84934 0.3333 0.5259 0.2778 0.2501 0.1726 0.0000 0.0000 0.0000 1.0000 1.000

Yes, it is.

Try with following code:

while(fgets(c, 78, file)!=NULL && fgets (d,6,file2)!=NULL) {
         if(c[0]!='\n')
               fprintf(filep,"%s %s\n",d,c); //second attempt
 }
This question has already been answered. Start a new discussion instead.