Enchanter
Enchanter

Reputation: 117

Reading In Data From A Text File And Separating Out The Data Contained In Each Line

In the program currently I wish to read in text from a text file line by line, extracting data from each line. Each line in the text file contains a letter such as “A” followed by a colon “:” followed by an integer such as “2”. The function in the program that should read in the data currently looks like this:

void readFile()
{
std::ifstream aFile;                                                                                                                                                                                
std::string alldata;
std::string letter;                                                                                                                 
aFile.open("KeyPopularity.txt");
while (std::getline(aFile, alldata))
{                                                                                                           
    letter = alldata[0];
    for (int i = 0; i < keyboardKeys.size(); i++)
    {
        if (keyboardKeys[i].character==letter)                                                                                      
        {
            keyboardKeys[i].noOfPresses = int(alldata[2]);                                                                          
        }
        std::cout << letter << " ";
        std::cout << int(alldata[2]) << "\n";
    }
}
aFile.clear();                                                                                                                      
}

However the alldata variable at position 0 (alldata[0]) instead of listing just a single character ie the letter lists a number such as 87 before the letter eg. 87 W as opposed to just W. The question is why is this happening and is there a way to extract the data from each line in the text file line by line to obtain the letter at position 0 of a line string and the number at position 2 of the line string?

Upvotes: 0

Views: 49

Answers (1)

Stefan Gheorghe
Stefan Gheorghe

Reputation: 29

Some observation/mistakes in your program:

  • letter is a std::string, however you assign a character to it; this is allowed but are you sure that this is the intent? You can use char instead of string.
  • alldata[2] is a char. For the line "A:2" for example, the value will be '2' (with ascii value 50, which is not the same as value 2). You cannot simply convert it to int with int(). To convert a character of a digit ('0'..'9') to its respective integer value you need to subtract the value 48 from the ascii code (or '0').
  • also change type of keyboardKeys[i].character to char instead of std::string

You can rewrite your code like this:

void readFile()
{
    std::ifstream aFile;
    std::string alldata;
    char letter;
    aFile.open("KeyPopularity.txt");
    while (std::getline(aFile, alldata))
    {
        letter = alldata[0];
        for (int i = 0; i < keyboardKeys.size(); i++)
        {
            if (keyboardKeys[i].character == letter)
            {
                keyboardKeys[i].noOfPresses = alldata[2] - '0';
            }
            std::cout << letter << " ";
            std::cout << alldata[2] - '0' << "\n";
        }
    }
    aFile.clear();
}

Upvotes: 1

Related Questions