Home > Software design >  When reading in each line from a file in C , how would one store values from each line without know
When reading in each line from a file in C , how would one store values from each line without know

Time:02-20

Sorry if this looks ugly, it's my first post on any code forum. Say I'm reading in a file with three lines:

lineone 1 1 2 2 3 3 4 4 
linetwo 1 1 2 2 3 3 
linethree 1 1 5 5

How would I go through each line, and save the first number as, let's say, variable a1, then the second variable as b1, then the third number as a2, then the fourth number as b2, without actually knowing how many numbers there are in the line?

It is worth noting that I have to access these numbers after reading each line, and pass them into an object that creates a point, for example:

Vertex(a1, b1)

Thank you for any help, and I'll be glad to answer any questions. I'm just trying to learn this concept in order to complete a big project.

CodePudding user response:

You can always use an array, or more preferably, 'std::vector', which is just a list of certain type, as such:

std::vector<Type> name;

And for your case:

std::vector<Vertex> vertices;

And anytime you wish to insert a vertex into the vector, just type in:

// Insert a variable of type Vertex
vertices.push_back(vertex_variable);

// Insert a new vertex
verices.push_back(Vertex()); // Vertex() is the constructor, can be replaced

CodePudding user response:

You can use std::vector for this as shown below. In the below program, we have a class called Vertex which represent a single point. Also, we have a struct called Line which represents a particular line. A Line object is made up of a line description data member called lineName and a vector of Vertex.

#include <iostream>
#include<sstream>
#include<string>
#include<vector>
#include<fstream>
//this class represents a single vertex which is made up of two point x and y
class Vertex 
{
     int x = 0;
     int y = 0;
     
     public:
     
         //parameterized constructor for initializing x and y
         Vertex(int px, int py): x(px), y(py)
         {
            
         }
         
         //display Vertex 
         void printVertex() const
         {
             std::cout<<x <<" "<<y<<" ";
         }
};
//this class represents a line segment which is made up of multiple vertices
struct Line 
{
    std::string lineName; //this describes which line it is 
    std::vector<Vertex> vertices; //this is a collection of all the vertex that this particular line is made up of 
    
};
int main()
{
    std::string lineString;
    
    int pointX = 0, pointY = 0;
    
    std::ifstream inputFile("input.txt");

    //a collection of all the Lines 
    std::vector<Line> lines;
    
    if(inputFile)
    {
        //go line by line 
        while(std::getline(inputFile, lineString))
        {
            std::istringstream ss(lineString);
            
            //create a Line object 
            Line currentLine;
            
            //read the first string specifying which line is this 
            ss >> currentLine.lineName;
            
            //go number by number 
            while(ss >> pointX >> pointY)
            {
                //add vertex to vector vertices 
                currentLine.vertices.emplace_back(pointX, pointY);
            }
            //add this currentLine to lines 
            lines.emplace_back(currentLine);
            
        }
        
    }
    else 
    {
        std::cout<<"file cannot be opened"<<std::endl;
    }
    
    
    //lets confirm that we have all the lines correctly stored
    for(const Line& line: lines)
    {
        std::cout<<line.lineName<<": ";
        for(const Vertex& vertex: line.vertices)
        {
            vertex.printVertex();
        }
        std::cout<<std::endl;
    }

}

See Demo.

  • Related