#checking if the file i am reading from is the correct format
79 messages · Page 1 of 1 (latest)
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.
What are the conditions for it failing? Is the input supposed to be 3 characters per line?
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
I'm not quite sure what you mean, your example above has 3 characters on the second line
yeah sry what i send was just a simple example
If you can expand a little more I can help
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
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
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
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
i dont know how to count how many numbers are in the line. thats my problem
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;
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
there isnt much yet as i am just setting up the reading rn without the checks
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
alright i probably can do this ty
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
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
also quick question
sure
i have a vec3 variable at the bottom of my code. to insert the values of x,y,z would i just need to cam_center.x = x for example?
you can use push_back
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
Do you have parenthesis after the function calls
Can you copy paste the vec3 class declaration if that’s not the issue
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?
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
Yea
alright tyvm
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
thx for the advice
@dry kettle Has your question been resolved? If so, type !solved :)
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
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
I used to do this, so I'm just trying to get him to learn from my mistakes lol
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
thx a lot for the advice guys
is this question solved?
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
!solved