Home > Software engineering >  Appending line from a file into char array
Appending line from a file into char array

Time:06-06

char wordList[200];
ifstream wordListFile ("wordlist.txt");


for(std::string line; getline(wordListFile, line); ) {
        wordListFile >> wordList;
}

This code currently returns the line at the end of the wordListFile (wordlist.txt),

is there any way to append the lines to wordList?

because when I use the append() function it returns an error.

CodePudding user response:

In the loop

for(std::string line; getline(wordListFile, line); ) {
        wordListFile >> wordList;

you are reading one line of input with getline(wordListFile, line);, but not doing anything with that line. Instead, you are reading the first word of the next line with wordListFile >> wordList;. This does not make sense.

If you want to append the line contents to wordList, then you could initialize wordList as an empty string and then use std::strcat:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

int main()
{
    char wordList[200] = "";
    std::ifstream wordListFile( "wordlist.txt" );

    for ( std::string line; std::getline(wordListFile, line); ) {
        std::strcat( wordList, line.c_str() );
    }

    std::cout << wordList << '\n';
}

For the input

This is line 1.
This is line 2.

this program has the following output:

This is line 1.This is line 2.

As you can see, the lines were correctly appended.

However, this code is dangerous, because if the file is too large for the array wordList, then you will have a buffer overflow.

A safer and more efficient approach would be to make wordList of type std::string instead of a C-style string:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string wordList;
    std::ifstream wordListFile( "wordlist.txt" );

    for ( std::string line; std::getline(wordListFile, line); ) {
        wordList  = line;
    }

    std::cout << wordList << '\n';
}

This program has the same output:

This is line 1.This is line 2.

CodePudding user response:

You can only use append on a std::string but your wordList is a char array.

Also a string isn't a list. Probably (but I am guessing) you want code something like this

std::vector<std::string> wordList;
for (std::string word; wordListFile >> word; ) {
    wordList.push_back(word);
}

Here wordList is a vector of strings, and push_back adds each word that you read to that vector.

If you actually want to append lines not words to a list then just use getline instead of >>

std::vector<std::string> lineList;
for (std::string line; getline(wordListFile, line); ) {
    lineList.push_back(line);
}

CodePudding user response:

because when I use the append() function it returns an error.

std::string::append (2) works well.

std::string wordList;
ifstream wordListFile ("wordlist.txt");

for(std::string line; getline(wordListFile, line); ) {
    wordList.append(line);
}

CodePudding user response:

std::string wordList;
std::ifstream wordListFile ;

wordListFile.open("wordlist.txt");
std::getline(wordListFile , wordList);
wordListFile.close();

Try this code. You could get whole string from the file. I wish this can help you.

  •  Tags:  
  • c
  • Related