Problem reading from external file

This is a discussion on Problem reading from external file within the C Programming forums, part of the General Programming Boards category; Hey im totally new to the boards and c programming and ive been trying to make a program in which ...

Problem reading from external file

Hey im totally new to the boards and c programming and ive been trying to make a program in which the user inputs a username and password

now when they try to login again im having trouble getting the program to read the username(external filename) and the password which is contained inside it.

here is my code...its all messed up cuz i was trying different things. If someone could please tell me how to get it so that it reads the external file("username") and opens it and compares the password inputted by the user and the password contained inside the external file("username") that would be great. Thanks

Is that what you intended? None of those braces actually did anything in your program. If they were supposed to do something, then it appears as if you're trying to create nested functions. Furthermore, your code is horrible to read and try and fix, because of your insistance on all of your variables being globals.

Its horrible to read cuz half of that stuff was me just trying different things
username and password are my only variables.
username is what is inputted by the user and the username is stored as the external filename. Now the password is written to that file. I have all that accomplished but im having a hard time getting it to open the username and match the password inputted by the user and the one that is contained in the file. How do i go about opening the username file and is my if statement enough to compare whats inputted and whats in the file?

fopen() only opens the file for reading, it doesn't do any reading itself. The arguments for fopen "&s", username will not do anything but give an error. The syntax for fopen is:
FILE *fopen(const char * restrict path, const char * restrict mode);
fp = fopen("file.txt", "r"); will open file.txt for reading.

perror is not like printf, it's syntax is:
void perror(const char *string);
perror(""); will print the error without any text before it. perror("file.txt") will print file.txt: [error here]. perror() does NOT accept arguments like printf and such does.

There are many ways to read from a file once you have open, I suggest you search the forums or google for how to do that. If you have problems, post your problem and code.

Well then you're stupid. It tells you how to get a line of text. You then take that line of text, and use it as your file name once you've removed the newline. Seriously, you obviously have no idea how to read anything from the user correctly, because you're doing stuff like this:

Code:

if((fp = fopen("&s" ,username, "r")) == NULL)
perror("%s" ,username);

That's supposed to do what exactly? You were given the FAQ link for a reason. It's apparent to everyone here who does know how to correctly read information from the user, that you do not.

any questions any type in programming
a ready made answer
-----------------------------------------------------------------------------------
FORTUNE FAVOURS THE BOLD!
-----------------------------------------------------------------------------------