Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:18:33+00:00 2026-06-06T17:18:33+00:00

This is my code: int main(int argc, char *argv[]){ FILE *fp; char *tmp, *tmp2,

  • 0

This is my code:

int main(int argc, char *argv[]){
    FILE *fp;
    char *tmp, *tmp2, *user, *pass, *line;
    printf("Inserire utente: "); scanf("%ms", &user); /* scanf %ms alloca dinamicamente la memoria */
    printf("Inserire password: "); scanf("%ms", &pass);
    line = malloc((strlen(user)+strlen(pass)) * sizeof(char)); /* DUBBIOOOOOOOOOO */
    fp = fopen("/home/pol/auth.txt", "r");
    if(fp == NULL){
        printf("Errore apertura file\n");
        return EXIT_FAILURE;
    }
    while(!feof(fp)){
        fgets(line, /* E QUI?? */ , fp);
        tmp = strtok(line, " ");
        tmp2 = strtok(NULL, "\n"); /* con fgets ultimo carattere è \n (se pwd=12 => 12\n) quindi devo tagliare prima di \n */
        if((strcmp(tmp,user) == 0) && (strcmp(tmp2,pass) == 0)){
            printf("USER: %s - PASS: %s\n", tmp, tmp2);
            free(user);
            free(pass);
            return EXIT_SUCCESS;
        }
        else{
            printf("Utente o password errati o non presenti nel DB\n");
            free(user);
            free(pass);
            return EXIT_FAILURE;
        }
    }
    return EXIT_SUCCESS;
}

I would like to know if:

  1. it is correct to write into fgets the value sizeof(line) or not. My doubt is that line contain a pointer so it is always 4 or 8 bytes…
  2. line is right allocated?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:18:36+00:00Added an answer on June 6, 2026 at 5:18 pm
    1. No, sizeof(line) will return the size of the pointer. What you really want to do is just

      size_t size_required = strlen(user) + strlen(pass) + 1; /* Don't forget about \0! */
      

      And then just use that size as the size of line. (BTW: your code does not free line.)

      Edit: Actually, since you ultimately want to read there user + password + 2 token separators searched for by strtok, you want to add extra 3 characters: 1 for terminating \0, 1 for first separator ‘ ‘, and 1 for next separator ‘\n’. So that should be:

      size_t size_required = strlen(user) + strlen(pass) + 3; /* Don't forget about \0! */
      
    2. You should remember, that m specifier for dynamic allocation is a non-standard extension. It won’t work on some systems. And you should release user and pass only if their scanf calls return positive value (as in: if they succeed).

    3. Allocation of line is incorrect: you allocate one character too few, so terminating \0 character won’t fit (see above). Note that strlen returns string size excluding the terminating \0 character.

      Additionally, sizeof(char) is pointless. The standard guarantees that sizeof(char) is exactly 1 (because sizeof does not really return number of bytes, but number of chars required to cover bit representation of an argument).

    4. BONUS You should check tmp and tmp2 for NULL, just in case. Calling strcmp on NULL argument will cause segmentation fault. This is important: if a line read contains user+password longer than provided earlier, a token separator will not be read by fgets, and strtok, unable to find, for example, final ‘\n’ character, will return NULL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my code #include<stdio.h> int main( int argc ,char *argv[] ) { FILE
I am running this code: #include <iostream> #include <cstddef> int main(int argc, char *argv[]){
With this code: #include <iostream> int main(int argc, char *argv[]) { return 0; }
I have written a source code like: int main(int argc, char *argv[]) { QFile
code: int main(int argc, char **argv) { int fd = open(test.txt, O_CREAT|O_RDWR, 0200|0400); if(fd
int main(int argc, char *argv[]) { FILE *fp = fopen(a.txt, wt); fprintf(fp, AAAA); //
In this code: int main(int a, int b) { printf( main(int, int) works \n\n);
This code causes a Segmentation Fault: int main(){ char *p; char a[50] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
I have this code: #include <stdio.h> #include <openssl/sha.h> #include <openssl/ssl.h> int main(){ printf(OpenSSL version:
Heres my code: void *PrintLine(void *line) { printf(Line: #%s\n, (char *)line); pthread_exit(NULL); } int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.