Hey guys i need some help. I have a txt file which looks like this:
234 56 123 34 456 56 7 45.....
123 3 34 56 56 67 67 2.....
.
.
.
Where each line represents a series of points in coordinate system, and the first number in each line represents the x-point, second y-point,third x-point and so on. I need to read the file, and store the data. I would like to store it in a structure:
struct path {

int x();
int y();
};
I would also need some kind of automatic variable naming for struct path, so when i begin to store second line of points i would have struct name path2. These are just my ideas, so if you can, please help with every approach you can think of. And when the data storing is done i should be able to know the total number of paths and total number of point pairs (x,y) for every path. If you have any ideas please help, i'm a begginer and really need to solve this problem. Thank you in advance!!

Dani AI

Generated

A simple, robust approach is to treat each input line as one path, parse all integers on that line into a vector, then pair them as (x,y) points and store each path in a dynamic container. Creating variable names like path2, path3, etc. is unnecessary and brittle; use std::vector so paths are indexed and counted easily.

Example pattern (C++11+):

struct Point { int x, y; };
struct Path  { std::vector<Point> pts; };

std::ifstream in("data.txt");
std::string line;
std::vector<Path> paths;

while (std::getline(in, line)) {
    std::istringstream iss(line);
    std::vector<int> vals;
    int v;
    while (iss >> v) vals.push_back(v);
    if (vals.empty()) continue;
    if (vals.size() % 2 != 0) {
        // decide policy: ignore last value, log an error, or handle differently
    }
    Path p;
    for (size_t i = 0; i + 1 < vals.size(); i += 2)
        p.pts.push_back({ vals[i], vals[i+1] });
    paths.push_back(std::move(p));
}

// totals:
size_t total_paths = paths.size();
size_t points_in_path0 = paths.empty() ? 0 : paths[0].pts.size();

Troubleshooting notes: trim or skip blank lines; handle non-numeric tokens (replace commas or use regex to extract integers); decide what to do when a line has an odd count of numbers (log and skip the trailing value or treat it as malformed). For very large files where only counts are needed, avoid storing every point: increment path and pair counters on the fly and discard the per-point data to save memory.

This follows ’s suggestion to use file streams while addressing the “automatic naming” concern raised by and the maintainability point from . ’s hint about fstreams is also consistent with the pattern above.

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

>I would also need some kind of automatic variable naming for struct path, so when i begin to store second line of points i would have struct name path2.

I don't quite get why you need to differentiate between the separate lines.

It makes your code slightly more difficult

Looks to me like a homework assignement...
try using fstreams file>>

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.