Hi guys,

I am having great difficulty in reading a text file char by char and storing them in a linked list.

I started off with simply attempting to read from file char by char, and that worked fine.

But when attempting to store the file into a linked list, it just doesnt seem to work.

Could someone please assist me with small sample code in regards to this. I have code written out and the error messages that im getting is as follows :

test11.cpp:79:16: warning: no newline at end of file
test11.cpp: In function 'int main(int, char**)':
test11.cpp:41: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::get(char [1])'
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/istream:272: note: candidates are: typename std::basic_istream<_CharT, _Traits>::int_type std::basic_istream<_CharT, _Traits>::get() [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/istream:286: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/istream:313: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/istream:324: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/istream:347: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.0.2/../../../../include/c++/4.0.2/istream:357: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]

Please let me know if you want to see a snippet of my code, and i will post bit by bit

Dani AI

Generated

The compiler message shows why the read failed: you passed a char array where the stream expected a different overload. already pointed out the immediate fix (use a single char variable). Below is a simple, robust pattern to build a singly linked list while reading the file byte‑by‑byte using an iterator (this avoids overload confusion and reads raw bytes reliably).

#include <fstream>
#include <iterator>

struct Node {
    char c;
    Node* next;
    Node(char ch) : c(ch), next(NULL) {}
};

int main() {
    std::ifstream in("input.txt", std::ios::binary);
    if (!in) return 1;

    std::istreambuf_iterator<char> it(in), end;
    Node* head = NULL;
    Node* tail = NULL;

    for (; it != end; ++it) {
        Node* n = new Node(*it);
        if (!head) head = tail = n;
        else { tail->next = n; tail = n; }
    }

    // use list...
    while (head) { Node* tmp = head; head = head->next; delete tmp; }
    return 0;
}

Troubleshooting and practical notes:

  • Always check that the file opened successfully before reading.
  • If you need exact byte-for-byte input (including CR on Windows), open with std::ios::binary; otherwise text mode may translate line endings.
  • Manual linked lists are easy to get right but expensive in practice: prefer std::string or std::vector<char> for most text tasks, or std::list<char> if you truly need list semantics. STL containers also avoid manual new/delete.
  • Watch memory and deletion: every new above must be freed (or replace raw pointers with smart pointers in modern C++).

Quick checklist for : confirm the file path, compile with warnings enabled (e.g. -Wall), try the iterator pattern above or the single‑char read that suggested, and test on a tiny file to verify the list-building logic.

Looks like you are doing something like

ifstream ifs("foobar");
char chr[1];
// Read a char
ifs.get(chr);

Just change to plain char, i.e.

ifstream ifs("foobar");
char chr;
// Read a char
ifs.get(chr);
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.