We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


String array

learn asked
Medium Priority
Last Modified: 2010-04-02

Can you show me the code for reading the whole lines from a text file and put them into a string array?

I found difficult to write such code while in other language it is quite simple.....espesially the length of the string elements in the array. Some lines could be very long :-)

Thank you in ad vance.
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)


Hi Thank you.

However, I learn C++ not from C :-(
Can you give me pure C++ code using fstream.h?
Moreover, in other language you don't need assume how many characters in a string variable :-)


You can use the STL string class.  The string will resize to store the number of characters that need to be stored.

One way you can do it is like this:

char *buf;
char bbuf[80];

/*  do your open of files here */




Now if you have lines that could be of various lengths then you will want to read in a char at a time until you find the end-of-line char.  

like this :

The problem still is that you have a limit on how long the line can be in the array.
However, it may be your best interest to just make a limit.

Hope this helps


To nietod:

I know C++ not much :-)
I would like very much to write a class, myString, so that I can declare something like
myString strArray[1000]
and the element in strArray can automatically resize to store the hole line characters.
So, can you tell me how to do that :-)
BTW I don't think I have got STL string class :-(



To kellyjj:

Thank you very much for your comment.
I think that is still a C style code which I feel difficult to understand :-(
I prefer something like:

#include "c:\myclass\mystring.h"
#include <fstream.h>
void main()
{mystring strArray[1000];
 for (int i=1; i<1001; i++)

Writing a string class is pretty easy and a good way to learn C++.  Writting a GOOD string class is harder however that I why I recomend using  the STL string class.  If you have a copy of the STL you have the string class.  It has been around for years now and is part of the C++ standard.  Unless your C++ compiler is REALLY old, you should have a copy.

String classes are the way to handle strings.  Once you start using them you will never need or want a char * string again.

Note that the STL string class is defined in the <string> (not <string.h>) include file.


To nietod:

Thank you very much for your help.

Do you mean I can use "<<" after including STL something like:

     #include <string>
     #include <fstream.h>
     void main()
     {string strArray[1000];
      for (int i=1; i<1001; i++)

Unfortunately the STL string does not provide << and >> operators (at least to my knowledge--I don't use it.)  But you can read a character from the file and attach it the end of the string that represents the current line with +=.  Thus for each line, you will read one character and add it to the end of the string representing the current line and repeat this until you reach the end of the line.

This way you do not have to have a maximum line length like you would using kelly or schhucka's answers.  You would build a string that is as long as the line you read in.  Lines that are short, will be stored in strings that are short.  Lines that are long will be stored in strings that are long.  

You have the same problem with the number of lines.  In one of the examples above they suggested using an array for storing1000 lines.  What if the files is 1001 lines long?  What if it is only 3, that would be a waste.  but you could use one of the STL container classes to store the lines.  These classes can expand to store the data you need at run-time.  The vector class would probably be best.


To  nietod:

You have show me something in STL :-)
I will check if I have it or not....perhaps not :-(

We can read floats from a file:
float f1;
If we declare a string, we should able to do (or to write a class to do):
string s1;
fileHandle>>s1;  //;-)


You can read floats from a steam (file stream or other streams)  with >> because the stream class defines >> operators to handle the "built-in types" (int, char, double, float, etc.)  They do not define >> operators for complex types like classes.  The person who creates the complex type is responsible for creating a >> operator to handle input for the class.  The STL string class could have an >> operator for use with a stream and then you would be able to read strings from a stream.  However that was not done.  Probably because there is not one right way to read a string from a stream.  Some people would expect it to read a single line.  Some would expect it to read a single word.  Some would expect it to read the whole stream.  Thus they left it up to the programmer to decide.
The STL string class does have insertion and extraction operators.  It reads in one word at a time.  If you have Borland 5, Visual 5 or the latest g++, you will have the STL built in.

you have to do the following though to make it look natural:

#include <string>
using namespace std;

then you can do:

int main() {
   ifstream fin("input.txt");
   string str;
   while (fin >> str) {
      cout << str;
   return 0;

side note -- in c++ the main function is defined to be int main, not void main, and it matters.

VEngineer,  thanks for  the tip on << and >> for strings.  For some reason my docs don't list them.  

>> in c++ the main function is defined to be int main, not void main, and it matters.
While it is a good idea, in most cases it really doesn't matter, that's why people get away with declaring it wrong.  


To  VEngineer and nietod:

Thank you for help on STL. i have checked my C++. There is no STL for me :-(
However, I mahaged to write my string class and it is working.... though looks not very good ;-) I will ask later to improve that.

Now, how about the points? who should get them?

What C++ compiler are you using?  The STL has been around for a long time.  It is possible to get one the public domain copies of STL, but you may have to make some changes to get it to compile.  That could be a pain.  Writing your own string class may be the way to go--If you can make it work.


To nietod:

Good night :-)

i spent one day to write my string class only for get the characters one by one and add to a string which keeps increasing its length by use "new" :-)

I have submited a bad piece of my string class to find help :-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.