#checking if the file i am reading from is the correct format

79 messages · Page 1 of 1 (latest)

dry kettle
#

lets say i am reading numbers from a file that should be in the format
1 1 1
1 1 1
but its
1 1 1 1
1 1
how can i check for this and throw an error?

bold rockBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

eager tulip
#

What are the conditions for it failing? Is the input supposed to be 3 characters per line?

dry kettle
#

its different for some lines but thats the general idea. for example i only want 1 number on line 2. if the file has 2 or more it throws an error

eager tulip
#

I'm not quite sure what you mean, your example above has 3 characters on the second line

dry kettle
#

yeah sry what i send was just a simple example

eager tulip
#

If you can expand a little more I can help

dry kettle
#
mr_rt_image
500
500 0.0 0.0 0.0
0.5 0.7 1.0
6
1.0 1.0 0.0 1.0 1.2 -2.95 0.25
0.82 .0.0 0.0 -0.25 -0.255 -0.75 0.25
0.8 0.5 0.3 -0.2 0.35 -3.0 1.0
0.0 1.0 0.42 0.0 -22.5 -1.0 22.0
0.0 0.42 1.0 0.15 -0.255 -0.6 0.1
1.0 0.0 0.42 0.025 -0.255 -0.5 0.1
#

this is my txt file

#

as you can see for example second line has only 1 number

#

if someone changes that file and puts a second number on the second line

#

i want the programm to throw an error

#

sorry if my explanation is bad

eager tulip
#

You'd have to know if the file was modified

#

Unless every single file that you check has the same structure, and is supposed to only have one non-zero value in the second line

dry kettle
#

i want the programm to check how many numbers are in every line and throw an error based on that number if its not a static value i have inputed in the code (1 for example for the second line)

#

not sure if it is even possible thats why i am asking

eager tulip
#

Yeah that's possible

#

So just read each line, parse the numbers, toss out any that are 0.0, and then count the numbers that are left

#

If you have any more than the count expected, then throw an error

dry kettle
oblique quarry
#

Can you show your code so far?

#

Just copy and paste

dry kettle
#
Sphere sphere;
    Camera camera;
    World world;
    std::ifstream file("read.txt");
    
    if (!file.is_open()) {
        std::cerr << "Failed to open the file." << std::endl;
        return 1;
    }

    std::string name;

    file >> name;

    std::ofstream(name + ".ppm");

    int width,height;
    file >> width >> height;
    camera.SetWidth(width);
    camera.SetHeight(height);

    double x,y,z;
    file >> x >> y >> z;
    vec3 cam_center;
eager tulip
#

There are probably a few ways to do it. I would personally split each line into a string. Then look for the first space in the line using std::find (or commas, or whatever you're using. Based on the text you gave me you're using spaces). Then take the characters before that, turn it back into a number, and see if it's 0.0. If it's not 0.0, then increment a count. Once there are no more spaces found then you can read the last number, and again compare to 0.0/increment the count if need be.

Once the line is parsed, you can compare the count to expected 🙂

#

Or

dry kettle
#

there isnt much yet as i am just setting up the reading rn without the checks

eager tulip
#

I think you can utilize the stream you already have when taking data in, instead of doing the whole line just take each number at a time using space as the delimiter

dry kettle
eager tulip
#

I don't know off the top of my head but there's probably a way to look for the newline character so you when you cross over to the next line

#

Then you don't have to write any weird parsing code, it will do it all for you

dry kettle
#

oohhh

#

alright tyvm!

eager tulip
#

Yup, let us know if you have any issues with that 🙂

#

Google should be of good help, it should be pretty easy to find some info on how to use that ifstream to grab individual numbers, and to tell if you're reaching a new line

dry kettle
#

also quick question

eager tulip
#

sure

dry kettle
oblique quarry
#

you can use push_back

dry kettle
#

class "vec3" has no member "push_back"

#

the error i get

oblique quarry
#

Oh it’s an object

#

Yea, the way you did it sounds good

dry kettle
#

a pointer to a bound function may only be used to call the function

#

thats what i get when i try the way i said

#

thats why i am confused

oblique quarry
#

Do you have parenthesis after the function calls

#

Can you copy paste the vec3 class declaration if that’s not the issue

dry kettle
#
class vec3 {
  public:
    double e[3];

    /**
     * @brief Default constructor. Initializes all elements to 0.
     */
    vec3() : e{0,0,0} {}

    /**
     * @brief Constructor that initializes the vector with the given values.
     * @param e0 The value for the first element.
     * @param e1 The value for the second element.
     * @param e2 The value for the third element.
     */
    vec3(double e0, double e1, double e2) : e{e0, e1, e2} {}

    /**
     * @brief Get the x-coordinate of the vector.
     * @return The x-coordinate.
     */
    double x() const { return e[0]; }

    /**
     * @brief Get the y-coordinate of the vector.
     * @return The y-coordinate.
     */
    double y() const { return e[1]; }

    /**
     * @brief Get the z-coordinate of the vector.
     * @return The z-coordinate.
     */
    double z() const { return e[2]; }
#

i cant send the whole thing

#

is this enough?

oblique quarry
#

Yea, when you create the object you can call the constructor with x, y, and z

#

You’re using the default constructor which makes them all 0

#

Right now

dry kettle
#

oh vec3 cam_center(x,y,z);

#

like this?

oblique quarry
#

Yea

dry kettle
#

alright tyvm

oblique quarry
#

np

#

You should read about constructors since they’re important and I think they’re easy to understand if you use them a couple of times

dry kettle
#

thx for the advice

bold rockBOT
#

@dry kettle Has your question been resolved? If so, type !solved :)

oblique quarry
#

it says this when you say thanks

#

don't say solve yet lol

dry kettle
#

oh

#

xd

#

alright

oblique quarry
#

Also, you might want to start giving more descriptive function names as it's good practice. The single letter functions like x(), y(), z() are not useful or clear when you want to call them in the main function.

I think better names for these are get_x(), get_y(), get_z() since they're returning their values

eager tulip
#

nou is right, make sure that your variables and functions are descriptive of what they do 🙂

#

I hate when people use variables with one or two letters all over their code, unless it's directly relevant to what the code is being written for. Makes it a lot more difficult to read and undersatnd

oblique quarry
#

I used to do this, so I'm just trying to get him to learn from my mistakes lol

eager tulip
#

Yeah I'm glad that was something I was taught early haha

#

Even my teachers do that, and I'm like man...

#

It takes barely longer to type someVar instead of sv, and it makes your code so much more readable

dry kettle
#

thx a lot for the advice guys

tame patio
#

is this question solved?

dry kettle
#

not yet. found a method but i have issues testing it because of unresolved extternals that i am trying to fix

#

will probably be solved by night

dry kettle
#

!solved