Solved

String array

Posted on 1998-08-04
17
425 Views
Last Modified: 2010-04-02
Hi

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.
0
Comment
Question by:learn
17 Comments
 
LVL 2

Accepted Solution

by:
shchuka earned 100 total points
ID: 1169356
Can you just do something like this (this is even pure C now)?

FILE *fp = fopen("filename","r");
char *lines[1000];         /* say, maximum 1000 lines of text */
int res;

int i=0;
do {
    lines[i] = (char*)calloc(1000,sizeof(char));        /* say max of 1000 chars per line */
    res = fgets(lines[i],1000,fp);
    i++;
} while(res);
fclose(fp);

After you're out of the loop, i will contain the number of strings read.
0
 

Author Comment

by:learn
ID: 1169357
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 :-)

Cheers.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169358
You can use the STL string class.  The string will resize to store the number of characters that need to be stored.
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1169359
One way you can do it is like this:

char *buf;
char bbuf[80];

/*  do your open of files here */

fread(buf,1,80,fhandle);
strcpy(bbuf,buf);

Or

fread(bbuf,1,80,fhandle);

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 :
fread(buf,1,1,fhandle);
strcat(bbuf,buf);

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
0
 

Author Comment

by:learn
ID: 1169360
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 :-(

Cheers.
0
 

Author Comment

by:learn
ID: 1169361
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++)
 {strArray[i]<<fhandle;
  ........
 }
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169362
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169363
Note that the STL string class is defined in the <string> (not <string.h>) include file.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:learn
ID: 1169364
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++)
      {strArray[i]<<fileHandle;
       ........
      }
     }
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169365
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.
0
 

Author Comment

by:learn
ID: 1169366
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;
fileHandle>>f1;
If we declare a string, we should able to do (or to write a class to do):
string s1;
fileHandle>>s1;  //;-)

Cheers
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169367
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.
0
 
LVL 2

Expert Comment

by:VEngineer
ID: 1169368
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.


0
 
LVL 22

Expert Comment

by:nietod
ID: 1169369
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.  
0
 

Author Comment

by:learn
ID: 1169370
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169371
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.
0
 

Author Comment

by:learn
ID: 1169372
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 :-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now