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

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. :-/

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

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
 }

phew....at last..thank you adapost....:) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.