Hey I'm trying to read the first line from a .txt file and print it to the terminal with the bellow code.
Code:
char pass[40];
FILE *login = fopen("test.txt", "r");
fgets(pass, 40, login);
printf("%s\n", pass);
I set the char size to 40 because that was the maximum size allowed when I printed to line to the file. The problem I'm having is there seems to be a bunch of blank space and a new line at the end of the string. I tried to remove it with the bellow for loop but when I do I get a bunch of junk at the end that shows up a "?" characters in my terminal. I have tried checking for new lines and not "0" in the for loop but I still get junk left at the end of the string showing as "?".
Code:
char passhold[40];
for(int i = 0; pass[i] != 0; i++){
passhold[i] = pass[i];
}
printf("%s\n", passhold);
I cant work out how to fix it. I just want to read the first line and not have blank space at the end of the string.