#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!