c - How to get word before and after the current word in a file? -
i have file, "data.txt" consists of following: http://pastebin.com/fy9ztqx6
i'm trying word before , after "<". old word being 1 on left , new word being 1 on right. have far:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* name: marcus lorenzana assignment: final */ //binary tree struct hold left , right node //as word , number of occurrences typedef struct node { char *word; int count; struct node *left; struct node *right; } node; //,.?!:;- int punctuation[7]; void insert(node ** dictionary, node * entry); char* readfile(char* filename); void printdictionary(node * tree); void tolower(char** word); void getreplacementwords(char *filecontents, char **newword, char **oldword) ; int main() { char *word; char* filecontents = readfile("data.txt"); char* oldword; char* newword; //create dictionary node node *dictionary; node *entry; //read words , punctuation in text file word = strtok (filecontents, " \n"); dictionary = null; while (word != null) { //word = strlwr(word); entry = (node *) malloc(sizeof(node)); entry->left = entry->right = null; entry->word = malloc(sizeof(char)*(strlen(word)+1)); entry->word = word; insert(&dictionary,entry); word = strtok (null, " \n"); } //printdictionary(dictionary); filecontents = readfile("data.txt"); getreplacementwords(filecontents,&newword,&oldword); return 0; } void insert(node ** dictionary, node * entry) { if(!(*dictionary)) { *dictionary = entry; entry->count=1; return; } int result = strcmp(entry->word,(*dictionary)->word); if(result<0){ insert(&(*dictionary)->left, entry); entry->count++; } else if(result>0){ insert(&(*dictionary)->right, entry); entry->count++; } else { entry->count++; } } //put file contents in string strtok char* readfile(char* filename) { file* file = fopen(filename,"r"); if(file == null) { return null; } fseek(file, 0, seek_end); long int size = ftell(file); rewind(file); char* content = calloc(size + 1, 1); fread(content,1,size,file); return content; } void printdictionary(node * dictionary) { if(dictionary->left) { printdictionary(dictionary->left); } printf("%s\n",dictionary->word); if(dictionary->right) { printdictionary(dictionary->right); } } void getreplacementwords(char *filecontents, char **newword, char **oldword) { char *word; word = strtok (filecontents, " \n"); while (word != null) { printf("\n%s",word); int result = strcmp(word,"<"); if (result == 0) { printf("\nfound replacement identifier"); } word = strtok (null, " \n"); } }
you can use fscanf(filename , "%s < %s" , firststringcontainer , secondstringcontainer)
after using fseek
line containing <
character string before < stored in firststringcontaine
r , 1 after in secondstringcontainer
here's code recommand use :
int found = 0; char buffer[chooseyoursize]; char firststringcontainer[chooseyoursize] , secondstringcontainer[chooseyoursize]; while(fgets(buffer , sizeof(buffer) , filename) != null) { if(strchr(buffer , '<')) { found++; break; } } if(found) { fscanf(file , "%s < %s" , firststringcontainer , secondstringcontainer); }
of course works if lines targeted contains 3 elements string < string case here
Comments
Post a Comment