Home > Software design >  C language reading from file and placing in variables
C language reading from file and placing in variables

Time:12-31

In a text file I have the form Type:Username Password, how do I place it in three different variables, so that the variable Type is in the variable type, username is in username, and password is in password in C ?

Example:

Admin:Username Password

How to make?

Type:Admin
User:Username 
Pw:Password

Here's my code:

int ch;
int i = 0;
while ((ch = fgetc(fp)) != EOF) {
    // Check for the colon character
    if (ch == ':') {
        // We have reached the end of the type string
        // Move to the next variable
        i = 0;
        continue;
    }
    // Check for the space character
    if (ch == ' ') 
    {
        // We have reached the end of the username string
        // Move to the next variable
        i = 0;
        continue;
    }
    // Store the character in the appropriate variable
    if (i < 50) {
        if (type[0] == 0) {
            type[i] = ch;
        } else if (username[0] == 0) {
            username[i] = ch;
        } else {
            password[i] = ch;
        }
        i  ;

    }
}

CodePudding user response:

Considering your initial requirement that you posted in your Question, that a text file consists of following line:

Admin:Username Password

To store Admin in variable type, Username in variable username and similarly Password in variable Password.
You can declare a structure something similar to:

typedef struct user {
    char type[512];
    char username[512];
    char password[512];
} user;

And as @IngoLeonhardt commented, that strtok() or strchr() can be used to parse the line read from the text file, you can refer the below simple example code snippet to understand and further improve the logic.

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

typedef struct user {
    char type[512];
    char username[512];
    char password[512];
} user;

int main(void)
{
    FILE *file = fopen("test.txt","r");
    char *line = NULL;
    size_t linesize = 0;
    char *token;
    const char colon[2] = ":";
    const char space[2] = " ";
    
    user user_1;
    
    if (file) 
    {
        while (getline(&line, &linesize, file) != -1) 
        {
            printf("%s\n", line);
            
            token = strtok(line, colon);
            memcpy(user_1.type, token, strlen(token));
            
            token = strtok(NULL, space);
            strcpy(user_1.username, token);
            
            token = strtok(NULL, space);
            strcpy(user_1.password, token);
            
            printf("%s\n", user_1.type);
            printf("%s\n", user_1.username);
            printf("%s", user_1.password);
            free(line);
        }

        fclose(file);
    }
    return 0;
}

PS: There may be some flaws/bugs in above implementation, please consider the above code just as an example.

CodePudding user response:

First, you can read a line from the file as a string, by using the function fgets. After doing that, you can use the function strchr to find the ':' and ' ' delimiter characters in the string. Once you have found them, you can print the individual sub-strings using printf.

Here is an example:

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

bool read_exactly_one_line( char buffer[], int buffer_size, FILE *fp );

int main( void )
{
    FILE *fp;
    char line[200];
    char *p, *q;

    //open the input file
    fp = fopen( "input.txt", "r" );
    if ( fp == NULL )
    {
        fprintf( stderr, "Error opening file!\n" );
        exit( EXIT_FAILURE );
    }

    //read one line of input
    if ( ! read_exactly_one_line( line, sizeof line, fp ) )
    {
        fprintf( stderr, "File is empty!\n" );
        exit( EXIT_FAILURE );
    }

    //find the delimiter between the first and second token
    p = strchr( line, ':' );
    if ( p == NULL )
    {
        fprintf( stderr, "Unable to find first delimiter!\n" );
        exit( EXIT_FAILURE );
    }

    //print the first token
    printf( "Type:%.*s\n", (int)(p - line), line );

    //find the delimiter between the second and third token
    q = strchr( line, ' ' );
    if ( q == NULL )
    {
        fprintf( stderr, "Unable to find second delimiter!\n" );
        exit( EXIT_FAILURE );
    }

    //move pointer to start of second token
    p  ;

    //print the second token
    printf( "User:%.*s\n", (int)(q - p), p );

    //move pointer to start of third token
    q  ;

    //print the third token
    printf( "Pw:%s\n", q );

    //cleanup
    fclose( fp );
}

//This function will read exactly one line and remove the newline
//character, if it exists. On success, it will return true. If this
//function is unable to read any further lines due to end-of-file,
//it returns false. If it fails for any other reason, it will not
//return, but will print an error message and call "exit" instead.
bool read_exactly_one_line( char buffer[], int buffer_size, FILE *fp )
{
    char *p;

    //attempt to read one line from the stream
    if ( fgets( buffer, buffer_size, fp ) == NULL )
    {
        if ( ferror( fp ) )
        {
            fprintf( stderr, "Input error!\n" );
            exit( EXIT_FAILURE );
        }

        return false;
    }

    //make sure that line was not too long for input buffer
    p = strchr( buffer, '\n' );
    if ( p == NULL )
    {
        //a missing newline character is ok if the next
        //character is a newline character or if we have
        //reached end-of-file (for example if the input is
        //being piped from a file or if the user enters
        //end-of-file in the terminal itself)
        if ( getchar() != '\n' && !feof(stdin) )
        {
            printf( "Line input was too long!\n" );
            exit( EXIT_FAILURE );
        }
    }
    else
    {
        //remove newline character by overwriting it with a null
        //character
        *p = '\0';
    }

    return true;
}

For the input

Admin:Username Password

this program has the following output:

Type:Admin
User:Username
Pw:Password
  • Related