如何将部分文本文件复制到C中的字符串中?

时间:2016-01-24 01:15:53

标签: c string file io

我有一个文本文件:

  

recipName = Fork friend = Cup sonName = Spork feature = hair sendName = Spoon“

我想要做的是将=符号前的所有单词复制到一个字符数组,并将=的右边的内容复制到另一个字符数组或字符串。

到目前为止,这是我的代码:

int main (int argc, char * argv[]) 
{
    char data[100];
    char line[5][100];
    char key[5][100];
    char value[5][100];   

    FILE * fdata = fopen(argv[1], "r"); //read data.txt file
    FILE * ftemp = fopen(argv[2], "r"); //read and write to template.txt file

    if (fdata == NULL) 
    {
        printf("could not read file.\n");
    }

    int i = 0;
    while (fgets(data, 100, fdata) != NULL) 
    {
        printf("data: %s", data);
        //this is where i get stuck, idk how to utilize this loop to copy the variable and variable names from the data.txt file i was given...thanks for the help

        ++i;
    }

    fclose(fdata);
    fclose(ftemp);

    return 0;
}

2 个答案:

答案 0 :(得分:0)

可能有一些更好的函数可以在string.h中完成更多你想做的事情 您将需要计算物流并计算' ='字符并决定如何处理。

#include <string.h>

char *ptr1, *ptr2;
char tempstring[100];
char before[100];
char after[100];

/* you already have data[] filled... where you get stuck */

ptr1 = strchr( data, '=' );   /* find first occurence of = */
ptr2 = strrchr( data, '=' );  /* find last occurence of = */

if ( *ptr1 == '\0' )
{
   /* did not find '=' print error message and stop */
}

if ( *ptr2 == '\0' )
{
   /* did not find '=' print error message and stop */
}

/* below is what you are interested in */

strcpy( tempstr, data );

ptr1 = strchr( tempstr, '=' );
*ptr1 = '\0';    /* turn = into null */
strcpy( before, tempstr );
printf("everything before = character is %s\n", tempstr );   /* watch out if = is first character, nothing before it */






strcpy( tempstr, data );

ptr2 = strchr( tempstr, '=' );
ptr2++;
if ( *ptr2 != '\0' )    /* = might have been last character */
{
   strcpy( after, tempstr );
    printf("everything after = character is %s\n", tempstr );
}

所以对于第一次strchr电话, 在[]将拥有&#34; recipName&#34; 并且在[]将有&#34;叉子朋友=杯子sonName = Spork特征=头发sendName = Spoon&#34;

你可以做一个     sscanf(之后,&#34;%s&#34;,after2); 只是&#34; Fork&#34;进入after2 []数组,假设总会有一个空格字符分隔。

答案 1 :(得分:0)

for chqrlie

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

    # define MAX 5

    int Count_Token ( char str[], char token )
    {
        char *p;
        int cnt = 0;

        /* str must be null terminated with '\0' otherwise infinite loop will happen */

        p = str;
        while ( *p != '\0' )
        {
            if ( *p == token )
                cnt++;

            p++;
        }

        return cnt;
    }


    void Get_KeyValue_at_Position( char str[], char token, int position, char k[], char v[] )
    {
        char *ptr;
        char temp_str[100];
        int i;

        ptr = str;

        /* assumes str[] passed in does not have leading space before first key */

        /* this for loop puts ptr on start of key at position */
        for ( i = 0; i < position; i++ }
        {
            /* move ptr to next space */
            while (( *ptr != ' ' ) && ( *ptr != '\0' ))
                ptr++;

            /* account for more than one space separating key/value pairs and put ptr on beginning of next key */
            while ( *ptr == ' ' )
                ptr++;
        }

        /* get key */
        strcpy( temp_str, ptr )
        ptr_token = strchr( temp_str, token );
        *ptr_token = '\0';
        strcpy( k, tempstr );

        /* get value */
        strcpy( temp_str, ptr );
        ptr_token = strchr( temp_str, token );
        ptr_token++;
        strcpy( v, ptr_token );
    }


int main ( int argc, char * argv[] ) 
{
    char data[100];
    char key[MAX][100];     /* has string before token */
    char value[MAX][100];   /* has string afte token */

    FILE *fdata;

    fdata = fopen(argv[1], "r"); //read data.txt file

    if ( fdata == NULL )
    {
        printf("could not read file %s\n", argv[1] );
        exit( 0 );
    }

    fgets( data, 100, fdata );
    while ( ! feof( fdata ) )
    {
        printf("data: %s", data);
        //this is where i get stuck, idk how to utilize this loop to copy the variable and variable names from the data.txt file i was given...thanks for the help

        /* count how many token characters there are in data[] string */
        num_token = Count_Token( data, '=' );

        if ( num_token > MAX )
        {
            printf("   num token is %d, MAX is %d, stopping program.\n", num_token, MAX );
            fclose( fdata );
            exit( 0 );
        }

        for ( i = 0; i < num_token; i++ )
        {
            /* make sure i index does not exceend declaration size of key and value */
            Get_KeyValue_at_Position( data, token, i, key[i], value[i] );
        }

        fgets( data, 100, fdata );
    }

    fclose(fdata);

    return 0;
}