3
    #include <stdio.h> 
    #include <stdlib.h>
    #include <string.h>

    int main() {
      FILE *userfile, *pwfile, *usernamesPasswords, *readUsernamesPasswords;
      char u, p, user[20][25], pass[20][20], userLine[25], passLine[20];
      int i = 0;
      int j = 0;
      int userIndex = 0;
      int passIndex = 0;

      userfile = fopen("usernames.txt", "r");
      pwfile = fopen("passwords.txt", "r");

      if (userfile == NULL || pwfile == NULL) {
        printf("Cannot open file \n");
        exit(0);
      }

      u = fgetc(userfile);
      while (u != EOF) {
        if ( u != '\n'){
          userLine[userIndex++] = u;
        } else {
          userLine[userIndex] = '\0';
          strcpy(user[i], userLine);
          userIndex = 0;
          i++;
        }
        u = fgetc(userfile);
      }
      fclose(userfile);


      p = fgetc(pwfile);
      while (p != EOF) {
        if ( p != '\n'){
           passLine[passIndex++] = p;
        } else {
            passLine[passIndex] = '\0';
            strcpy(pass[j], passLine);
            passIndex = 0;
            j++;
        }
        p = fgetc(pwfile);
      }
      fclose(pwfile);

      usernamesPasswords = fopen("usernamesPasswords.txt", "w");
      int w, k;
      char newLine[1024];
      for (w=0;w<20;w++) {
        strcat(newLine, user[w]);

        int q = strlen(newLine);
        for (k=0;k<(25-q);k++) {
          strcat(newLine, " ");
        }

        strcat(newLine, pass[w]);
        strcat(newLine, "\n");
        strcat(newLine, "\0");
        fputs(newLine ,usernamesPasswords);
        strcpy(newLine, "");
      }
      fclose(usernamesPasswords);

      printf("\nDo you want to display the new file? Enter (y) to view and any other key to exit\n");
      char word;
      scanf("%c",&word);

      if (word == 'y') {
        readUsernamesPasswords = fopen("usernamesPasswords.txt", "r");
        if (readUsernamesPasswords == NULL) {
          printf("Cannot open file \n");
          exit(0);
        }

        char r;
        while((r=fgetc(readUsernamesPasswords))!=EOF) {
          printf("%c", r);
        }
        fclose(readUsernamesPasswords);
      }

      return 0;
    }

Expected Output:

Moodie                  123456
Intelllligent           password
Happee                  12345678
Mischeivous             qwerty
SweetKristy             123456789
KristyHoney             12345
BubblySnowflake         1234
AngelicPrincessKristy   111111
FairyPrincessKristy     1234567
BabyKristyButterfly     dragon
daffyusers              123123
magpiedoodle            baseball
aspiringthreat          abc123
landmarksspreader       football
cavaliervanquish        monkey
chatteringparma         letmein
veggiehydrogen          696969
teethunsure             shadow
rumpboast               master
lidarstrudel            666666

Instead...

                  123456
           password
                  12345678
             qwerty
             123456789
             12345
         1234ke
   111111incessKristy
     1234567sKristy
     dragonutterfly
              123123
            baseball
          abc123
       footballer
        monkeysh
         letmein
          696969
             shadow
               master
            666666

This only happens when I try to output on terminal. The actual usernamesPasswords.txt file comes out as expected. The left side of strings only seem to get printed only when I newline in place of whitespace...

I even tried fgets and fread but similar output. I have read other posts about eating or consuming so I tried using fgets or fread as suggested and even tried unget. did not seem to work. I have been looking all over stack overflow to no avail.

If this is a duplicate, I apologize in advanced. I have really tried to look all over the web for couple hours before deciding to post.

I usually don't post cuz stack overflow usually has everything already...

Please help! Thank you!

John
  • 31
  • 1

2 Answers2

2

You cannot call strcat or strlen on uninitialized storage. It's Undefined Behaviour. String functions generally require input string arguments to be correctly NUL-terminated. (Without the terminator, there is no way to tell where the string ends.)

And strcat(s, "\0") is a no-op because the second argument is effectively an empty (zero-length) string. (And, as above, if s is not already NUL-terminated, it is Undefined Behaviour.)

rici
  • 234,347
  • 28
  • 237
  • 341
0

In your program, this part of code is having few problems:

  char newLine[1024];
  for (w=0;w<20;w++) {
    strcat(newLine, user[w]);

    int q = strlen(newLine);
    for (k=0;k<(25-q);k++) {
      strcat(newLine, " ");
    }

    strcat(newLine, pass[w]);
    strcat(newLine, "\n");
    strcat(newLine, "\0");
    fputs(newLine ,usernamesPasswords);
    strcpy(newLine, "");
  }

Problems like newLine buffer is not initialized and used in strcat. Concatenating empty string has no effect - strcat(newLine, "\0");. You can solve these problems by simply initializing buffer with {0} and instead of concatenating an empty string, assign \0 at appropriate buffer index. But you don't need to do all this strcat's, instead you can simply do all this in just one line like this:

  char newLine[1024];
  for (w=0;w<20;w++) {
    snprintf (newLine, 1024, "%-25s%s\n", user[w], pass[w]);
    fputs(newLine ,usernamesPasswords);
  }

snprintf appends a terminating null character after the content written to the buffer.

Few points:
- You should check the return value of fopen.
- The return type of fgetc is int and not char and you are taking its return value in a char type.
- You need to take care of few things in your program like your program works on an assumption that usernames.txt and passwords.txt contains exact 20 lines. It's better to make it flexible.

H.S.
  • 11,654
  • 2
  • 15
  • 32