« first day (1966 days earlier)      last day (878 days later) » 

2:04 AM
@nwp does this copies the data? Or holds a memory address pointing to it
 
 
2 hours later…
3:40 AM
char* Shader::ReadShader(const char* fname,const unsigned int type){
    std::ifstream Reader(fname);
    std::string temp_buff;
    std::string shader;
    if(!std::ifstream(fname)){
        if(type==GL_VERTEX_SHADER){
            std::cout<<"(OPENGL : VERTEX SHADER SOURCE "<<fname<<" DOES NOT EXIST!)\n";
        }
        else if(type==GL_FRAGMENT_SHADER){
            std::cout<<"(OPENGL : FRAGMENT SHADER SOURCE "<<fname<<" DOES NOT EXIST!)\n";
        }
        shader="";
    }
    while(getline(Reader,temp_buff)){
fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(0.8f, 0.3f, 0.02f, 1.0f);\n"
"}\n\0";
it is supposed to look this way,so am i doing it right?
#include <iostream>
#include <cstring>
#include <fstream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#ifndef SHADERS
#define SHADERS
class Shader{
public:
Shader(const char* vertex_fname,const char* fragment_fname);
unsigned int& GetProgram();
char* ReadShader(const char* fname,const unsigned int type);
void UseShader();
~Shader();
private:
unsigned int VertexShader;
unsigned int FragmentShader;
unsigned int ShaderProgram;
char* VertexShaderSource;
char* FragmentShaderSource;
};
#endif
 
 
1 hour later…
4:58 AM
Oh my bad pasted in wrong room can someone move it to C C++ Question And Answer Room
 
Hello everyone! One question! How can I swap a string that contains first name and last name? Example: std::string full_name{"Justin Beiber"}; I want the output to be Beiber Justin?
I know about swap function
But it won't work here
I thought maybe I could solve it with using if(full_name == " ") do something with the full_name.length()
any build in methods I could use?
 
5:45 AM
5 messages moved from Lounge<C++>
 
 
1 hour later…
nwp
7:11 AM
@Agent_A It just points to the data, so you have to keep the std::string alive while using it.
 
 
7 hours later…
2:13 PM
Hi all!
I've been wondering about the thread safety of std::shared_ptr, since I was asked that question in an interview (and it was a complete surprise).
In particular, the C++ Stndard Library book by N. Josuttis reads that different threads can modify the reference counter independently, and the atomic operations are overloaded exactly to avoid such a race condition. However, Meyers' Effective Modern C++ suggests that the reference counters should be atomic (without stating explicitly that they are required to be atomic by the standard). I've taken a look at the local implementation (g++ 9) and they are indeed atomic.
Who's right then, and what is the purpose of the atomic operations on shared pointers? Is it to copy the actual pointer value, not just the reference counters, atomically?
Maybe this is a decent SO question?
 
nwp
2:31 PM
The question has been asked multiple times.
The answer can be put fairly boringly. std::shared_ptr<T> is just as thread-safe as T *>.
You can modify them from different threads which is a data race which is undefined behavior.
You can access different objects from different threads which is not UB.
The latter is surprising because the implementation of std::shared_ptr has a shared state and if you let std::shared_ptrs pointing to the same control block run out of scope then you'd think there is a race on the control block. However, the standard says this is allowed, meaning the implementation has to safeguard the control block somehow.
If they do that using std::atomic<std::size_t> reference_counter; or other means is left as an exercise to the implementation.
In practical terms it means you can completely ignore the thread-safety aspects of std::shared_ptr because it behaves the same as all the other not thread related types.
 
 
2 hours later…
4:37 PM
Hello everyone! (=
Can you get two string-type variables with the use of only one std::getline in c++?
I mean
Imagine the file contains
Jordan Jonson
London 233445 longway streeet 34
And I want to swap the first and last name
in one string
how can I do it without failing the reading file
correction writing to file
?
would really appriciate if anyone could help me! Thank you!
 
4:56 PM
:53541808
#include <iostream>
#include <fstream>
#include <string>

int main() {
	std::fstream file("test.txt", std::ios::in | std::ios::out | std::ios::binary);

	auto line_start_position = file.tellg();
	std::string line;
	std::getline(file,line);
	auto first_space_position = line.find_first_of(' ');
	if (first_space_position == std::string::npos) {
		std::cout << "first line contained no space" << std::endl;
		return -1;
	}
	file.seekp(line_start_position);
	file.write(line.data()+ first_space_position+ 1, line.size()-first_space_position-1);
 
5:20 PM
Hey dear Peter! Thank you for your help! Can I please share my code with you?
@PeterT Hey dear Peter! Thank you for your help! Can I please share my code with you?
 
5:33 PM
sure, go ahead
 
 
1 hour later…
6:39 PM
:53542026
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>



int main(){
std::ifstream input; // read from file
std::ofstream output; // write to file


input.open("names.txt", std::ios::in); // Läs från filen
if(!input){
std::cerr << "The file failed to be loaded" << std::endl;
// std::exit(EXIT_FAILURE);
return 1; // Exit the program
}



output.open("output.txt", std::ios::out); // skriv ut resultatet i en annan fil.
@PeterT
The program reads and writes info about people
This post is hidden. You deleted this post 14 hours ago.
I'm trying to read from one file which contains around 4000 fictional people. Looks like this in the reading file but with many more people following.

Justin Biber
199403012345
London second street 12 33444, Miami
my task was to swap the names with the last name to look like this...


Bieber Justin [M] // Male // Part one
London second street 12 33444, Miami
I want my output to be like this above.
The problem is I can't swap the first_name and last_name.
due to the reason that I defined my string as full_name
 
@Amiu ok, so is there a delimiter between each person entry?
or is it always like 3 lines
 
it is always three lines in inpute
like this
Justin Biber
199403012345
London second street 12 33444, Miami
the out put I want is
Bieber Justin [M] // Male // Part one
London second street 12 33444, Miami
But I get
 
ok, you wanted to just output to another file, you made it sound like you needed it to be in-place somehow
 
Justine Bieber [M] // Male // Part one
London second street 12 33444, Miami
Note: look at the firs and last_name
I don't know how I can solve this problem!
yes, exactly (= to another file haha
sorry
 
	std::string full_name; //i.e. "Justin Bieber";
	std::string full_name_reverse;
	auto first_space_position = full_name.find_first_of(' ');
	if (first_space_position == std::string::npos) {
		std::cout << "full name contained no space" << std::endl;
		full_name_reverse = full_name;
	}
	else {
		std::string last_name = full_name.substr(first_space_position + 1);
		std::string first_name = full_name.substr(0, first_space_position);
		full_name_reverse = last_name + " " + first_name;
	}
 
6:50 PM
should I put it in my while loop?
 
sure, and output "full_name_reverse" of course
 
[M]
Bössgränd 90, 373 02 RAMDALA

[M]
Sandviken 76, 710 27 DYLTABRUK

[M]
Löberöd 29, 521 29 KÄTTLISTORP

[M]
Hagagatan 87, 360 53 SKRUV
The output is this
 
yeah you're supposed to keep your existing "full_name", not copy the empty version in my snippet
 
okey, I will try it! Thank you very much Peter!
You are a genius, dear Peter! Thank you very much! You are such a beautiful, person!
Thank you!
It works now!
I'm new, started with programming two weeks ago! And I was feeling intimidating and was doing eveything to fix it but now I see your code and understand what I needed to do!
Thank you agin!
 
 
3 hours later…
9:37 PM
hey, I have a string like (12) (asdf) SOME OTHER CONTENT (that is, (number) (string with no whitespace) string with possible whitespace. why isn't it read correctly by sscanf(line, "(%d) (%s) %[^\n]", &number, string_one, string_two)? more specifically, string_two doesn't get "SOME OTHER CONTENT"
 
 
1 hour later…
10:48 PM
what even is that scanf syntax that looks like regex? Is that some weird extension?
huh "negated scanset" is actually a thing
 
11:32 PM
@PeterT Dear Peter! I have found another way as well to solve the string problem that you helped me with! Look at this code, please! What are your thoughts on this? I'm using this to extract from the file, and swap the names as well! Thank you for guiding me again (=
std::istringstream input (full_name);
input >> std::noskipws;
input >> first_name >> std::ws >> last_name;
first_name.swap(last_name);
output << first_name << ", " << last_name << gender_checker << std::endl;
 

« first day (1966 days earlier)      last day (878 days later) »