简体   繁体   中英

Reading from an input file one word at a time in c++

I am trying to read a set of words from an input file into 3 different string arrays. The words in the file are separated by a '#'. My code for some reason is running twice and the first array is blank, and words are . Please help, my loop is definitely wrong and i must be overlooking something. Please let me know what i am doing wrong. Thanks

 Sample input file (input.txt)

complicated
insinuated
complex
juggernaut
#
blah 
...
...
#
...



#include <iostream>
#include <fstream>
#include <string>
#include "conio.h"

using namespace std;

int main() {

ifstream inFile("dictionary.txt");

// Check for error
if (inFile.fail()){
    cout << "Error Opening File." << endl;
    exit(1);
}

string hard[27], medium[29], easy[33];
string getHardWord, getMedWord, getEasyWord; 
int hardCount = 0, medCount = 0, easyCount = 0, delimitCount; // n is number of #
  // delimiters and count is array position

// Read the dictionary file until the end of file
while (inFile){
    inFile >> getHardWord;

    while ((getHardWord != "#") && (delimitCount = 0)){
        hard[hardCount] = getHardWord;
        hardCount++;
        inFile >> getHardWord;
    }

    delimitCount++;
    cout << delimitCount << endl;

    for (int iii = 0; iii < 27; iii++){
        cout << hard[iii] << endl;
    }

    cout << endl;

    inFile >> getMedWord;

    while ((getMedWord != "#") && (delimitCount = 1)){
        medium[medCount] = getMedWord;
        medCount++;
        inFile >> getMedWord;
    }

    delimitCount++;
    cout << delimitCount << endl;

    for (int jjj = 0; jjj < 27; jjj++){
        cout << medium[jjj] << endl;
    }

    cout << endl;

    inFile >> getEasyWord;

    while ((getEasyWord != "#") && (delimitCount = 2)){
        easy[easyCount] = getEasyWord;
        easyCount++;
        inFile >> getEasyWord;
    }

    delimitCount++;
    cout << delimitCount << endl;

    for (int kkk = 0; kkk < 27; kkk++){
        cout << easy[kkk] << endl;
    }

    inFile.close();
}

_getch();

return 0;
}

There are several small mistakes in this code and in the sample text file:

1- The sample file should have a # at the end, otherwise the last loop will run forever.

2-

while ((getHardWord != "#") && (delimitCount = 0))

Will always evaluate to false, since delimitCount will be zero. You should initialise delimitCount to zero in its declaration and drop the assignment in this loop, so that it becomes:

while (getHardWord != "#")

3- If you're closing the file at the end, your while condition should be to check that the file is open, so instead of:

while (inFile)

use

while (inFile.is_open())

I tested your code with these changes and it worked fine:

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "conio.h"

using namespace std;

int main() {

ifstream inFile("dictionary.txt");

// Check for error
if (inFile.fail()){
    cout << "Error Opening File." << endl;
    exit(1);
}

string hard[27], medium[29], easy[33];
string getHardWord, getMedWord, getEasyWord; 
int hardCount = 0, medCount = 0, easyCount = 0, delimitCount=0; // n is number of #
  // delimiters and count is array position

// Read the dictionary file until the end of file
while (inFile.is_open()){
    inFile >> getHardWord;

    while ((getHardWord != "#")){
        hard[hardCount] = getHardWord;
        hardCount++;
        inFile >> getHardWord;
    }

    cout<<hard<<endl;

    cout<<endl;


    delimitCount++;
    cout << delimitCount << endl;

    for (int iii = 0; iii < 27; iii++){
        cout << hard[iii] << endl;
    }

    cout << endl;

    inFile >> getMedWord;

    while ((getMedWord != "#") && (delimitCount = 1)){
        medium[medCount] = getMedWord;
        medCount++;
        inFile >> getMedWord;
    }

    delimitCount++;
    cout << delimitCount << endl;

    for (int jjj = 0; jjj < 27; jjj++){
        cout << medium[jjj] << endl;
    }

    cout << endl;

    inFile >> getEasyWord;

    while ((getEasyWord != "#") && (delimitCount = 2)){
        easy[easyCount] = getEasyWord;
        easyCount++;
        inFile >> getEasyWord;
    }

    delimitCount++;
    cout << delimitCount << endl;

    for (int kkk = 0; kkk < 27; kkk++){
        cout << easy[kkk] << endl;
    }

    inFile.close();

}

_getch();

return 0;
}

Is there a reason you aren't using getline? If I understand your code correctly, it could be simplified by just using # as a delimiter in that function.

for (int i = 0; i < 27; i++) //It looks like there are 27 words per type, if that's wrong change this
{
    getline(inFile, getHardWord, '#');
    hard[i] = getHardWord;
}
//And so on for the other difficulties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM