-1

I currently have two .txt files, one files which contains characters, i wish to create a file stream and grab the characters from that file and put them into an array of characters.

I then also have a text file with 6 strings on a different line, i need to add these into an array of strings here is my code so far, i am having a slight error when i am trying to put the value of "words" my string file for the string array into each position in an array.

    //Gets the characters from the textfile and creates an array of characters.

char ch;
fstream fin("text1.txt", fstream::in);

while (fin >> noskipws >> ch) {
    char letters[5];
    cout << ch;


    for (int i = 0; i < 14; ++i)
    {
        ch >> letters[i];
    }


}
fin.close();


//Get the words from the file and add into an array of strings

string words;
fstream fin2("search1.txt", fstream::in);

while (getline(fin2, words)) {
    string wordsArray[6];
    cout << words;

    for (int i = 0; i < 6; ++i)
    {
        words >> wordsArray[i];
    }
}
fin2.close();
crashmstr
  • 28,043
  • 9
  • 61
  • 79
jbob
  • 1
  • 3
  • I see at least 2 bugs in this code (possibly 3, depends how you use `letters` in your real code - you don't use it at all here). Please show the exact error you are seeing (ie paste the error message). – BoBTFish Mar 07 '16 at 16:06
  • Also, please reconsider your use of the bad practice [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191). – BoBTFish Mar 07 '16 at 16:07
  • 3
    `ch >> letters[i];` What do you think this is doing? – n. m. could be an AI Mar 07 '16 at 16:12
  • 2
    In fact, the more I look, the more I realise this code is nothing like compilable. Please create an [MCVE](http://stackoverflow.com/help/mcve) and include the exact error message you get from that in your question. – BoBTFish Mar 07 '16 at 16:14
  • that magic 14 in the condition of the first for loop is a weird typo. If only it were 1+4... Are you aware that your arrays are locals? How do you use them in your real code? – Bob__ Mar 07 '16 at 16:49
  • Neither part of your code works or even compiles. Look at those compiler errors and start fixing them one by one. If you *don't* understand how to fix it, post the minimum code to display that error, identify the lines that error occurs on, and the *full text* of the error message. – crashmstr Mar 07 '16 at 16:57
  • updated the code, both arrays now store the desired values from each .txt file, thanks for feedback – jbob Mar 07 '16 at 17:25
  • @Pickles552 oh my... do you really put that much whitespace in your files? Even if you do, *don't* post code like that here as it makes it much harder to see the relevant code. Also: don't update questions with answers or solutions. That is what answers are for. – crashmstr Mar 07 '16 at 17:58

2 Answers2

4

According to your title, you can read text line by line using:

std::string text_line;
while (std::getline(input_stream, text_line))
{
    // ...
}

To read words from a string, you could use std::istringstream:

std::string word;
std::istringstream text_stream(text_line);
while (text_stream >> word)
{
    // Process the word
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Okay so here i have the code, taking a line of characters from a text file and putting them into an array of char[]

    char ch;
char chArray[14];
fstream fin1("text1.txt", fstream::in);
if (fin1.is_open())
{
    cout << "File of characters Opened successfully!!!. Reading data from file into array" << endl;
    while (!fin1.eof()) {
        for (int i = 0; i < 14; ++i) {
            fin1.get(ch);
            chArray[i] = ch;
            cout << chArray[i] << endl;
        }
    }
}

Now what i need to do is somehow, convert the array of characters into single character strings, so that i can compare them with an array of strings including words.

Is there a way to simply convert that array into a string array?

jbob
  • 1
  • 3