Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Segmenting a subtring from a main string C

Get specific content from a file and store it in a variable. So far I get that I can convert the file content into a string. But I'm not sure how can I 'extract' the content from the string I converted and would like some help.

The original file looks something like this:

XXXXXX
XXXXX

Addr = 12:23:34:45:45
XXX
XXX

I need to extract and store the Addr as a string. Want to look for the prefix Addr = and just copy it into a buffer. But I don't know how can I do it...

So far my code looks like below:

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

//So far I'm looking for it using the MAC addr format 
const char *get_mac_addr(char *str, char *dest) {
    if (str == NULL)
        return 0;

    char *start = NULL;
    int token_count = 0;

    char *ptr = str;
    if (*ptr && *(ptr + 1))  // skip two chars in the beginning of the string
        ptr += 2;
    else
        return 0;

    while (*ptr != '' && *ptr != '
' && *ptr != '
') {
        if (token_count == 5)
            break;

        /* if ':' found and previous two characters are hexidecimal digits then
           the substring could be part of MAC
        */
        if (*ptr == ':' && isxdigit(*(ptr - 1)) && isxdigit(*(ptr - 2))) {
            token_count++;
            if (start == NULL)
                start = ptr - 2;

            int i = 0;
            while (*ptr != '' && i++ < 3)          
                ptr++;  
        } else {
            start = NULL;
            token_count = 0;
            ptr++;
        }
    }

    strcpy(dest, start);

    return dest;
}

const char *file2str(){
    /* declare a file pointer */
    FILE *infile;
    char *buffer;
    long numbytes;
    char dest[18];
    
    /* open an existing file for reading */
    infile = fopen("~/Desktop/file.config", "r");
    
    /* quit if the file does not exist */
    //if (infile == NULL)
    //    return 1;
    
    /* Get the number of bytes */
    fseek(infile, 0L, SEEK_END);
    numbytes = ftell(infile);
    
    /* reset the file position indicator to 
    the beginning of the file */
    fseek(infile, 0L, SEEK_SET);    
    
    /* grab sufficient memory for the 
    buffer to hold the text */
    buffer = (char *)calloc(numbytes, sizeof(char)); 
    
    /* memory error */
    //if(buffer == NULL)
    //    return 1;
    
    /* copy all the text into the buffer */
    fread(buffer, sizeof(char), numbytes, infile);
    fclose(infile);

    /* confirm we have read the file by
    outputing it to the console */
    printf("The file called test.dat contains this text

%s", buffer);

    //memset(dest, '/0', sizeof(dest));
    get_mac_addr(buffer, dest);

    /* free the memory we used for the buffer */
    //free(buffer);
    printf("Dest is 

%s", dest);
    return dest;
}

int main() {
    printf(file2str);
    return 0;
}
question from:https://stackoverflow.com/questions/66050851/segmenting-a-subtring-from-a-main-string-c

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

1 Reply

0 votes
by (71.8m points)

I really appreciate your help. Please bare with me as I'm not very good at c programming. I would like to convert the main function into one function so I can directly call it and return a string. I converted the main function as following, but I'm not sure why when I print it, there is nothing show up:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE * xfopen(const char *path, const char *mode);

const char *MACadd()
{
        char buf[256];
        char *addr = NULL;
        
        FILE *in = xfopen("~Desktop/file.config", "r");
        while( fgets(buf, sizeof buf, in) ){
                addr = strstr(buf, "Addr = ");
                if( addr && addr < buf + sizeof buf - ADDRLEN){
                        addr += strlen("Addr = ");
                        addr[ADDRLEN] = '';
                        break;
                }
        }
        //printf("addr = %s
", addr);
        return addr;
}

FILE *xfopen(const char *path, const char *mode)
{
        FILE *fp = fopen(path, mode);
        if( fp == NULL ){
                perror(path);
                exit(EXIT_FAILURE);
        }
        return fp;
}

int main(){

    printf("%s", MACadd());
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...