Operations on file | get and put pointer - c++

I have some questions on manipulating on a file ;
a.) I am a bit confused about get and put pointer in c++. Do I show correct position of get pointer and put pointer.
MyFile . seekg ( 0 , ios :: beg ) ;
MyFile . seekp ( -10 , ios :: end ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
__________________________________________________________________
^ ^
^ ^
^ ^
get Pointer put pointer
Myfile . get ( character ) ;
MyFile . write ( SomeString, 4 ) ;
MyFile . flush ( ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
__________________________________________________________________
^ ^
^ ^
^ ^
get Pointer put pointer
i.) Are Seekg and seekp always guarentee that get an put pointer always shows correct position ?
ii.) If you know more about this topic, can you show/give me some point(s) I should be careful when I use them, ( if there is )
b.) Is
FileIN . seekg ( 1, ifstream :: cur ) ;
equal to
FileIN . seekg ( 1, ios :: cur ) ;
Platform : linux
File format : binary

a) It's wrong. File streams maintain one file pointer, for both input and output. Both seekg and seekp do the same thing. The reason there are two different functions is that the interface of iostreams is generic, it can be used for devices which do have separate put and get pointers.
Quote from the standard [filebuf]:
In particular:
— If the file is not open for reading the input sequence cannot be read.
— If the file is not open for writing the output sequence cannot be written.
— A joint file position is maintained for both the input sequence and the output sequence.
b) Yes, they are the same.
EDIT:
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
MyFile . seekg ( 0 , ios :: beg ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
MyFile . seekp ( -10 , ios :: end ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
Myfile . get ( character ) ;
// you must sync/flush if your last operation was input and you switch to output,
// or your last operation was output and you switch to input.
MyFile . sync ( ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer
MyFile . write ( SomeString, 4 ) ;
MyFile . flush ( ) ;
index :0 1 2 3 4 5 6 7 8 9 10 ... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
_____________________________________________________________________
^ file-pointer

Related

itpp soft demodulation

I have a problem with the output of the soft demapper function (demodulate_soft_bits) when using 16-QAM and 64-QAM modulation.
here is a portion of the code (64-QAM):
cvec comple;
Modulator_2D qam;
ivec cont_d = "0:1:63";
vec qam_r = "-7 -7 -7 -7 -7 -7 -7 -7 -5 -5 -5 -5 -5 -5 -5 -5 -3 -3 -3 -3 -3 -3 -3 -3 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7";
vec qam_i = "-7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7";
comple.set_length(qam_i.length(),false);
for(int i=0; i<qam_i.length(); i++)
comple(i) = std::complex<double>(qam_r(i)/sqrt((double)42),qam_i(i)/sqrt((double)42));
qam.set(comple, cont_d);
temp = qam.demodulate_soft_bits( symb_recus,channel,1);
symb_recus is the output of a realistic 4x4 MIMO channel
channel is the channel gain
At the output I got a random values which doesn't correspond to the hard output and I got also a succession of 0
exp :
temp : [6.80 3.33 1.64 -14.27 -7.06 -3.51 0 0 0 0 0 0 -3.47 .....]
Hard output : [ 0 1 0 1 1 1 1 1 0 0 1 1 0 ... ]
can you please help me

Need help reading from a file until end of line

I'm working on a a project for my class where I have to use all four algorithms for the maximum subarray sub problem (cubic, quadratic, linear, and recursive). My problem is that I'm supposed to read the input from a file and I'm having trouble figuring out how to read until the end of a line, execute code with that data, and then move onto the next line. The input file looks like this:
2
-5 -10 -2 -4
2
-2 10 -5 -6
3
-10 -5 0 5 -20 20 -50
4
10 8 2 -20 -50 -100 -150
0
-1 -2 -3 -4 -5
1
-100 -200 0
4
200 500 -700 1000 2000 -5000 4 10
4
100 200
6
0 0 0 0 0 0 -10
5
-4 10 -3 200 500 -700 2
0
5 10 15 20 25 30 35
2
10 30 50 70 100
3
-15 16 23 -30 0 -2 13 8 6 0 -4 -15
12
9 8 23 -1 -23 -4 0 0 4 7 9 10 -23 68 1 -2 -3 -6 -19 10 5 1 1 2 4 3 -46 12 -100 78 -23 0 0 12 2 7
5
-1 -3 0 2 3 6 1 -5 -3 -2
now my issue is that I want to be able to read in a single number (the minimum subarray length) and then fill the array with the row of numbers below it, then execute all four algorithms using that data, then I want to move on to the next minimum subarray length and the next array of values below it, execute all four algorithms etc., and I want to be able to do this all in one go.
For example, I want to read 2 as the minimum subarray length, then use (-5,-10,-2,-4) as the array, execute all four algorithms, then use 2 as the minimum length, (-5,10,-5,-6) as the array, execute all four algorithms, and keep doing that until the end of the file.
Here is a quick possible solution that might help ("test.txt" contains your values...)
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
ifstream in("test.txt");
string str;
int value;
while (in)
{
vector<int> vec;
getline(in, str);
stringstream stream(str);
while (stream)
{
stream >> value;
vec.push_back(value);
}
vec.erase(vec.end()-1);
//Here the vec conntains all values of the current line
for (auto v : vec)
cout << v << " ";
cout << endl;
}
}

Efficient algorithm for counting frequency of numbers in an intervals

I need to build a bar gragh that illustrate a distribution of pseudorandom numbers that determined by linear congruential method
Xn+1 = (a * Xn + c) mod m
U = X/m
on the interval [0,1]
For example:
Interval Frequency
[0;0,1] 0,05
[0,1;0,2] 0,15
[0,2;0,3] 0,1
[0,3;0,4] 0,12
[0,4;0,5] 0,1
[0,5;0,6] 0,15
[0,6;0,7] 0,05
[0,7;0,8] 0,08
[0,8;0,9] 0,16
[0,9;1,0] 0,4
I used such a method:
float mas[10] = {0,0,0,0,0,0,0,0,0,0};
void metod1()
{
int x=-2, m=437, a=33, c=61;
float u;
for(int i=0;i<m;i++){
x=(a*x + c) % m;
u=(float)x/(float)m;
int r;
r = ceil(u*10);
mas[r] = mas[r] + 1;
}
for(i=0;i<10;i++) cout<<"["<<(float)i/10<<";"<<(float)(i+1)/10<<"]"<<" | "<<mas[i]<<"\n-----------------"<<endl;
return;
}
If you know another officient methods for this problem, that are not straitforward, i would appreciate it.
Your code currently has a much larger problem the efficiency. Assuming you've defined mas as something like int mas[10];, it has undefined behavior.
To see the problem, let's modify your code to print out the values of r that it generates:
void metod1() {
int mas[11] = { };
int x = -2, m = 437, a = 33, c = 61;
float u;
for (int i = 0; i < m; i++) {
x = (a*x + c) % m;
u = (float)x / (float)m;
int r;
r = ceil(u * 10);
//mas[r] = mas[r] + 1;
std::cout << r << '\t';
}
// for (i = 0; i < 10; i++) cout << "[" << (float)i / 10 << ";" << (float)(i + 1) / 10 << "]" << " | " << mas[i] << "\n-----------------" << endl;
return;
}
Then let's look at the results:
0 -2 -7 -4 -7 -7 -6 0 -6 -1
-5 -6 -1 -9 -7 -2 -7 -3 0 -6
0 -8 -5 -6 -8 -6 -7 0 -2 -6
-7 -6 -2 -4 -9 0 -4 -5 -1 -2
-5 0 -2 -1 -4 -8 -5 -2 -8 -5
-9 -4 -5 -7 -9 -8 -3 -9 -9 -9
-3 -4 -5 -3 -9 -6 -5 -3 -1 0
-5 -5 -6 -7 -9 -5 -4 -1 -5 -1
-9 -2 0 -9 -6 -7 -5 -5 -3 -3
-9 -3 0 -4 -1 -1 0 -8 -4 -4
-2 -7 0 -6 -6 -8 -4 -8 -2 -8
-8 -2 -4 -7 -1 -6 -1 -3 -7 -3
-5 -9 -8 -5 -8 -7 -4 -1 -8 -7
-7 -2 -9 -5 -3 0 2 8 8 2
6 8 7 1 5 2 8 4 1 5
10 1 3 6 4 10 5 6 6 10
[more elided]
It doesn't look like you've planned for the fact that you'll be producing negative numbers, and if you fail to do so, the result is undefined behavior when you index outside the bounds of mas.

std::getline only reading one line:

I took some code from http://www.cplusplus.com/doc/tutorial/files/ in order to read the character data from a file:
string line;
ifstream myfile ("example.txt");
if (myfile.is_open()) {
while ( getline (myfile,line) ) {
cout << line << '\n';
}
myfile.close();
}
If I create the file with emacs and put in the text data myself, I get exactly what I expected: The entire file is written. But if I try to read in the data from the file I want to read, then I only read the first line.
File1.txt, which doesn't work, has the following hex dump (output truncated):
0000000 1 9 9 8 0 1 0 2 , 9 3 6 , 1 3 .
0000010 3 3 4 5 , 1 3 . 3 3 4 5 , 1 3 .
0000020 3 3 4 5 , 1 3 . 3 3 4 5 , 1 3 4
0000030 8 9 6 , 4 , 0 , 5 \r 1 9 9 8 0 1
0000040 0 2 , 9 3 7 , 1 3 . 3 3 4 5 , 1
0000050 3 . 3 4 5 7 , 1 3 . 3 3 4 5 , 1
0000060 3 . 3 4 5 7 , 1 4 9 8 8 . 5 , 4
0000070 , 0 , 0 \r
File2.txt (which does work) has the following hex dump:
0000000 1 4 0 3 8 0 , 3 2 5 0 0 , 2
0000010 1 4 0 1 4 \n 1 3 1 3 4 , 2
0000020 3 4 8 , 2 3 4 . 0 2 3 , \n 1 2
0000030 3 4 , 1 2 3 4 8 , 1 2 3
0000040 . 3 9 2 4 \n
So clearly one file has a different end-of-line char than the other, but how can I get past this? I'm using MacOSX.
\r is not the default delimiter.
You can set the delimiter for "getline", in the third argument.
getline (myfile,line, '\r');

M-x align-regexp removes text?

Here's an example text I'm trying to format:
(9 12 19 38 0 -39 -20 -13 -10)
(7 9 14 29 0 -30 -15 -10 -8)
(4 6 9 19 0 -20 -10 -7 -5)
(2 3 4 9 0 -10 -5 -4 -3)
(0 0 0 0 0 0 0 0 0)
(-3 -4 -5 -10 0 9 4 3 2)
(-5 -7 -10 -20 0 19 9 6 4)
(-8 -10 -15 -30 0 29 14 9 7)
(-10 -13 -20 -39 0 38 19 12 9)
I would like each column to align on digits, (i.e. if there's a minus sign, add extra space).
If I do it like so: C-uM-xalign-regexpRET\([[:digit:]]+\)RETRETRETy
I get this:
(9 12 19 38 0 -3 -2 -1 -1)
(7 9 14 29 0 -3 -1 -1 -8)
(4 6 9 19 0 -2 -1 -7 -5)
(2 3 4 9 0 -1 -5 -4 -3)
(0 0 0 0 0 0 0 0 0 )
(-3 -4 -5 -1 0 9 4 3 2 )
(-5 -7 -1 -2 0 19 9 6 4 )
(-8 -1 -1 -3 0 29 14 9 7 )
(-1 -1 -2 -3 0 38 19 12 9 )
which is very close, but not what I want.
And if I try to modify the expression to include the minus sign, like so: \(-?[[:digit:]]+\)
Then I get this:
(9 1 1 3 0 - - - -)
(7 9 1 2 0 - - - -)
(4 6 9 1 0 - - - -)
(2 3 4 9 0 - - - -)
(0 0 0 0 0 0 0 0 0)
(- - - - 0 9 4 3 2)
(- - - - 0 1 9 6 4)
(- - - - 0 2 1 9 7)
(- - - - 0 3 1 1 9)
Is this a bug, or is there something I don't know?
The text matched by the designated group (usually group 1) is expanded or shrunk, so non-whitespace characters in the group are subject to deletion, as you saw. Unless, that is, justification is enabled, which is indicated by supplying a negative group number to align-regexp:
C-uM-xalign-regexpRET\(\s-*-?\)[0-9]+RET-1RETRETy
If you want, you can align the columns on the ones digits of each number by including the digits in the match group:
C-uM-xalign-regexpRET\(\s-*-?[0-9]+\)RET-1RETRETy
In either case, an extra space will be inserted after each opening parenthesis. I don't see any way to keep align-regexp from doing this, but if you do it often you could wrap it in a command that does the align-regexp, then replaces the regexp "^( " with "(" everywhere in the original region.