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

  • Home
  • SEARCH
  • 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 8476689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:14:58+00:00 2026-06-10T18:14:58+00:00

Below is my c code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> char

  • 0

Below is my c code:

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

char *username;
char *password;

void set_credentials(char*, char*);

int main(void)
{
  set_credentials();
  printf("%s\n", username);     //look here 3
  printf("%s\n", password);     //look here 4
  return EXIT_SUCCESS;
}

void set_credentials(char *username, char *password)
{
  char c;
  char lines[2][100];
  char * tmp = * lines;
  char * user = "user";
  int i = 0;
  FILE *fp = fopen("/netnfork/config/netnfork_credentials.properties", "r");

  if (fp == NULL)
    exit(EIO);
  while ((c = fgetc(fp)) != EOF)
  {
    if (c != '\n')
    {
      *tmp = c;
      tmp++;
    } else {
      *tmp = '\0';
      i++;
      tmp = lines[i];
    }
  }
  fclose(fp);
  i = 0;
  while (i < 2)
  {
    if (strncmp(user, lines[i], 4) == 0)
    {
      username = lines[i] + 5;
      printf("%s\n", username); //look here 1
    } else {
      password = lines[i] + 9;
      printf("%s\n", password);  //look here 2
    }
    i++;
  }
}

Now, when I run the code, I get this:

myname //for 1
mypassword //for 2
myname // for 3
mypasswo��� // for 4

I can’t understand why that behavior. Have anyone ideea about why that?

  • 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-10T18:14:59+00:00Added an answer on June 10, 2026 at 6:14 pm

    Here is a version that uses malloc() and free().

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    
    /* "user " */
    #define USERLINE_PREFIX 5
    /* "password " */
    #define PASSLINE_PREFIX 9
    
    
    
    void set_credentials(char**, char**);
    
    int main(void)
    {
      /* to point to the username and password obtained from a text file */
      char *username = NULL;
      char *password = NULL;
      set_credentials(&username,&password);
      /* (original scheme) printf("username: %s\n", username);  */
      /* (original scheme) printf("password: %s\n", password);  */
      printf("username: %s\n", username + USERLINE_PREFIX ); // line starts "user "    
      printf("password: %s\n", password + PASSLINE_PREFIX ); // line starts "password "
      /* (original scheme)  free(username - USERLINE_PREFIX); */
      /* (original scheme) free(password - PASSLINE_PREFIX); */
      free(password);
      free(username);
      return EXIT_SUCCESS;
    }
    
    void set_credentials(char **username, char **password)
    {
      /* file format:
      line 1 ->user <username>
      line 2 ->password <password>
      */
      char c;
      #define FILE_LINES 2
      #define MAX_LINE_LENGTH 100
      int i = 0, j = 0;
      char *lines[FILE_LINES];
      char *tmp = NULL;
      char *user = "user ";
      char *pass = "password ";
      char user_found = 0, password_found = 0;
      for (j = 0; j < FILE_LINES; j++)
      {
        lines[j] = malloc( MAX_LINE_LENGTH + 1 );
        lines[j][0] = '\0';
      }
      tmp = lines[0];
      const char *filename = "/netnfork/config/netnfork_credentials.properties";
      FILE *fp = fopen(filename, "r");
    
      if (fp == NULL)
      {
        printf("ERROR %d trying to open %s\n",errno,filename);
        /* if not exiting program, would need to free() here */
        exit(EIO);
      }
                                    /* in case more lines than expected */
      while ((c = fgetc(fp)) != EOF && i < FILE_LINES)
      {
        if (c != '\n')
        {
          *tmp = c;
          tmp++;
        } else {
          *tmp = '\0';
          i++;
          tmp = lines[i];
        }
      }
      if ( i < 2 )  {
         printf("ERROR: file %s is incomplete needs %d lines (password and user)\n",filename,FILE_LINES);
         /* if not exiting program, would need to free() here */
         exit(1);
      }
      fclose(fp);
      i = 0;
      while (i < FILE_LINES)
      {
        if (strncmp(user, lines[i], USERLINE_PREFIX) == 0)
        {
           user_found = 1;
           /* (original scheme) *username = lines[i] + USERLINE_PREFIX; */
           *username = lines[i];
        }
        else if  ( strncmp (pass, lines[i],PASSLINE_PREFIX) == 0 ) {
           password_found = 1;
           /* (original scheme) *password = lines[i] + PASSLINE_PREFIX; */
           *password = lines[i];
        }
        else {
           printf("ERROR: invalid line in file:\n");
           printf("%s\n",lines[i]);
           /* if not exiting program, would need to free() here */
           exit(1);
        }
        i++;
      }
      /* check for the extremely unlikely event that the two lines are both of the same type */
      if ( ! (password_found && user_found ) )
      {
         printf("ERROR: file %s is invalid, missing %s line\n",filename, (user_found) ? "password" : "user" );  
         /* if not exiting program, would need to free() here */
         exit(1);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code shown below #include <stdio.h> #include <stdlib.h> void Advance_String(char
I have some doubt on below code #include<stdio.h> int i=6; int main() { int
my test code is below: main1.c: #include <stdio.h> extern struct tt ; int main()
i have following code #include <stdlib.h> #include <stdio.h> int sorter( const void *first_arg,const void*
Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> typedef struct node { char*
In the code below i was expecting another output! : #include <stdio.h> #include <stdlib.h>
Please see the code snippet below : #include <iostream> using namespace std; int main()
I have a code like this below in /root_project/main.cpp : #include theoraplayer/TheoraVideoClip.h unsigned int
I create a file using the code below: #include <stdio.h> #include <stdlib.h> #include <sys/stat.h>
I wrote the below code which replaces '|' characters from the string. #include <stdio.h>

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.