|
I should have been more specific. If that path was on Windows, it would have been a partial path.
I understand it's a Linux path.
|
|
|
|
|
Dave Kreskowiak wrote: The first / assumes "the current drive", whatever that is at the time.
Yes I know how pathing works in unix (and linux) and windows.
There are only two terms that I know. So if you know of another please provide the term and definition.
- Partial path.
- Full path.
Are you claiming that the first forward slash on a windows file path only means a 'partial path' but on unix/linux it always means a 'full path'?
On linux/unix the path might have a path that resolves to a different drive.
Otherwise, just like windows, it resolves to the current drive.
Thus it is in deemed a 'full path' on windows and unix/linux.
In terms of this forum which would be relevant for C/C++ include file paths...
A partial path in an include path on both types of OS will use the rules associated with the build process to resolve the path. That can either be because it defined a 'current' directory or it will resolve to the execution directory (or some variant.)
A full path (forward slash) in an include path on both types of OS will end up using the current drive excepting of course unless there is a path resolution to a different drive for linux/unix.
|
|
|
|
|
Quote: just like windows, it resolves to the current drive.
This is what I'm calling a partial path.
I'm not calling out anyone for anything here. I made a mistake because I assumed Windows. My mistake also shows that I don't do Linux very much, like once every couple of years so I have re-learn this crap every time.
|
|
|
|
|
Dave Kreskowiak wrote: My mistake also shows that I don't do Linux very much, like once every couple of years so I have re-learn this crap every time
Ditto, but in reverse!
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
Dave Kreskowiak wrote: This is what I'm calling a partial path.
Just to be clear what exactly do you think that the following does on Windows? On Linux?
Specifically is there any realistic difference between the two in general?
#include "/work/x/y/TestClass.h"
|
|
|
|
|
Just to be clear, I'm done with this.
|
|
|
|
|
For unix-like systems there are two terms in general use:
absolute path: any path that starts with a /
relative path: a path relative to the current directory. This is any path that does not start with a / Some examples might be src/projects/foo/bar.c or ../include or ./program
jschell wrote: On linux/unix the path might have a path that resolves to a different drive Technically true. For example, you might have /home mounted as a separate drive - it might even be a network drive. For the average user, the file system is homogeneous, that is you can navigate to any point in the file tree with out having to know if it's on a separate drive/partition,networked-fs, etc. Its only really an issue for applications that may want to move files from one location to another, as you can not use link() and unlink() to move a file to a different place in the file tree if the source and target are in different file systems.
Edit: It should be noted that file systems may be mounted at any level, so you might have a partition mounted as /home, and another partition mounted as /home/games, and yet another mounted as /home/games/adventure.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
k5054 wrote: Technically true.
You know that on Windows I can map a different drive to a directory in the current drive file system right?
|
|
|
|
|
I would assume that you didn't write that line. The source that gave you that line should have provided instructions about additional packages needed to compile the code: where to get them from, where to place them, etc.
That being said, to me it's quite obvious the author didn't know what he/she was doing. I'd rather look for better quality code.
Mircea
|
|
|
|
|
Salvatore Terress wrote: and the error ( file not found ) is not helping much to fix it. Well that is easy to correct. Work your way down that path starting in directory /media , to see whether all the subdirectories exist. And oncel you reach the last one, see if the file is there, or if it is possibly mis-spelled. The problem for us with such issues is that we have no access to your system so cannot do any of the searching.
But it is usually not best practice to use full paths in your #include statements. Just us the name of the header file and add its path to the list of directories in the include list - either by use of the -I option to the compiler, or the relevant environment variable (see man page for details).
|
|
|
|
|
[edit] I solved the background flickering problem by changing the last argument in the InvalidateRect() function to false. I also got the test rectangle moving. The only problem left is that the white moving rectangle is still flickering.
[/edit]
I wrote some code, for some reason the rectangle that should work as background is flickering and I can`t get a test rectangle to move.
I`m pasting the source bellow, please help me get my code in proper configuration.
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
UpdateGame();
}
}
switch (msg)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
draw(hdc);
movingR = movingR + 0.1;
RECT ARectangle;
ARectangle.left = 0;
ARectangle.top = 0;
ARectangle.right = 600;
ARectangle.bottom = 600;
InvalidateRect(hwnd, &ARectangle, false);
UpdateWindow(hwnd);
EndPaint(hwnd, &ps);
return 0;
void draw(HDC hdc)
{
Gdiplus::Graphics gf(hdc);
Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 0));
Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 255, 0));
Gdiplus::SolidBrush brush2(Gdiplus::Color(255, 255, 255, 255));
gf.FillRectangle(&brush, 0, 0, 600, 600);
gf.FillRectangle(&brush2,(int)movingR, 200, 100, 100);
}
void UpdateGame()
{
}
modified 10-Dec-23 7:50am.
|
|
|
|
|
You should use double buffering. Just google for “OpenGL double buffering”.
Mircea
|
|
|
|
|
Do not call InvalidateRect and UpdateWindow from inside your WM_PAINT handler. You should be setting the rectangle's dimensions from outside the handler, and then calling InvalidateRect to cause the update to happen. Then when you get to the drawing code you get the rectangle details from inside the Paintstruct . Also using hardcoded values like you show above is not the correct way to do it.
|
|
|
|
|
Richard and Mircea thanks for advice
|
|
|
|
|
I created some code to download a web page:
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char* argv[]) {
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cerr << "WSAStartup failed: " << iResult << std::endl;
return 1;
}
const char* hostname = "www.example.com";
const char* path = "/";
struct hostent* host = gethostbyname(hostname);
struct sockaddr_in server_address;
int socket_fd, bytes_received;
char buffer[1024];
if (host == NULL) {
fprintf(stderr, "Error: Could not resolve hostname.\n");
int error_num = WSAGetLastError();
exit(1);
}
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
perror("Error: Could not create socket.\n");
exit(1);
}
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(80);
memcpy(&server_address.sin_addr, host->h_addr_list[0], host->h_length);
if (connect(socket_fd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
perror("Error: Could not connect to server.\n");
exit(1);
}
char* request = (char *)malloc(strlen(path) + strlen(hostname) + 16);
sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, hostname);
send(socket_fd, request, strlen(request), 0);
while ((bytes_received = recv(socket_fd, buffer, sizeof(buffer), 0)) > 0) {
fwrite(buffer, 1, bytes_received, stdout);
}
free(request);
closesocket(socket_fd);
return 0;
}
It prints the web page out, but then it seems to get stuck in the recv function.
Anyone know what's wrong?
Thanks.
|
|
|
|
|
Some things that might help you diagnose the problem:
- use a debugger and check return codes from each function. See where it fails.
- a network sniffer like Wireshark can help you see what’s going on on the wire.
- a network terminal program like Putty can be used to check the expected behavior. See if the server really answers the way you expect.
Also, I assume you are using a real server name, not example.com 😀
Mircea
|
|
|
|
|
I got it working. I added this code after the send call:
iResult = shutdown(socket_fd, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(socket_fd);
WSACleanup();
return 1;
}
I also had to increase the buffer size for malloc. There was a bug where it wasn't allocating enough bytes.
Thanks.
|
|
|
|
|
Glad to hear it! Now, if you want to check a more C++ way of working with sockets, you can take a look at my series of articles about working with Windows sockets in C++. First instalment is Windows Sockets Streams[^]. Latest version of code can be downloaded from GitHub[^].
Mircea
|
|
|
|
|
mike7411 wrote: get stuck in the recv function.
I suspect that none of the suggestions in the other post are going to help with this.
Your design is wrong. The message flow looks like this.
- Client- Open socket (server accepts)
- Client- Send request
- Server- Sends response
- Client- Read request
- Client- Closes socket
Notice in the above the server does nothing to terminate the message stream. The client is responsible, not the server.
So recv() sits there waiting for a message that the server will never send.
The HTTP protocol defines a request and then a response.
You however are not following that protocol. At a minimum you are missing the following
1 - You are not checking for a HTTP error code.
2 - You are not reading the 'content-length' header attribute.
3 - You are not looking for the response body.
If you were doing the second then you would use that to read to the end of the message using the content-length. That specifically defines how many bytes the server should send in the response body
Additionally there are additional error conditions that good code must expect
- The content-length might be missing. Invalid HTTP but one must still anticipate that.
- The content-length is too long. Very difficult to deal with. And it still results in the problem you are seeing. So you must add a timeout. Google for how to do that.
What about if the content-length is too short? Myself I just ignore that case. Because in most cases content-length will always be right. And too short might lead to other problems but you have no way to detect that unless you always do a timeout read, and that will slow the application to no point (again because it almost always will be right.)
|
|
|
|
|
|
I wrote some code to copy a file:
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
fs::path source_file("source.txt");
fs::path destination_file("destination.txt");
try {
fs::copy_file(source_file, destination_file);
std::cout << "File copied successfully!" << std::endl;
}
catch (const fs::filesystem_error& e) {
std::cerr << "Error copying file: " << e.what() << std::endl;
}
return 0;
}
Is this a good way of doing it?
Anyone know what buffer sizes are being used behind the scenes?
Thanks.
|
|
|
|
|
|
mike7411 wrote: Is this a good way of doing it?
1. You want to check what happens if different drives are involved.
2. You want to verify paths are supported.
3. Catching one type of exception ignores possible other ones. Probably unlikely but in case.
mike7411 wrote: buffer sizes are being used behind the scenes?
There are all sorts of possible buffers. Disk, OS, library.
Only concern however for that is speed. You can profile it. You can also use a OS command shell call for comparison.
If it matters, at least in my experience, OS shell commands will always be faster. This is especially true when copying directories. Seems reasonable given that the copy operation in the OS doesn't involve loading the data into the application.
Even so if you need it to be 'fast' for some reason then I would suggest that you need to change your requirements/design. Copying files, in general, is always 'slow'. Speed doesn't matter for single small files. So only matters for very large files and/or large numbers of files. But those will always be 'slow'. And there can be error conditions that make it even slower (which your code does not account for.) So attempting to guarantee a speed rate is never going to work.
|
|
|
|
|
I am in Microsoft Visual Studio Community 2022 (64-bit).
I noticed that I can use printf even without including stdio.h.
It works if I do this:
#include <iostream>
Any idea why this works?
Thanks.
|
|
|
|
|
The chain of include files can be long and tortuous. In this particular case:
iostream -> istream -> ostream -> ios -> cstdio -> stdio.h
Should you rely on compiler include chains? Depends if you expect your code to be compiled with a different compiler and how long the chain is. In this particular case, there is a strong probability that all C++ iostream operations will end up like C function calls so you are pretty safe.
Mircea
|
|
|
|
|