arrays - C...string splitting issue -


i having issues firstcheck() function. explain directly below current code. bare me c skills lackluster. have written program using knowledge of c++.

the firstcheck() function not working should. in readfile() function have split text given file array line line. firstcheck() should take array "mystring" , read first string until " " occurs (basically first word/character/etc.) can evaluate it.

i'm pretty sure issue part. program seems stop , wait input assume because of "stdin" better way implement snippet?

while(strcmp( fgets( item.mystring, sizeof item.mystring, stdin ), " " ) != 0 )
{ myword[s] = strdup(item.mylines[s]); }

should use scanf() instead? told using gets() bad practice, used fgets() instead

assem.c

#include "assem.h"  int readfile(file *file, struct assem *item)  {     size_t =0;     item->counter = 0;  //this sort of constructor, if will.      size_t maxlines = sizeof item->mylines / sizeof *item->mylines;  //breaks down text file line line      while(i < maxlines && fgets(item->mystring, sizeof item->mystring, file))  //and stores them "mylines array"     {         item->mylines[i] = strdup(item->mystring);         i++;         item->counter++;     }         return 0; }  void printfile(struct assem item)  //just printing things..prints new line in front {     printf("\n");     for(int s = 0; s < item.counter; s++)     {         printf("%s\n", item.mylines[s]);     }     printf("\n"); }  int firstcheck(struct assem item)                {     char *myword [7] = {null};      for(int s = 0; s < item.counter; s++)        {         while(strcmp( fgets( item.mystring, sizeof item.mystring, stdin ), " " ) != 0 )                  {             myword[s] = strdup(item.mylines[s]);         }                }      for(int s = 0; s < item.counter; s++)     {         printf("%s\n", myword[s]);     }      return 0;        } 

"assem.h"

#include <stdio.h> #include <stdlib.h> #include <string.h>  struct assem {     char mystring[101];  //buffer     char *mylines[20];  //this store lines text file..up 20 lines     int counter;  //counter number of lines     //printing stuff...prints file directly whats stored in array };  int readfile(file *filetoberead, struct assem *item);  //takes in file needs read , splits lines int firstcheck(struct assem item); void printfile(struct assem item); 

"main.c"

#include "assem.c"  int main() {     struct assem test;     file *mips;     mips = fopen("/home/rpf0024/cse2610/program1/mips.asm", "r");     readfile(mips, &test);     printfile(test);     firstcheck(test);     fclose(mips);     return 0; } 

answering "what better way implement snippet?" , not looking @ other code:

while(strcmp( fgets( item.mystring, sizeof item.mystring, stdin ), " " ) != 0 ) 

this cause strcmp() segfault @ end of file, because fget() return null.

also aware fgets() keep newline @ end of input. comparing input " " never match (except if last line of file has no newline).

it better this

while (null != fgets(item.mystring, sizeof item.mystring, stdin)) {     if (strcmp(item.mystring, " \n"  != 0) {        ...     } } 

but not sure work you, since want find space within string, not whole file line. in case, should research strtok().

char *tok; while (null != fgets(item.mystring, sizeof item.mystring, stdin)) {      tok = strtok(item.mystring, " \t\r\n");     while (tok) {         printf("%s\n", tok);         tok = strtok(null, " \t\r\n");     }  } 

this split string sections delimited space(s) or other whitespace. note item.mystring fragmented in process, tokenising.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -