When using Boost::mapped_file, why does the size of the file appear to cap at 65,356 bytes? [closed] - c++

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
For my program, I am using boost::mapped_file to memory map a file that I want to access for a pattern detection algorithm. In order to test the construction of the mapped files, and the following execution of my algorithm I have been testing the mapping of the file by specifying the size of the file that I want, creating the file and filling it with random characters, and then mapping that file. The problem is that I have run into a weird error, and I am not quite sure what is happening.
The error I am getting is that once my file reaches size=65,536 bytes, when attempting to use the boost::mapped_file_source::size() method, the return value is 0. Any file sizes that I try to use after this are just an incremented version of 65,536 meaning that a file size=65,538 returns a size of 2.
I was curious if this had to do with the alignment of the mapped file, which I saw referred to here. However, when I called the boost::mapped_file_source::alignment() method, it returned 4096, which I don't understand. What does alignment mean, and how does it play into this problem?
Also, why does boost::mapped_file_source::size() return 0 when the file_size>65,536 bytes? I know that 65,536 is equal to the alignment value (4096) * 16, but I don't understand why.

You should show the relevant code. Docs
explicit mapped_file_source( const std::string& path,
size_type length = max_length,
boost::intmax_t offset = 0 );
Specifically, if length is not specified it will use the filesize.
Boost IOStreams does NOT have the limitation you describe, as you can easily show:
Live On Coliru
#include <boost/iostreams/device/mapped_file.hpp>
#include <iostream>
#include <sys/stat.h>
#include <fcntl.h>
static constexpr auto PATH = "path";
void test(std::size_t length) {
int fd =::creat(PATH, 0600);
if (::ftruncate(fd, length) || ::close(fd))
::perror("whoops");
boost::iostreams::mapped_file_source m(PATH);
std::cout
<< m.size() << " = "
<< std::hex << std::showbase << m.size() << "\n";
}
int main() {
test(0XFFFF);
test(0X1FFFF);
test(0X2FFFF);
}
Prints
65535 = 0xffff
0x1ffff = 0x1ffff
0x2ffff = 0x2ffff

Related

is there something wrong with array.size? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I want to give in a string into an array from keybord, so that i can count how many letters it has with for loop
I tried to buil and run the code but i doesnt work actually and there an error but I dont really understand the error and the way to fix them. The error is :
request for member size in Bao( my Array), which is of none class type
Here is my code:
char Bao[100];
cout<<"Give me a sentence"<<endl;
cin.getline(Bao, 100, '\n');
cout<< Bao.size()<<endl;
There is no function 'size' in a c++ array. You have to use
sizeof(array)
which gives the size of the array in bytes. To get the true size of the array use
sizeof(array)/sizeof(array[0])
which divides the size of the array with the size of a single element in the array giving you the number of elements.
Also, why are you using an array in this instance? It seems like a string might be what you are looking for.
Here, you shouldn't even be using an array. Use std::string.
Here's some sample code that accomplishes the same thing as the code you posted:
std::string sentence;
std::cout << "Give me a sentence" << std::endl;
std::cin.getline(sentence, 100, '\n');
std::cout << "Scentence is " << sentence.size() << " bytes" << std::endl;
EDIT: using getline() for the spacebar bug.

How to organize infinite loop with symbols analysis in it? [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 4 years ago.
Improve this question
I need to organize infinite loop with symbol analysis in it. In C I used fgets(buf, N, stdin), suppose buf is buf[10]. User could type string of any length and I could analyze it by breaking down the input and examining parts of length 10. How can I implement this in C++ without using C libraries. Sorry for my English if you can't understand what I mean
In C++ you should std::cin to read from standard input.
// #include <iostream>
do
{
char buf[10]{}; // create array of 10 bytes filled with zeros.
std::cin.read(buf, 10); // read 10 bytes
// at this point you should check if std::cin.read succeeded.
// otherwise you will be reading zeros.
std::streamsize numRead = std::cin.gcount(); // obtain number of read bytes.
std::cout << numRead << " " << buf << std::endl; // some printing.
}while(std::cin);

C++ command line argument verification [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If provided with command line argument argv[], what is a way to determine whether or not that input is a file name.
E.g. If we are entering ex.txt into the command line, and printing out the contents, how can I write a conditional statement to determine whether the input for argv[1] is correct?
Thanks. Let me know if I was too vague. This is my first post, and english is not my first language.
You probably don't want to know only whether the file exists but also if you can open and read it. The only reliable way (And, by the way, the only way currently supported by standard C++.) to do this is to try opening the file and see if you succeed.
#include <fstream>
#include <iostream>
int
main(const int argc, const char *const *const argv)
{
for (int i = 1; i < argc; ++i)
{
std::ifstream istr {argv[i]};
if (!istr)
{
std::cerr << "error: " << argv[i] << ": cannot read file\n";
continue;
}
// Do something with the stream...
}
}
Be aware that if you close the file after verifying that it is good, there is no guarantee that it will still be good if you try to open it again later. Some other process could have deleted it or taken your permissions to read it.

search string in textfile [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 7 years ago.
Improve this question
Say you have a text file like this
all you want from the text is the "size value"
but repeats more that thousand time with different value
"field_pic_flag : 0
bottom_field_flag : 0
idr_pic_id : 0
Found NAL at offset ,size 28813 !! Found NAL"
"field_pic_flag : 0
bottom_field_flag : 0
idr_pic_id : 0
Found NAL at offset ,size 210 !! Found NAL"
Results
i just want a code to write a text file with this format as shown below
size 28813
size 210
and so on
If using C/C++ is not mandatory, then grep is probably your friend :
grep "size [0-9]*" -o yourfile.txt > all_sizes.txt
And if you only need the 50 first results, head it is :
head -n 50 all_sizes > result.txt
(Now, this assumes you're using some kind of Unix, or OS X ...)
I saw that the question is tagged as C/C++. If you're allowed to use C++ 11, than you can easily use the new introduced regex library.
In the code bellow the data is stored directly in a string, but you can easily read it from a file. The results is saved in the result.txt file, been displayed on the screen at the same time.
#include <iostream>
#include <string>
#include <regex> // The new library introduced in C++ 11
#include <fstream>
int main()
{
const int n = 50;
int i = 0;
std::ofstream outputFile;
outputFile.open("result.txt");
// You can read this string from your input file
std::string s("\"field_pic_flag : 0\
bottom_field_flag : 0\
idr_pic_id : 0\
Found NAL at offset, size 28813 !!Found NAL\"\
\"field_pic_flag : 0\
bottom_field_flag : 0\
idr_pic_id : 0\
Found NAL at offset, size 210 !!Found NAL\"");
std::smatch m;
std::regex e("size [0-9]*");
while (std::regex_search(s, m, e) && i++<n) {
for (auto x : m) {
std::cout << x << " ";
outputFile << x << " ";
}
std::cout << std::endl;
outputFile << std::endl;
s = m.suffix().str();
}
outputFile.close();
return 0;
}
A classical solution will require a little more effort.

Issue in file operations in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
This is part of a c++ code that writes value of a vector of strings into a file.
int main () {
//freopen ("out.txt", "w+", stdout);
ofstream data;
data.open("data.txt");
BinaryTree<string>* bt = new BinaryTree<string>;
LoadBinaryTree(bt);
fillArrayOfNodes(bt);
for (int i = 0; drawArray[i] != "\0"; i++)
data << drawArray[i] << endl;
data.close();
delete bt;
return 0;
}
First, I couldn't write into the file. I mean after running the program and checking the output file, it was empty. after that, I noticed that my output format wasn't right. I changed it and now I can write into the file. (the code shown above is the modified code)
The problem is the way you're attempting to iterate through the array. The Standard C++ string class std::string should not be handled like a regular char array. That is, you shouldn't base your condition upon finding the null character. The correct way would be to iterate until you reach the length of the string.
Moreover, you should be using a vector of strings and inserting strings using push_back():
std::vector<std::string> v;
// fill vector with push_back()
for (int i = 0; i < v.size(); ++i)
data << v[i] << endl;
You need to include the right headers like <fstream>.
Try this example: http://www.cplusplus.com/doc/tutorial/files/