Replacement for sync() command [duplicate] - c++

This question already has an answer here:
Sync File System command for windows [closed]
(1 answer)
Closed 8 years ago.
I need to port one app on Windows. Originally that app was written on Linux and it uses Linux specific commands. I stacked at one place with sync(). Windows doesn't have such utility. The code looks like
QSettings *data
...
data->setValue("some_var", var);
data->sync();
sync();
That is a peace of C++ file. I don't know C++. It was written not by me. I use other languages. So how can I make it work on Windows or how can I rewrite that part?

Basically you can ignore the system-specific sync() call. It's not needed, even on linux. QSettings does the right thing for you.

If you have access to all the files you have open, I believe the equivalent of calling sync() on Linux is the same as going over all the file handles and flushing them on Windows, probably by using FlushFileBuffers().
EDIT 1
If you're using the C file interface (since you came from Linux), fflush() is your friend (you still need to have access to all open files.)
EDIT 2
I see there is a _flushall() call you can use. Not sure about its similarity to Linux's sync() but they generylly seem to do the same thing. I'm also a little wary about using functions that start with underscore.

Related

Programming and platform dependance for c++ questions [duplicate]

This question already has answers here:
How to write portable code in c++?
(12 answers)
What is "Portable C++"? [duplicate]
(2 answers)
What is meant when a piece of code is said to be portable? [closed]
(3 answers)
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
Not sure if this is the right place to ask this:
How do programs written in c++ run on other computers if you don't write them specifically to do that? I saw something about not just sending the .exe, but also sending other things with it?
Is there a high level programming language that is as fast or nearly as fast (in run speed) as c++ while also being platform independent?
See above.
You compile your code for all the platforms you target and deploy a number of executables. Hence, Write once, compile anywhere.
C++ allows you to write portable source code. So assuming you write portable code to start with, you can compile it for some target platform, and run the resulting binary on that target.
Now, depending on what your program uses, you may have to package other "stuff" with the executable. What you mention ("I saw something about not just sending the .exe, but also sending other things with it?") would arise if your program used some dynamic link libraries that were not part of the OS (presumably Windows, based on the mention of .exe). But, it's kind of up to you to decide whether to use a library that's packaged as a DLL or not. If you don't want to package DLLs with your executable, don't use them (but sometimes, you may decide it's less trouble to use and package the DLL than do without).
As far as another language goes...doesn't really make a lot of difference as a rule. If you write code that depends on something else, you have to satisfy that dependency on the target computer. Some languages require you to add some DLLs to support the language itself, but most C++ compilers don't. On other OSes, those dependencies won't be called "DLLs", but most reasonably modern OSes provide something similar.

Is it possible to decompile a C++ executable file [duplicate]

This question already has answers here:
Is it possible to "decompile" a Windows .exe? Or at least view the Assembly?
(16 answers)
Is there a C++ decompiler? [closed]
(5 answers)
Closed 4 years ago.
I lost the source code to an executable file but still have the actual file. Is there any way to retrieve the original C++ code?
Duplicate of this question here.
Yes, it is possible, however when it comes to peeking function bodies and the like, you might have a little less luck. Operating systems like Kali Linux specialize in de-compilation and reverse engineering, so maybe look into a VM of that. And of course, windows has a lot of applications you can use as well to check the application code.
Look over the other question for specific app suggestions. :)
Edit : You will most likely have lost all your logic and function bodies, but you might be able to recover the overall structure. It's your EXE so you might be more familiar with how it was all connected up.
You cannot get the original source code but you can decompile the binary into source code using tools given in this similar question: Is there a C++ decompiler?
The output source code will not look like the original as the compiler will have optimised the original source when generating the executable.
Short answer NO.
Long answer, because C++ doesn't use some intermediate code like C# or Java you cannot decompile the app in some readable format. But if you can read assembly maybe you can save some time.

In C++ code, how do I get the user to input a number in between 2 strings of text [duplicate]

This question already has answers here:
How do I input variables using cin without creating a new line?
(7 answers)
Closed 5 years ago.
Suppose we have the following sentence:
The last time I shopped was __ days ago.
How would I change this into C++ code, so that __ is an integer that the user inputs?
So far, I have come up with the following, which puts the first string, the integer input, and the second string on separate lines:
#include <iostream>
using namespace std;
int main(){
cout << "The last time I shopped was ";
int x;
cin >> x;
cout << "days ago";
}
I would like to get all of this onto the same line - how do I do this?
You can't do that in standard C++11 (or C++14 or C++17), without any additional and system specific library.
You need a system specific library that handles an editable line (in the terminal where you are running your program). Standard streams in C++ don't have that (and standard C++ don't even know about terminals; in many cases, such as command pipelines, or redirections, or shell scripts running in batch or as a cron job, you might not have any).
This answer suggests something for Windows (but it probably is not robust since it doesn't handle all cases, such as programs running in batch or with redirections).
On Linux and POSIX systems, you would use something related to tty-s (see the tty demystified and termios(3)). You might use isatty(3) to detect if you've got a terminal. Then you could use a library such as ncurses or readline. BTW, these libraries might have been ported to Windows too (I leave you to check that).
Another approach might be to write some GUI application (showing a window where some parts are editable, perhaps as a GUI form). Then you'll need a GUI toolkit library like Qt.
Or you could make your program a web application. Then (assuming you know some web technologies and at the very least understand the HTTP protocol and HTML5) use some HTTP server library like Wt or libonion (and your user would use his web browser to interact with your application, which becomes some specialized web server)
PS. Whatever approach you choose, it is more complex than you might believe and would require at least several days of work (first, reading documentation to become familiar with the library you want to use).

Calling external files (e.g. executables) in C++ in a cross-platform way

I know many have asked this question before, but as far as I can see, there's no clear answer that helps C++ beginners. So, here's my question (or request if you like),
Say I'm writing a C++ code using Xcode or any text editor, and I want to use some of the tools provided in another C++ program. For instance, an executable. So, how can I call that executable file in my code?
Also, can I exploit other functions/objects/classes provided in a C++ program and use them in my C++ code via this calling technique? Or is it just executables that I can call?
I hope someone could provide a clear answer that beginners can absorb.. :p
So, how can I call that executable file in my code?
The easiest way is to use system(). For example, if the executable is called tool, then:
system( "tool" );
However, there are a lot of caveats with this technique. This call just asks the operating system to do something, but each operating system can understand or answer the same command differently.
For example:
system( "pause" );
...will work in Windows, stopping the exectuion, but not in other operating systems. Also, the rules regarding spaces inside the path to the file are different. Finally, even the separator bar can be different ('\' for windows only).
And can I also exploit other functions/objects/classes... from a c++
and use them in my c++ code via this calling technique?
Not really. If you want to use clases or functions created by others, you will have to get the source code for them and compile them with your program. This is probably one of the easiest ways to do it, provided that source code is small enough.
Many times, people creates libraries, which are collections of useful classes and/or functions. If the library is distributed in binary form, then you'll need the dll file (or equivalent for other OS's), and a header file describing the classes and functions provided y the library. This is a rich source of frustration for C++ programmers, since even libraries created with different compilers in the same operating system are potentially incompatible. That's why many times libraries are distributed in source code form, with a list of instructions (a makefile or even worse) to obtain a binary version in a single file, and a header file, as described before.
This is because the C++ standard does not the low level stuff that happens inside a compiler. There are lots of implementation details that were freely left for compiler vendors to do as they wanted, possibly trying to achieve better performance. This unfortunately means that it is difficult to distribute a simple library.
You can call another program easily - this will start an entirely separate copy of the program. See the system() or exec() family of calls.
This is common in unix where there are lots of small programs which take an input stream of text, do something and write the output to the next program. Using these you could sort or search a set of data without having to write any more code.
On windows it's easy to start the default application for a file automatically, so you could write a pdf file and start the default app for viewing a PDF. What is harder on Windows is to control a separate giu program - unless the program has deliberately written to allow remote control (eg with com/ole on windows) then you can't control anything the user does in that program.

Implementing a File Object (C++)

I've been looking over the Doom 3 SDK code, specifically their File System implementation.
The system works (the code I have access to at least) by passing around an 'idFile' object and I've noticed that this class provides read and write methods as well as maintaining a FILE* member.
This suggests to me that either the FILE* is 'opened' with read and write access or the file is closed and reopened (with the appropriate access) between calls to Read() and Write().
Does this sound correct or am I over simplifying it?
If this isn't the case (which part of me suspects it isn't - due to speed etc.) does anyone have any suggestions as to how they would achieve this elegant interface?
Please bare in mind that I am fairly new to both C++ and stdio (which I'm pretty sure iD favours).
You can open a FILE* in read-write mode.
If you do that, you should flush and seek to a known location when changing between reading and writing, but you don't have to reopen the file.
Without ever having looked at the Doom code (I'm guessing you can specify a mode when you create the object), you can use freopen() to re-open a file (in a different mode, if you want) without closing it first.