New to C, trying to better understand char arrays, pointers, reading in files, etc -
alright, working on linux , emacs first time using c, , coming pretty beginner level java programming in eclipse, new cs course daunting. stuff has been thrown @ me if knew it...
anyway, current part of assignment working on involves reading in text file (doing piping text file standard input program). had 3 functions, main function read in file / call other functions, function reverses order of single word (apple becomes elppa) *char beg , *char end parameters, , function reverses order of every word in line of words, calling previous function , taking char *str parameter.
i having trouble reading in files in main method in way makes easy utilize these functions. here's snippet of how reading them in currently, can see haven't figured out way store line of words , send function (i need reverse line line, can't add every single char 1 long array)
enter code here`` char line[8192] int location = 0; file *in = stdin; int buff = 0; while (buff = fgetc(in)) { fprintf(stderr, "character is: %d '%c' \n", buff, (char)buff); if (buff == eof) { break; } line[location] = (char)buff; location++; } line[location] = '\0';
if want whole line, can this:
char line[max_line_size]; fscanf(in, "%[^\n]", line); //do whatever need line fscanf(in, "%[\n]", line);
the first fscanf call reads whole line , store in variable line. doesn't skip line! if use again, store same line.
the second fscanf call this: stores '\n' in variable line , skips line read previously.
if want, can create buffer '\n' , skip line:
char garbage[2]; fscanf(in, "%[\n]", garbage);
i hope helps.
Comments
Post a Comment