#include <stdio.h> #include <string.h> main() { FILE *p; char buf_login[10]; char buf_stream[10]; puts("Login:"); scanf("%s",buf_login); printf("Your login is:%s\n", buf_login); p=fopen("12.txt","rt"); fgets(buf_stream, 10, p); fclose(p); if (strcmp(buf_stream,buf_login)==0) { puts("OK"); } else { puts("error!"); } } 

Cannot pass "authentication". In the file 12.txt , for example, is abcd . I compile, I enter abcd and still throws error! instead of ok. I can not understand where the error. It seems the code is correct. Encoding 12.tht ASCII file. I am writing in Borland 2.0, on a virtual machine on Ubuntu.

  • four
    Remove the picture and paste the normal source. The link "edit" under the post. And do not forget to format the source as a code (button {} in the editor). - PinkTux

3 answers 3

Errors can be two ...

  1. If the file is opened for reading, then it is actually not the string "abcd", but the string "abcd \ n" (with a line break). You can remove it by opening the file itself in edit mode and using the arrow keys of the cursor and deleting, remove the last non-printable character from the file (after the "d" character). You should have one line without carriage return.

  2. The file does not open, because it is not in the same directory from which the program is started. Confirmation of opening the file should be a valid pointer p. You do not check it in your code, but look at least in the debugger. Although most likely, in the absence of a file, the program will fall out with an error.

The code itself is basically working.

  • The file is in the correct directory, so the problem is with \n , can you help? - Artyom Olufsen
  • one
    @Artyom Olufsen Help remove a carriage return from a file? Are you seriously? Is it really that bad? :) - Max ZS
  • To help you understand, there is no line feed in the file. - Artyom Olufsen
  • I created a file on Windows and dropped it into a virtual machine and it all worked ... I will deal with \ n. Thank you very much! - Artyom Olufsen
  • one
    @ArtyomOlufsen, everything works. Try to create a file like this: echo -n abcd >12.txt / In general, go to programming directly in Linux, there will be fewer glitches. - avp 9:09 pm

The cause of the problem You have already been indicated - the symbol '\ n' at the end of the line in the file "12.txt".

We read man 3 fgets:

It has been shown that there has been more than Reading stops after an EOF or a newline. If a newline is read, it is stored in the buffer. A terminating null byte ('\ 0') is stored after the last character in the buffer.

Those. you have in the buffer a sequence of characters "abcd", after which lies the character '\ n' and only AFTER it is already the terminator of the string '\ 0'.

And when you read the login from the keyboard using scanf, the '\ n' character is NOT in the buffer, so the comparison does not work - the lines are of different lengths !.

This extra '\ n' is easy to remove as follows:

  buf_stream[strlen(line)-1] = '\0'; 

Those. just gloss over the unnecessary character '\ n' with a line terminator.

And, by the way - yes, it is MANDATORY to check the condition of opening a file !!!

    Try to enter your program password 0987654321 for the sake of laughter - it will fall down due to a buffer overflow.

    The scanf function with %s format is not safe since it does not check the buffer size.

    A slightly safer option is scanf("%9s",buf); Note that you have an array of 10 elements, but it contains 9 characters + a line ending marker.