Avatar of TechMonster
TechMonster asked on

c++ get textfile data into Array

Hello,,Here is my problem..thnks for any help.
Have to store each line iof a text file into an array.

Textfile reads:
This is one line of data.
This is the next line of data.
I have this:
*******************************************************************************************
 const int Max = 100;
int index, lettercount;
  string LineArray[Max];  

while(!txtData.eof())
          {
                txtData.get(Chr);      
                cout<<Chr;  
                Chr = txtData.peek();  //<--this part does work fine,,no problems here.
                                                    //It is the below.....
                for (index = 0; index < lettercount; index++)
                    {
                    LineArray[index] = Chr;
                    cout << LineArray[index];
                    }
C++

Avatar of undefined
Last Comment
TechMonster

8/22/2022 - Mon
josgood

I think you want to do something like this...

int index = 0;
while not eof {  // you already have this
   txtData.get(Chr);
   if Chr isn't a newline ('\n') and index isn't at the maximum{
      LineArray[index] = Chr;
      index++;
   } else {
      null-terminate LineArray (put a zero at the end)
      cout << LineArray << std::endl
      index = 0
   }
}

This assumes you're opening the txtData stream in text mode.
josgood

I note that
   string LineArray[Max];  
allocates an array of std::string, rather than a single std::string pre-sized to Max.

I suggest something like
   string LineArray(Max,0);  
which sizes the string and initializes it to all zeroes.

Also, in the pseudo-code above, you'll need to end the function with
   if (index > 0) out << LineArray
The std::endl won't be needed because you're copying the newline '\n' from the text file.
ASKER
TechMonster

I am trying it now...thanks
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Infinity08

Why not use getline ?

        string line;
        while (getline(txtData, line)) {
            // do something with line, like push it back into a vector, or store in an array, or ...
        }

Reference page for getline :

        http://www.cplusplus.com/reference/string/getline.html
evilrix

Two working examples of how you can do this simply using streams. The first way overloads operator >> for a vector of strings and uses getline to populate it. The other way uses input interators and a overloaded operator >> for strings. Both were tested on Windows and Linux. Of the two I'd go for example 1 as it's probably the simplest to implement and maintain.


// Example 1
#include <fstream>
#include <iostream>
#include <vector>
#include <iterator>
#include <string>
 
typedef std::vector<std::string> strvec_t;
 
std::istream & operator >>(std::istream & is, strvec_t & sv)
{
	std::string s;
	while(std::getline(is, s)) { sv.push_back(s); }
	return is;
}
 
int main()
{
	std::ifstream ifs("data.txt");
	strvec_t sv;
	ifs >> sv;
 
	std::ostream_iterator<std::string> osi(std::cout, "\n");
	std::copy(sv.begin(), sv.end(), osi);
}
 
 
// Example 2
#include <fstream>
#include <iostream>
#include <vector>
#include <iterator>
#include <string>
 
namespace std {
	std::istream & operator >>(std::istream & is, std::string & s)
	{
		std::getline(is, s);
		return is;
	}
}
 
typedef std::vector<std::string> strvec_t;
 
int main()
{
	std::ifstream ifs("data.txt");
	std::istream_iterator<std::string> isi(ifs);
	
	strvec_t sv;
	std::copy(isi, std::istream_iterator<std::string>(), std::back_inserter(sv));
 
	std::ostream_iterator<std::string> osi(std::cout, "\n");
	std::copy(sv.begin(), sv.end(), osi);
}

Open in new window

ASKER
TechMonster

I am having some trouble trying to get it to work properly.
I was able t store the whole text file into the array and display it forwards
but I have to figure out how to display it backwards...Is this a second question??

//Get Textfile data
   ifstream txtData;
   txtData.open("TestFile.txt");
     
   
  //Store Letters into Array
      const int Max = 500;;
      char Chr;
      char LineArray[Max];
      int index, ChrCnt, VowelCnt;
          index = 0;
          ChrCnt = 0;
          VowelCnt = 0;
          int LnCnt = 0;
         
    while(!txtData.eof())
    {
            txtData.get(Chr);
            LineArray[index] = Chr;
            index = index + 1;
            ChrCnt = ChrCnt + 1;
           
            if (Chr = '\n')
            LnCnt = LnCnt +1;
         
    }        
       
        for(index = 0; index <= ChrCnt; index ++)
                   {
                     if (LineArray[index] != '\n')
                     {                                
                     cout<<LineArray[index];     // this displays the index forwards..works great.
                          if (LineArray[index] = ('A' || 'a' || 'E' ||'e'||     // this part doesn't count vowel..
                             'I' || 'i' || 'O' || 'o'||'U' || 'u'))
                             {
                             VowelCnt = VowelCnt + 1;
                             }
                         
                     }
                     else
                     {
                      cout<<endl;
                       cout<< // I believe the array would go here to display it backwards..
                       
                      {
                      cout<<VowelCnt;
                     
                      }
                      ChrCnt = 0;
                      system("PAUSE");
                                                       
                    }
                       
                      }
                                 
     
         
         
           
       
    //Close File
    txtData.close();
     
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
josgood

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
TechMonster

I prefer the single dimensional array..
ASKER
TechMonster

Ok,,
I have some challenges still but I think I can get it from here,
Thanks everyone.