38208

c, Import text file with different lines and handle the lines

Question:

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!

Answer1:

To find if the word "damage" is contained within a line:

if (strstr(line, "damage") != NULL) { /* "damage" found */ }

Answer2:

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

Recommend

  • Differences between two regular expressions
  • Perl critic error on splitting string into array using regexp
  • Signing a json document or string with x509 certificate
  • java.lang.NoClassDefFoundError: javax.activation.DataHandler in android
  • C++ iomanip Alignment
  • Using TRY / CATCH to perform INSERT / UPDATE
  • Laravel 4: fetching only models that have related models
  • DateTime.TryParse different results
  • Samsung Note layout issues
  • System::Windows::Forms::Form::ShowDialog()
  • How to make a shared resource thread-safe when using dependency injection?
  • Obtaining GUID for Existing USB Device on Windows XP
  • Yii2: How to show checked values in CheckboxList
  • cast of generic type fails
  • How to create command menu item with checkbox?
  • Expand header row into multiple child rows
  • TabBarController: Orienting views in different orientations
  • this class is not key value coding-compliant for the key PerformRotate
  • Get all DNS records associated with an IP
  • How to mount dynamically single file component in Vue.js
  • Entity framework 'include' including more than it should
  • Bullet-Proof ACL using AngularJS
  • parallelize process in missForest package
  • Moving data between processes in Spartan 3
  • Access Google Maps from outside
  • Getting IIS6 to play nice with WordPress Pretty Permalinks
  • Titanium doesn't recognize Android SDK on Windows
  • How to use tag-it
  • ckeditor and jquery UI dialog not working
  • Most efficient way to move table rows from one table to another
  • jQuery: add elements until a particular height is reached
  • How does document.ready work with angular element directives?
  • Reduction and collapse clauses in OMP have some confusing points
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Lost migrations and Azure database is now out of sync
  • Projection media query: browser support and workarounds?
  • Google cloud sdk not working when python points python3
  • R: gsub and capture