• C

Merge two text files with c programming language

I am having issues with a c program. The program is to take two text files and merge them into one.

One file contains user names, the other one contains passwords. It should output it to a third file in this format

USERNAME1                                              PASSWORD1
USERNAME2                                              PASSWORD2
.                                                                 .
.                                                                 .

etc

The passwords are suppose to be indented 20 chars.

Thanks for any help you can give.
#include <stdio.h>
char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size)
{
   int count = stop - start;
   if ( count >= --size )
   {
      count = size;
   }
   sprintf(dst, "%.*s", count, src + start);
   return dst;
}
 
int main() {
  char c[20] = "                    ";  /* declare a char array */
  char c2[20]= "                    ";  /* declare a char array */
  //char c;
  FILE *file, *file2, *file3;  /* declare a FILE pointer  */
  
  file = fopen("username.txt", "r"); 
  file2 = fopen("password.txt", "r"); 
  file3 = fopen("concat.txt", "w");
  /* open a text file for reading */
 
  if(file==NULL) {
    printf("Error: can't open username file.\n");
    /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
    return 1;
  }
   else if(file2==NULL) {
    printf("Error: can't open password file.\n");
    /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
    return 1;
  }
   else if(file3==NULL) {
    printf("Error: can't open concatenated file.\n");
    /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
    return 1;
  }
  else {
      while(fgets(c2, 200, file2) != NULL && fgets(c, 200, file) != NULL) { 
      /* keep looping until NULL pointer... */
     fprintf(file3, "Username: %s Password: %s",substring(0, strlen(c), c, c), c2);    
      /* print the file one line at a time  */
    }
 
    printf("\n\nNow closing file...\n");
    getch();
    fclose(file);
    fclose(file2);
    fclose(file3);
    return 0;
  }
}

Open in new window

bbcacAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
Always post the issues you are facing.

>substring(0, strlen(c), c, c)
>char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size)

function requires 5 params and you are passing only 4. Not sure why you need all those params.
Also I dont see code for indenting password by 20 spaces.

>fgets(c, 200, file)
your buffers are only 20 chars long
0
bbcacAuthor Commented:
The attached code below is where the issue is.
I didn't realize that I attached my revised code in the orginal post. The issue is that it is getting the entire string including the '\n' character at the end of the time. I used 200 only becuase it is much larger than I every need. I think there is a MAX_LINE_LENGTH or something that I can put in there. How can I get a substring from the beginning of c to one character before the '\n'

My output for the below code which goes into file3 is as follows:
Username: username1
 Password: password1
Username: username2
 Password: password2
Username: username3
 Password: password3
Username: username4
 Password: password4
Username: username5
 Password: password5
Username: username6 Password: password6

while(fgets(c2, 200, file2) != NULL && fgets(c, 200, file) != NULL) { 
      /* keep looping until NULL pointer... */
     fprintf(file3, "Username: %s Password: %s",c, c2);    
      /* print the file one line at a time  */
    }

Open in new window

0
97WideGlideCommented:
Please see modified code below :

char * p;

while(fgets(c2, 200, file2) != NULL && fgets(c, 200, file) != NULL) {

/* add the following code */
  p = strchr(c, '\n');
    if (p) {
      *p = '\0';
    }
  p = strchr(c2, '\n');
    if (p) {
      *p = '\0';
    }
/* end add code */

      /* keep looping until NULL pointer... */
     fprintf(file3, "Username: %s Password: %s",c, c2);    
      /* print the file one line at a time  */
    }


Also, note that fgets() is not the best way to read lines of text from a stream.  See link below :
http://crasseux.com/books/ctutorial/getline.html

Good Luck.
0
97WideGlideCommented:
1 more thing ... since you are removing the \n from c2 you will need to put it back into your fprintf() as in :
fprintf(file3,"Username: %s Password: %s\n", c, c2);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.