C ++ reading a textfile into linked list

text file:

Humpty Dumpty sat on the wall
Humpty Dumpty had a great fall
all the kings horses and all the kings men
could not put Humpty Dumpty back together again
---------------------------------------------------

the idea is to read in this sentence into a linked list..

i would a ppreciate it if you can post similar codes that read in a sentence..

thanks..

pls help me because my dead line is 2 days away...:-|

Dani AI

Generated

asked for a way to read a text file into a linked list and posted a working character-based solution. That works if you really want each node to hold a single character, but it skips whitespace when using formatted extraction and uses a fixed-size C string for the filename (unsafe). First decide whether you want nodes to hold characters, words, whole lines, or sentences — the reading approach changes for each.

A simple, robust pattern to store each line as a node uses std::ifstream + std::getline and std::list<std::string> (no fixed buffers, preserves spaces). Use std::vector instead if you need random access or better cache performance. To split lines into words, run each line through std::istringstream and push tokens. To split into sentences you can call std::getline with a delimiter (for example '.') and then trim whitespace.

Example: read lines into a list

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <stdexcept>

std::list<std::string> read_lines(const std::string& path)
{
    std::ifstream in(path);
    if (!in) throw std::runtime_error("cannot open file");
    std::list<std::string> out;
    std::string line;
    while (std::getline(in, line))
        out.push_back(line);
    return out;
}

Quick tips and pitfalls

  • Avoid while (!in.eof()). Use while (std::getline(...)) or while (in >> token).
  • Use std::string for filenames to avoid fixed-size buffer overflow.
  • If you parse sentences by '.', handle trailing spaces and punctuation (trim).
  • If you implement a custom linked list, manage memory (use RAII or smart pointers) and provide clear ownership semantics.
  • Check the working directory or full path if the file fails to open.

For reference: see std::getline and std::list docs on cppreference for behavior details and edge cases.

Recommended Answers

All 3 Replies

Try something like this! Remember to #include <list>

void Standard::get_list()
{
    char filename[16];
    char variable;

    cout << "Please enter a filename: ";
    cin >> filename;

    fstream instream(filename);

    if(instream.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    while (instream >> variable)
        list1.push_back (variable);

    cout << "\nThe contents of your file: \n" << endl;

    for (i=list1.begin(); i != list1.end(); i++)
    {
        cout << *i << " " << endl;
    }
}

thank you!

thank you!

Not a problem wanted to know if it worked out for you?

Melinda

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.