Hey Guys,
I want to read from a file. And store the string in a string class array. Number of strings in the file is unknown. How could this be done?

int n=0;
	while(!inFile2.eof())
	{
		string tempStr = getWord(inFile2,n);
		if(tempStr.length() >=1)
		{
			wordCount++;
		}
	}
	inFile2.close();

	inFile2.open(inFileName2.c_str());
	string wordStr[wordCount];
	n=0;
	while(!inFile2.eof())
	{
		string tempStr = getWord(inFile,0);
		if(tempStr.length() >=1)
		{
			omitWordStr[n] = tempStr;
		}
		n++;
	}

bbcode

i am getting an error that expected constant expression.
Is there an easier way other than this logic? By doing it this way, I find my self getting string twice.

Thanks,

Recommended Answers

All 12 Replies

Hey Guys,
I want to read from a file. And store the string in a string class array. Number of strings in the file is unknown. How could this be done?

int n=0;
	while(!inFile2.eof())
	{
		string tempStr = getWord(inFile2,n);
		if(tempStr.length() >=1)
		{
			wordCount++;
		}
	}
	inFile2.close();

	inFile2.open(inFileName2.c_str());
	string wordStr[wordCount];
	n=0;
	while(!inFile2.eof())
	{
		string tempStr = getWord(inFile,0);
		if(tempStr.length() >=1)
		{
			omitWordStr[n] = tempStr;
		}
		n++;
	}

bbcode

i am getting an error that expected constant expression.
Is there an easier way other than this logic? By doing it this way, I find my self getting string twice.

Thanks,

You probably need to clear the inFile2 flags with the clear command between lines 10 and 12 with the clear command:
http://www.cplusplus.com/reference/iostream/ios/clear.html

I'm not sure whether line 13 works. You may have to declare this array dynamically with the "new" command since wordCount isn't known at compile time.

Regarding getting the strings twice, which strings are you getting twice? If it is only the last string in the input file, you may be going through the loop one too many times due to the eof () function possibly not failing quickly enough. What line are you getting the error in?

If you use the vector class you don't have to know how many words there ahead of time. Just read the file once and add each word to the vector -- it will expand as necessary to contains all the strings

#include <vector>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    vector<string> theList;
    string word;
    ifstream infile("filename.txt");
   while( infile >> word)
       theList.push_back(word);
   // after the above loop finishes the vector will
   // contain all the words.
}

Ok. What I meant by getting the string twice is that, I am getting all the words 1st time just to know how many words are there. Then, 2nd time I am doing same thing but now I am actually storing them in an array.

If you use the vector class you don't have to know how many words there ahead of time. Just read the file once and add each word to the vector -- it will expand as necessary to contains all the strings

#include <vector>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    vector<string> theList;
    string word;
    ifstream infile("filename.txt");
   while( infile >> word)
       theList.push_back(word);
   // after the above loop finishes the vector will
   // contain all the words.
}

I haven't used vector before, but that looks easy using vectors. How would I compare say, "Alkesh" in the vector "theList".

Yes, vectors are very easy to use. You would loop through the vector just like you would loop through any other normal array

for(int i = 0; i < theList.size(); i++)
{
    if( theList[i] == "Alkesh" )
    {
       cout << "Found at array element number " << i << "\n";
    }
}

Tutorial here

thanks a lot man. it works now.

Just one more thing,
What are the not printable characters for C++?
One that I found is —. That is not minus(-).

If you look at the standard ascii chart you will see that it contains 256 entries, some are printable but most are not. This ascii chart only contains characters in the English language. Other languages will use different charts to accommodate the language that is installed on the computer.

Is there a way to check if we've got one of the extended ASCII codes in C++? Because, I am reading from a file that contains '—', and because of that I get a "Debug assertion failed: (unsigned)(c+1) <= 256, file isctype.c". If i remove '—', the error disappears.

Did you write the file? Can you get the desired char to appear in you C++ program? If you can get the desired char to appear in you program try outputting the char to the screen but cast it to an int in the process and see what the value is? Or try storing a number like 257 in a char value and then print it to the scree to see if it's valid?

If you want to check for printable characters you can use the isprint() macro in ctype.h. What is the decimal value of the '—' you are talking about.

If you want to check for printable characters you can use the isprint() macro in ctype.h. What is the decimal value of the '—' you are talking about.

char ch = '—';

	cout << static_cast<int>(ch);

The output is -105.

I tried isprint(), but it still gives an error.

try using unsigned char

int main()
{
unsigned char ch = '—';
cout << static_cast<int>(ch) << "\n";
if( isprint(ch))
    cout << "TRUE\n";
else cout << "FALSE\n";
}

output is 151
FALSE

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.