How can I read in a line of text, one by one, and store in an array?

Hi, I'm a major C noob but trying real hard to understand what's going on. I would simply like to read in, line by line, from a text file and have each line stored in an array. In other code, like VB, I would simply do something like:

String myArrayOfLines(200);
i=0;
For each line in myTextFile
myArrayOfLines(i) = aLinefromMyTextFile;
i++;
Next Line

But I realize this isn't possible with quite the ease. I've spent days trying to get something to work with multidimensional arrays of char type and having no luck.

I could really use some direction on how to store each line of text from a text file into an array, and then be able to print them all out, line by line.

Please read the "Before you make a query" thread before you post again. To post without doing so (or while ignoring its recommendations) is rude.

C does not have a string type such as you show (C++ does). A C string IS an array. It's an array of char, terminated by a zero ('\0'). Consequently, an array of strings is a 2D array of char. For an actual 2D array (fully contiguous) one would need to make the 'rows' at least one char longer than the longest string to be read, or else some string(s) will be truncated. One can get around that by reading a line, getting storage of the correct size for the string from malloc (which returns a pointer), transferring the temp string to that area, and storing those pointers in an array.

Use 'fgets()' to read the line into temporary storage. You should attempt to make this storage longer than the longest anticipated line. fgets allows you to specify the maximum length to be read, so you may truncate a line if you have guessed incorrectly, but you won't generate a buffer overflow.

To print them out, merely use printf with a string format.

You would find this much more familiar if you were using C++ and its string type. I perceive that you have not really studied C for spit. The basic "Hello World" program would have taught you how to print a line. I would suggest a book or tutorials. There are good tutorials referenced on the forum. Forums are better as help than as a primary learning entity.

Just try using getline() function. this function will read string untill it encounters a '\n' or termination count provided in function itself. This is quite simple. Better read a good C programming. If you get to read, must fo through K&R or Gottfried. If you a mewbie in C programming better read Yashwant kanetkar.Its ideal for new comers....