Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

c - How do I read a file, take the file content (which is just simple characters) and put them in a variable to compare?

So I have an assignment for hs and honestly I do not know how to program in C so I would appreciate all the help I can get.

Anyways my problem is that I cannot for the life of me read a content of a file, asign it to a variable and compare it to another variable if the content is the same.

I am making a login interface with already pre-input admin email and admin password and here is my mess of a code for now:

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

int main()
{
    FILE *emailf;
    emailf = fopen("email.txt", "r");
    char* fgets(char* emailx){
    return emailx;
    }
    fclose(emailf);
    char email [30];
    char password [16];
do{
    printf("E-mail:");
    scanf("%c", &email);


    }
    while (email != emailx);

What it does is tells me that emailx variable is not even declared so if you could help me I would be glad! And also it does not have to resemble this code since I do not know jack crap about C programming language so if you could tell me a different way to try what I am trying would be good as well. Thank you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are several things that are not good in your program.

Firstable, are you sure you are openning properly the file "email.txt" ?

Then, this part of the code is wrong, you cannot define a function inside a function, especially if this function already exist in a library:

 char* fgets(char* emailx){
return emailx;
}

maybe you should try something like

emailx = fgets(emailx);

But first you should declare your variable.

I ve never used those functions so I don t know how it works precisely, but chek the manual.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...