Reading in multiple lines of keywords as character arrays from file? In C -
Reading in multiple lines of keywords as character arrays from file? In C -
hi trying read in lines of text input file. file can contain multiple keywords. each keyword seperated end of line. can assume keywords wont longer 30 characters. far able readon 1 keyword , loop ends. how read multiple keywords until reach end of file. want store each keyword different character arrays. have far.
char readkeywords() { file *finn; char array2[30]; int = 0; finn = fopen("cp3keywords.txt", "r"); while (fscanf(finn, "%c", &array2[i]) != '\n') { i++; } }
ok, you've got several problems here.
the fscanf homecoming value number of items read, in case 1.
the loop seems intended end when read newline, not when reach end of file.
how this:
int getnextkeyword(file *fp, char *result) { int c; while (eof != (c = fgetc(fp))) { if (c == '\n') break; *result++ = c; } *result = 0; homecoming c; }
this consumes characters next newline or eof. accumulates result in input buffer provided. when nail newline or eof, write 0 in buffer, resulting in standard null-terminated string. returns lastly character written. write more code calls function in loop until returns eof.
please notice how variable c type int, not type char. that's because eof (the end of file value returned fgetc) not representable character. if write char c; c = fgets(fp); if (c == eof) ... never true. mutual c programming mistake.
so calling code might this:
file *fp = fopen("cp3keywords.txt", "r"); char array2[30]; while (eof != readkeyword(fp, array2)) { // want keyword }
c arrays character
Comments
Post a Comment