Link to home
Start Free TrialLog in
Avatar of bbcac
bbcac

asked on

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

Avatar of sunnycoder
sunnycoder
Flag of India image

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
Avatar of bbcac
bbcac

ASKER

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

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.
ASKER CERTIFIED SOLUTION
Avatar of 97WideGlide
97WideGlide

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial