Are there any issues reading directly into an std::string? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I've seen in code a direct read into std::string where the contents are intended to be interpreted as a string as follows:
std::string note;
note.resize(n);
read( &note[0], n );
Assume that n is of a fixed size, as in a parsing scenario.
Are there are any issues with reading directly into a string? I have seen a lot of uses of ifstreams, but it seems excessive in this case.

First, if it's a text file made by several lines, I find the std::string class not a good choice as a "container"; I would prefer just a std::vector<char>, or if you want to do some additional parsing and break the file into its single lines, a std::vector<std::string>.
I'd also pay attention to the encoding used by the file: is it UTF-8? Is it some other char-based encoding?
For example, if the file is UTF-16, reading it as a raw sequence of bytes into a std::string would be very misleading (and bug prone).
Moreover, it's important also to pay attention to the size of the file. If you have a gigantic text file (e.g. 5GB) and you are building a 32-bit Windows application, your code won't work (as 32-bit processes on Windows are limited to 2GB by default). In such cases, reading the file content in smaller chunks (or using memory-mapped file techniques with smaller "views" on the file) may be a better alternative.

Look at it this way: “What's the worst that could happen?”
Are you obtaining a file from the local user? And if they supply a file that's too big, perhaps their machine will thrash, or even kill your program with an out-of-memory error?
Do you expect the user to do that often enough to worry about?
Alternatively:
Are you obtaining the file from a network source or untrusted user? Would giving that user the ability to potentially thrash your system or kill your application constitute a risk?

Related

How to properly store an array of constant data? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
My question is particularly pointed towards what's neat and efficient and won't get me made fun of as a programmer.
So basically I want to store a huge file of names of PNGS. So of course I'd use an array of strings in order to store all those names. But my question is should I store these strings in something like a binary file? Or should I just put them straight onto the script? I personally hate the idea of using a text file to store them but what do I know.
If I were to put them on a script then it would look something like this:
const std::string tileFile[textureAmount] ={
"Grass.png",
"Dirt.png"
};
But this just looks really stupid to me for some reason. How would one handle a situation like this more professionally?
A character is 1 byte, whether you write a text file or a binary file. The only savings would be if you did some encoding or compression to it, and text does compress nicely since not all the bits in the chars are usually used.
But one pragmatic rule for programming is, "remember the power of plain text". Binary is nice, but if you don't need it, don't use it. Text is easier to work with, to read, to understand when it is corrupted, to use with other tools, and so on. If you can map the data into memory, use binary. If you're going to gzip it or something like that, use binary. If you're just storing the data and it's not excessively big, then I'd recommend a nice, easily parsed text file.
And don't feel ashamed of it. :)

What are memory mapped files? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Recently, I've come across this video that shows how to use mmap() with file io. However, I can't find the video of his that documents the function. I don't have an understanding of what it is, why it exists, nor how it relates to files.
Too much of the jargon is flying over my head to make sense of it. I had the same problem with sites like Wikipedia.
Files are arrays of bytes stored in a filesystem.
"Memory" in this case is an array of bytes stored in RAM.
Memory mapping is something that an operating system does. It means that some range of bytes in memory has some special meaning.
Memory mapped file is generally a file in the file system, which has been mapped by the operating system to some range of bytes in memory of a process. When the process writes to the memory of the process, the operating system takes care that the bytes are written to the file, and when the process reads from the memory, operating system takes care that the file is read.

Is my company doing this right, sharing data between exes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
First off, my company is into power grid, not IT, so software is kinda a secondary role here.
I work on a power system simulation software, very old code base in C++ with MFC, like 15 years old. What we do is take large amounts of data, ~100,000 floating point values then format and write to a text file (most of the code actually uses the C FILE structure to do this). Then, it's read by a separate engine exe which computes the electrical algorithm (Electrical algorithms are mostly numeric solutions of system of diffn equations) and then writes back huge amount of data to another text file, which we read and update the UI.
My question is, is this how it should be done? It there a way to skip writing into the text file and directly pass the data to the exes?
exes are called using CreateProcess() MFC function.
EDIT::
Sorry, site won't let me comment.
#Vlad Feinstein Well, yes, it's like a Ladder. A thing called load flow solves power flow through the lines, which in turn will be used to find stability of the systems, which in turn for overvoltage ect. It's huge, the UI is million+ lines of code, engine exes another million maybe.
Doesn't MFC already implement IPC using Dynamic Data Exchange? I can pass strings to another process's PreTranslateMessage() func. A scaled up version of that?
There is no such a thing as "should be done as ..." there are multiple methods to do IPC and while the method you describe might not be the fastest, it is a viable solution nevertheless. If the performance doesn't bother you in this particular case you should not bother with modifying it. It is exactly the case where the phrase "if it ain't broke, don't fix it" applies.
Probably, you would not want to make any new IPC in the application that way, though.

Why is it not possible to erase contents from a file in C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
The title pretty much says it all, but still to elaborate:
I would have understood if the language would have restricted me from adding new contents to a file (irrespective of position) because it would lead to fragmentation.
But what I do not understand is why it is not possible to:
Erase contents from the last line, similar to backspacing from EOF. [ASCII/BINARY]
Erase contents from middle portion of file [ASCII/BINARY]
Replace text in a file with some other text of same size [ASCII]
Replace data in a file with some other data of same size [Binary]
Does any other language support this?
EDIT: To do this in C++, you need to read the file, perform the modifications on variables, then create a new file. The question was why it is not possible to edit the "original" file instead of creating another file.
Does any other language support this?
C++ supports it. You can seek and you can write. But the fact that this is a very uncommon scenario to replace the same exact byte length and the fact that replacing a bunch of bytes with another bunch of bytes that has a different length is horribly inefficient to a point were writing a new file is faster, lead to the language and functions that exist.
Other languages may handle this differently, but I have yet to learn one that actually does. Because file handling does not change across languages and all language and framework writers came to the same conclusions: files are not the best medium for insertion and deletion of data. Arrays are not the best medium for insertion and deletion, files just being one them.
If you need to insert and delete, use another container (lists come to mind). If you need to persist this container to disk, do so after all manipulation is done.
Looks like you were expecting lowish-level access to files to magically work like some word processor / text editor application. But they don't do this, either! They simply abstract away from the user all the complicated mechanisms involved in editing and re-committing a file to disk.
In writing C++ source code, you are now taking responsibility for implementing those mechanisms. It's not quite as simple as dumping a bunch of backspace characters at EOF. :)

Writing large amount of data to an ascii file fast in C++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am writing a particle tracker in C++ which releases a particle in a numerical flow field and then integrates in time to calculate the new position of the particle.
This is done for as many particles as time allows (hopefully millions) for approximately 10k timesteps per particle. During the simulation I generate a double array1[10k][10] and int array2[10k][4] which represent the 14 characteristics of each particle at each timestep.
My question is how to write such a 'matrix' to a notepad++ readable .txt file (for the purpose of post-processing). Both arrays are to be merged in the file such that a [10k][14] matrix is formed.
Personally I have only had experience with fprintf but Visual Studio appears to have some security issues with that function. Other functions I have seen are streams (more secure I suppose) and fwrite.
Could someone enlighten what the advantages and disadvantages are of the similar functions? And possibly what the better choice is for my problem (if there is one).
Kind regards,
EJG
Newer Microsoft compilers give you various "security" warnings if you do not define _CRT_SECURE_NO_WARNINGS. See here and here
The _s versions it will suggest using instead are microsoft specific, so if you ever wanted to compile your code for another platform you would need to re-write.
There are other ways to write to files in C++. You could consider using a C++ std::ofstream instead.
Or, you can just write to std::cout and stream the program output to a file
myprogram.exe > output.txt
If you want to use fprintf, just add #pragma warning(disable:4996) at the top of your source file and it won't complain about you using it.
Otherwise you can always use fprintf_s() or whatever the alternative is that VS provides.