问题
im trying to make a little program that reads a chat log and calculates some numbers. Thing is im not sure what command to use since the lines arent identical. Heres a few lines from the log
[22:56:37] Your strike was absorbed by a magical barrier!
[22:56:37] You miss!
[22:56:37] You attack Tylaia with your bright arcanium weighted bearded axe and hit for 70 (-41) damage!
[22:56:37] You critical hit Tylaia for an additional 19 damage!
[22:56:37] You hit Tylaia for 66 (-21) damage!
[22:56:37] You hit Tylaia for 17 extra damage!
[22:56:37] Tniatha hits your hand with her bright duskwood exceptional buckler for 72 damage!
[22:56:37] Tniatha critical hits you for an additionnal 32 damage!
[22:56:37] Tniatha does 8 extra damage to you!
[22:56:37] Tniatha hits you for 8 damage !
[22:56:37] You are enveloped in a cloud of dirt!
How can I import only the lines where the "damage" is done?
This is what i have so far;
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct{
char your_self[15], char who_ever_else[15];
int damage_done, damage_taken, healing_done, healing_taken;
} input;
int main(void){
input *inputArray = malloc(1);
read_log_file(&inputArray);
return 0;
}
void read_log_file(input **inputArray, int *lineCount){
char your_self[15], char who_ever_else[15];
int damage_done, damage_taken, healing_done, healing_taken;
FILE *inputFile;
inputFile=fopen("chat.log", "r");
if(inputFile = NULL){
printf("File cant open");
exit(1);
}
}
Was thinking to do something like this;p
(fscanf(inputFile, ".............."...
But then since every line is not identical ill get the wrong values into my variables?
Thank you!
回答1:
To find if the word "damage" is contained within a line:
if (strstr(line, "damage") != NULL) {
/* "damage" found */
}
回答2:
I recently wrote a similar code. Guess this will help
char *match;
/* Read line by line - fgets reads only till a newline/ EOF - u can refer help*/
while (fgets(buffer, sizeof(buffer), fp))
{
/* Search for pattern */
match = strstr(buffer,"damage");
if (match != NULL)
{
//Do ur stuff
}
}
来源:https://stackoverflow.com/questions/15246437/c-import-text-file-with-different-lines-and-handle-the-lines