0

I am reading from a file which has words like:

"connecting", "classes", "feeds"..

I need to convert each character to lowercase and then call a function to remove suffix from each word. Say, first on connecting, then on classes...

I am done with rest of the part but have a problem reading the file and storing words in array.

I will have a minimum of 50 such words in the file. What is the best way to store it?

{
    int val=0; char fin_char;    
    string line;string arr[100];    
    ifstream myfile("testfile.txt"); 
    if (myfile.is_open())
    {
        while(myfile.good())
        {
            getline(myfile,line); 
            arr[i]=line; 
            i++;
        }   
        myfile.close(); 
        for (int j=0;j<i;j++)
        {
            while (arr[j][k]!='\0')
            {
                c=arr[j][k];
                cout<<"C"<<c<<" "<<"J:"<<" "<<j<<"K:"<<k<<"\n";
                val=int(c);
                if (val>=65&&val<=90){ val=val+32;fin_char=static_cast<char>(val);arr[j][k]=fin_char;}
                k++;
            }
        }   
        for (int j=0;j<i-1;j++)
        {
            cout<<" "<<arr[j]<<"\n";
        }   
        system("pause");
        return 0;
    }

This is the output I get:

 C99 J:0 K:0 C111 J:0 K:1 C110 J:0 K:2 C110 J:0 K:3
Caesar
  • 8,395
  • 6
  • 34
  • 62
  • 1
    Using `good()` or `eof` is not the right way to parse a stream, it'll be flagged only after the error has occured, instead use `operator>>`. http://stackoverflow.com/a/5605159/183120 – legends2k Jun 13 '13 at 18:25
  • As @legends2k mentioned correctly. Otherwise, using a string arr[] is fine to store such strings. – fkl Jun 13 '13 at 18:27
  • I don't see `k` declared/initialized anywhere. You probably need to re-initialize it on every iteration of your `for (int j ...)` loop... – twalberg Jun 13 '13 at 19:07

2 Answers2

1

If you want a random access container (like array) without declaring it's size, use vector from STL.

Bartosz Marcinkowski
  • 6,127
  • 1
  • 32
  • 59
  • The way I am reading it, draws a line from the file. Is there any way I could possibly add it char by char in a char array and then perform the necessary operations? Necessary operations refers to removing suffix part, I need the word in form of a char array. say "classes" as arr[0]="c", arr[1]="l", arr[2]="a",.... to remove the suffix -es from classes. – user2480956 Jun 14 '13 at 05:59
0

How are you storing the words in your text file ? Because unless each word is in a new line, your arr array would have only one element with a very long string.

Also as Bartosz-Marcinkowski suggested, use a vector. You'll still be able to access each character, just use it as a 2D array.

If you need all the words in one line in the text file then i suggest splitting the line up based on spaces, you can use this function if you'd like :

#include "sstream"
#include "vector"
#include "string"

vector<string> split(const string str)
{
 stringstream ss(str);
 string buf;
 vector<string> elems;

 while(ss >> buf)
     elems.push_back(buf);

 return elems;
}

which returns a vector of all the words in one line. So, you can use it like this :

int main()
{ 
 string line,
 vector<string> arr;
 ifstream myfile("testfile.txt");

 if(myfile.is_open())
     getline(myfile,line);


 arr = split(line);
}

now if you wanted to access the 3rd character in the 5th word you would just need to arr[4][2]

Krishna_Sp
  • 101
  • 5