-1

I try to write a code that can read a text file in a specific order. The Problem is that the strings overwrite the last string in the Array, but I want that the array is cleared for the next string, however I don't know how to do this.

Here is the text file:

@str_hello_world_test={hello world!test}
@str_hello_world={hello world}

And the output is here:

symbol:str_hello_world_test²`
string:hello world!testtest²`

hello world!testtest²`
symbol:str_hello_worldttest²`
string:hello worldorldttest²`

hello worldorldttest²`

And my Code:

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

#define MAXBUF 255

//prototypes
void readingFile(FILE* fp);
char* readingSymbol(FILE* fp);
char* readingString(FILE* fp);

int main(void)
{
    FILE* fp;
    char directory[MAXBUF];

    puts("Geben sie ein Verzeichnis ein: ");
    gets(directory);
    fp = fopen(directory, "r");

    readingFile(fp);

    system("pause");
    return EXIT_SUCCESS;

}

void readingFile(FILE* fp){
    int c;

    while((c = fgetc(fp)) != EOF){
        char* symbol = readingSymbol(fp);
        char* string = readingString(fp);
        printf("%s\n", symbol);
    }
    return;
}

char* readingSymbol(FILE* fp){
    int c;
    int i = 0;
    char symbol[MAXBUF];

    while((c = fgetc(fp)) != '='){
        if(c == '@'){
            continue;
        }
        else{
            symbol[i] = (char)c;
            i++;
        }
    }
    printf("symbol:%s\n", symbol);
    return symbol;
}

char* readingString(FILE* fp){
    int c;
    int i = 0;
    char str[MAXBUF];

    while((c = fgetc(fp)) != '}'){
        if(c == '='){
            continue;
        }
        else if(c == '{'){
            continue;
        }
        else{
            str[i] = (char)c;
            i++;
        }
    }
    printf("string:%s\n\n", str);
    return str;
}
DarkGengar
  • 13
  • 1
  • 5

1 Answers1

0

Your code has a glaring example of undefined behavior. You return dangling references. Turn your warning level as high as possible. Then heed those warnings. A rough (implementation dependent) rationalization for why this is happening, is that the two functions have exactly the same layout of stack frames. So when you call the second one, it fills exactly the same location with a another string.

An immediate fix is to avoid returning a pointer to a buffer, and pass a pointer to a buffer into the function. Then it is responsible for filling it:

char symbol[MAXBUF];
readingSymbol(fp, MAXBUF, symbol);

// ...

void readingSymbol(FILE* fp, size_t const buff_len, char symbol[buff_len]) {
    int c;
    int i = 0;

    while((c = fgetc(fp)) != '='){
        if(c == '@'){
            continue;
        }
        else{
            symbol[i] = (char)c;
            i++;
        }
    }
    printf("symbol:%s\n", symbol);
}

The above is valid C99. If you compile in C89 for some reason, or your compiler doesn't support variable length arrays, change the function signature to be as follows:

void readingSymbol(FILE* fp, size_t const buff_len, char *symbol)
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458