How to organize infinite loop with symbols analysis in it? [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 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);

Related

When using Boost::mapped_file, why does the size of the file appear to cap at 65,356 bytes? [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
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

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 write correctly into multidimentional char array unknown amount of values but fixed amound of chars [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'm sick and tired of solving why my ch[0] is of value "Thomas EdisonÇ#", when it should be "Thomas Edison"
int main(){
using namespace std;
ifstream in("U2.txt");
int n;
in>>n; //n=rows, so in every line there will be "name surname", time, money
char ch[n][21]; //I'm trying to get Name+Surname which must be 20 char long
in.read(ch[0], 20);
cout << ch[0]; //but getting Thomas EdisonÇ#
return 0;}
It works on one dimentional ch[21], but there's gonna be lots of values so I want to use ch[n][21]
Any other out of my box solution is welcome, I'm tired
You are forgetting that C strings need to be nul terminated
in.read(ch[0], 20);
ch[0][20] = '\0'; // add the nul terminator
cout << ch[0]; // now correct output

Add integers to array using cin simultaneously in C++ [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 5 years ago.
Improve this question
Hellow guys i want to add nine integers to array at once time without pressing enter key in run time. please guys tell me how to add nine integers to array simultaneously in C++.
Thanks!
If you want to process each integer value right after its input in console is complete (e.g. in that a blank indicates that the next integer value shall begin), you are in a bad position.
The reason is that terminal input (beyond of what your C++ program can influence) often is buffered, and even cin might not receive any character until Enter or EOF is pressed in the terminal.
There may exist workarounds like conio.h or ncurses, but the are not standard and probably not worth the effort in your situation unless you really need to implement integer scanning for a production environment tightly connected to console input.
Try it out and compare input taken directly from console to input from a stream that is already "filled" with enough input:
int main() {
stringstream ss("12 34 56 78 90 10 11 12 13");
//istream &in = ss; // would output each integer immediately.
istream &in = cin; // will probably wait for enter before processing begins.
int value = 0;
for (int i=0; i<9; i++) {
if (! (in >> value))
break;
cout << value << "; ";
}
}

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.