![]() ![]() ![]() Look things over and let me know if you have further questions. Passing the file to read as the first argument: $. ( note: it is a good idea to initialize a 2D array used to hold strings all zero)Įxample Input file $ cat dat/captnjack.txt return 0 /* what? you exit at end of iteration */įree (line) /* no neef for if, calling free (NULL) doesn't hurt */ StreamWriter file new System.IO.StreamWriter (speichern) ausgabeListe. N = getWords (line, arr) /* pass line, str not needed */įor (i = 0 i < n i++) /* i < n, not i <= n */ It's this line which writes the ToString representation of the List, resulting into the text line you got: StreamWriter file new System.IO.StreamWriter (speichern) file.WriteLine (ausgabeListe) file.Close () Instead you want to write each line. While ((read = getline (&line, &len, fp)) != -1) /* initialize arrays */ Int n = 0, i, j = 0, inword = 0 /* inword is flag for in/out of word */įor (i = 0 n 1 ? fopen (argv, "r") : stdin Int getWords (char *base, char (*target)) #define ROWS 10 /* if you need a constant, #define one (or more) */ #include /* use issspace() to check whitespace */ Pass line to getwords() and then fix the loop limits in main() to display the words and remove the unneeded return 0 : n = getWords (line, arr) /* pass line, str not needed */ fp = fopen ("dyrekt.html", "r") /* NEVER hardcode filenames */ you invoke Undefined Behavior reading beyond the end of your array, your loop limits should be 0 1 ? fopen (argv, "r") : stdin.you do not need char str to begin with, simply pass line to getWords().Instead of sprinkling Magic Numbers in your code ( 10, 20), #define a constant or use a global enum Pass the filename as the first argument to your program (that's what argc and argv are for in int main (int argc, char **argv)), or prompt the user and take the filename as input. You should not have to re-compile your program just to read from a different filename. never hardcode filenames or use MagicNumbers in your code.You have a large number of issues to correct. Alternatively, you could statically allocate an array and use strcpy or, even safer, strncpy. Changing to a char* fixes this since that's a pointer. In your case, you were trying to set your char to something of variable length, and C doesn't allow that. This is valid because the literal string "test string" is of a static length. So, for example, the following code is valid in C: char str = "test string" As a result, C doesn't let you initialize it to be something of variable length. An array, however, is somewhat different. This pointer could point to something of (theoretically) any length. ![]() Well, the copied string variable (in my case test, in your case line) is a pointer in memory. When I run it, I get the following output: this is a test stringīut I'm sure you're wondering why that happens. When I compile the following code: #include I made a simple test program to test that element of your code. Comment hinted at this, but I'll expand here. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |