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;
}