Hi,
i am having some trouble trying to finish my program!
So far i have been able to load the data from a file into an array! Although now, what i cannot do is use this data. I cannot figure out how to use this data later in the program!
I have strugled with this for so long..please help

thanks

Dani AI

Generated

The original post says the file data was loaded into an array but then "cannot use this data later." As and pointed out, seeing the code is ideal; meanwhile the common, repeatable causes and reliable fixes are listed below so future readers with the same symptom can check them quickly.

Common causes to check:

  • Lifetime/scope: returning or storing a pointer to a local array (stack memory) produces a dangling pointer. Use a container with proper lifetime instead.
  • Passing semantics: arrays decay to pointers; passing by value vs by reference matters. Either pass a container by reference or return a container by value.
  • Size/index mistakes: iterating past the number of items read (off-by-one) or using an uninitialized size variable.
  • File I/O failures: file not opened or read errors; code that silently fails on open will leave the array empty.
  • Shadowing/uninitialized memory: local variables hiding the real variable, or writing into unallocated memory.

A safe, modern pattern is to read into a std::vector and either return it or pass it by reference to processing functions. Returning a vector by value is efficient (moves/copy elision) and avoids lifetime bugs.

#include <vector>
#include <string>
#include <fstream>

std::vector<std::string> loadLines(const std::string& filename) {
    std::vector<std::string> lines;
    std::ifstream in(filename);
    if (!in) return lines;
    std::string s;
    while (std::getline(in, s)) lines.push_back(s);
    return lines;
}

Process the data by reference (no copies) and always validate sizes and file-open results. Avoid returning pointers to local arrays:

void processLines(const std::vector<std::string>& lines) {
    for (const auto& line : lines) {
        // process
    }
}

// BAD: returning pointer to local array -> dangling pointer
int* badLoad() {
    int arr[100];
    // fill arr
    return arr;
}

When debugging, add simple checks (print sizes, check if (!in)), run with AddressSanitizer/Valgrind for memory problems, and assert indexes before access. For container semantics see std::vector and for file I/O see std::ifstream.

Recommended Answers

All 4 Replies

if you don't post your code, we can't really help you...

My crystal ball is pretty cloudy right now. If you want more help please be more specific, like post your code (using code tags) and information about what it is exactly that you are trying to do.

h

Oh, there's the problem. It should be:

H

C++ is a case sensitive language.

(sorry, couldn't resist. too much time spent on usenet. Really, tom, please post the full code file and problem description.)

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.