C++ :: Reading And Writing Variable Binary Width Data
Mar 8, 2013
How to read and write an arbitrary number of bits from/to a file stream.
For instance, how to repeatedly read 9 bits from a file, then change to 10 bits, then 11 bits, and so on?
Obviously one way is by doing a lot of bit shifting, and masking. But honestly, I'm too dumb to get it right. Then I thought about using std::bitset and std::vector<bool>.
View 5 Replies
ADVERTISEMENT
Mar 6, 2013
I am having problems either writing data to a binary file or reading from the file. Through the process of elimination I am posting the code where the data is written to file to see if I can eliminate that as an option. I know the data is being processed correctly because, through the use of another function, I can view the data.
I also know that fwrite must be including some padding because the file size ends up being 576 bytes after it is written instead of 540 bytes (the size it would be if no padding is used). Here is my struct:
Code:
typedef struct {
char teams[25];
float wins;
float losses;
float pct;
int runsScored;
int runsAgainst;
} STATISTICS;
Here is where I initialize it:
Code:
STATISTICS stats[12] = {
{"Anaheim Arrays", 0.0, 0.0, .000, 0, 0},
{"Pittsburg Pointers", 0.0, 0.0, .000, 0, 0},
{"Indianapolis Integers", 0.0, 0.0, .000, 0, 0},
[Code] ....
And here is the function that writes my data. The sA array is only used to change the scheduled games based on the variable week.
Code:
void schedule(STATISTICS stats[]) {
FILE *f;
int sA[12], week = 0, runsPerGameA = 0, runsPerGameB = 0, runsAgainstA = 0, runsAgainstB = 0;
int index, a = 0, b = 1, i = 0;
[Code] .....
View 13 Replies
View Related
Apr 2, 2013
I am writing a double number in Binary format to the console of program A (in FORTRAN) with the following code:
Code:
REAL*8 A
A = 12.54
INQUIRE(6, name = xstring)
OPEN(1,file=xstring, access='stream',action='write')
WRITE (1) A
CLOSE(1)
And trying to read that number by program B (in C++) which is connected to program A by anonymous pipes. Following is the reading part of program B:
Code:
#define BUF_SIZE 5000
BOOL bSuccess = FALSE;
char Buf[BUF_SIZE];
DWORD dwRead;
for (;;) {
bSuccess = ReadFile( V_hChildStd_OUT_Rd, Buf, BUF_SIZE, &dwRead, NULL);
if( ! bSuccess || dwRead == 0 ) break;
}
Note: V_hChildStd_OUT_Rd is a handle to the output of program A.
After running the program although bSuccess becomes TRUE, Buf array does not include the number (12.54) that I am expecting. If I do the same process without using the binary format it works fine and I can read the number. I know somethings wrong with the writing or reading of binary data but I do not know what it is.
View 14 Replies
View Related
Jun 5, 2014
why I'm giving "Access violation reading location 0x336827B8" and also I was able to read my data but it's giving me weird stuff. I want to write the sorted grades and the average in a new disk file. so here's my code so far here's my code
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int avg(int sum, int size);
void swap(int *, int *);
[code]....
View 5 Replies
View Related
May 30, 2013
Both arrays arr and pointers are the same size. I am having problems reading pointers from file into a new int array.
FILE* ky_pt=fopen("stashedclient","ab");
write(fileno(ky_pt), pointers, sizeof(pointers) );
pointerindex=lseek(fileno(ky_pt), 0, SEEK_CUR );
printf("pointerindex after writing array %d
[Code] .......
View 2 Replies
View Related
Aug 16, 2013
I a want to write a code to convert a string into binary data for that i wrote a code its working perfectly but there is one problem , some of the binary data is written in 7bit and i want to convert it to 8 bit by adding 0 to the last.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
[Code]....
View 2 Replies
View Related
Jan 19, 2015
I am trying to make a project using arduino to control several power surces by time, i don't want to have an lcd, that would be easyer but i don't have enought space on my project, i want to set the time and the timing for the relays via Serial
Of curse i could set it using the serial monitor on the arduino ide, but i want to make a simple program to make the pc comunicate with the arduino and make it simpler to set the time and similar stuff. How to use one of my usb ports to print and read serial data?
View 1 Replies
View Related
Jul 9, 2014
fstream ifs;
ifs.open ("data.str", ios::binary | ios:ut);
char *data1 = "data1";
char *data2 = "data2";
ifs.write(data1, strlen(data1));
ifs.write(data2, strlen(data2));
when i this,data2 is not going under data1, i thought each write starts on a new line?
View 4 Replies
View Related
Jan 5, 2015
Code to write data(Double type e.g 12345.67891) in text file like pattern given below. Remember to put tab between each column.
-----------------------------------------------------
Column1 Column2 Column3
Value 1 Value 2 Value 3
Value 4 Value 5 Value 6
Value 7 Value 8 Value 9
----------------------------------------------------
& Also how to read data from this file.
View 3 Replies
View Related
Oct 20, 2013
When you have to write data on a file in binary mode multiple times (without closing the file), is the put pointer left where you ended writing the last time, or do you have to use tellp() or seekp() again and again to make sure you write at the right place?
I would have the same question about the get pointer, does he stay in place after you're done reading something (without closing the file, of course), or do you have to set it back at the right place with seekg() ??
View 5 Replies
View Related
Feb 23, 2013
I am using visual studio 2012.....in below code i m writing data in to a test.txt file but i dont know with which key file stop accepting char...i tried ctrl+z and ctrl+d but not working ....
Code:
#include<stdio.h>
#include<conio.h>
main(){
char ch;
FILE *ptr;
[Code] ....
View 7 Replies
View Related
Mar 14, 2013
It said my width1 is being used wihtout being initialized.. what does it mean?
#include<iostream>
using namespace std;
double Area(double height, double width);
double Perimeter(double height, double width, double height1, double width1);
[Code] .....
View 2 Replies
View Related
Sep 24, 2014
I am trying to set the width of the data values which the user will input when using the program but I don't know how to get it to show the values when I tryto set the width of the variables in a nice column .
//Programming Assignment 4
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main() {
double q1r1, q2r1, q3r1, q4r1;
double q1r2, q2r2, q3r2, q4r2;
double q1r3, q2r3, q3r3, q4r3;
[Code] ....
View 2 Replies
View Related
Feb 11, 2013
Provide the definitions for the following class declaration and then write a program that demonstrates usage of each member function using the test data of 10 for width and 8 for height. Remember to put your main program, class header and class source into separate files. This is an easier version of the exercise than the one you have been asked to practice. Only provide code for the methods specified in the class declaration. Note that the displayRectangle() method should output an appropriate ‘box’, its dimensions, area and perimeter to the screen.
class Rectangle {
public:
Rectangle(int height, int width);
~Rectangle(void);
[Code] .....
View 1 Replies
View Related
Jul 17, 2013
So, I was reading that article on binary files:
Disch wrote:
u16 ReadU16(istream& file) {
u16 val;
u8 bytes[2];
file.read( (char*)bytes, 2 ); // read 2 bytes from the file
val = bytes[0] | (bytes[1] << 8); // construct the 16-bit value from those bytes
[Code] .....
u32 would be the same way, but you would break it down and reconstruct it in 4 bytes rather than 2.
But, I don't know what he means by the part in bold text.
View 8 Replies
View Related
Mar 10, 2014
So I'm trying to write an array of integers to a binary file. He's my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
[Code].....
I know that it is an array of characters right now, and I will be using the reinterpret_cast when I finish my program. Anyways, when I run the executable, it only writes 1234 to the file. My assumption was that the sizeof() was not being set properly, but even manipulating that won't fix it.
View 6 Replies
View Related
Nov 3, 2014
I'm writing a program using Huffman algorithm to compress text file. I have tested my program by just printing the printing ASCII character to file and it worked fine. However, now I have to implement using bits and my program doesn't work. It seems like I'm not reading or writing the right bits. Here is the result of my testing:In the input file I put abc the input file to compress it. Then I uncompress it the out out is aaa. Below is a snippet of how I read and write bits
Code:
class BitInput {
istream& in; // the istream to delegate to
char buf; // the buffer of bits
int nbits; public:
BitInputStream(istream& s) : in(s), buf(0), bufi(8) { }
[Code] ....
View 4 Replies
View Related
Sep 19, 2013
I have a text file containing 500 signed decimal numbers. My task is to read each entry and convert into a 16-bit 2's complement representation (binary number) and write into the another text file.
View 2 Replies
View Related
Mar 19, 2013
Code:
#include <stdio.h>
struct hardware{
int recNum;
char toolName[30];
int quant;
double cost;
[Code] .....
I'm having issues with my functions working properly. I'm not sure where I'm going wrong with them and if i'm using the correct mode for the fopen.
View 6 Replies
View Related
Feb 20, 2015
I have to write a program that sorts names and grades from a file and output them to another.
For example:
Name: Peter Parker
Exam 1: 95
Exam 2: 90.625
Exam 3: 91.20
Name: Mary Smith
Exam 1: 65
Exam 2: 79.1234
Exam 3: 70.24
Becomes something like this in the output file:
Name: Parker, Peter
Average Score: 92.28
Grade: A
Name: Smith, Mary
Average Score: 71.45
Grade: C
I know I'm supposed to read the whole file, but I'm getting really confused on how to take the name of each student separately without recording Exam 1, 2, and 3. I'll be able to do the average score and grade on my own.
View 2 Replies
View Related
Apr 30, 2013
Is there a way to read and write to the same file?
I'm writing a game program and I want to save the score at the end of the game to a txt file. The txt file already contains other game scores. How do I store the score without overwriting the previous scores.
I'm just using basic fstream.
View 2 Replies
View Related
Oct 28, 2013
I have to make a program that asks for your username (any)to register it. When you type it it will say something. I want to create a txt file also. so this is what I have:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int a;
{
ofstream myfile;
[Code] ....
now, how can I use the cout again to print something "Welcome to the program..."?
View 1 Replies
View Related
Nov 3, 2014
I'm writing a program using Huffman algorithm to compress text file. I have tested my program by just printing the printing ASCII character to file and it worked fine. However, now I have to implement using bits and my program doesn't work. It seems like I'm not reading or writing the right bits. Here is the result of my testing:In the input file I put abc the input file to compress it. Then I uncompress it the out out is aaa. Below is a snippet of how I read and write bits
public:
BitInputStream(istream& s) : in(s), buf(0), bufi(8) { }
/** Read the next bit from the bit buffer.
* Return the bit read as the least significant bit of an int.
*/
int readBit(){
int i;
if(nbits == 8){
buf = in.get();
[Code] .....
View 2 Replies
View Related
Jan 16, 2015
I'm messing around with reading and writing files. The first file creates a small txt file. Simple enough
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string name;
string desc;
[Code] ....
It does what it should. It creates a text file "items.txt" .... It reads as such:
dagger,a dagger,15,10,0,1,3,1,0,0,0,1,0,0,0,0,0,1
The second file is meant to read the file and place the data back into the variables. This happens, but the data crams itself into the first variable, and the rest of them collect the trash that's in memory.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string name,desc;
[Code ....
I need to get "dagger" into name, "a dagger" into description, and each value with their perspective variable. I'm sure I need some type of "separator". Hopefully I can use the comma. Before it's over, I will have about a hundred items that will need to be read into a class of items.
View 4 Replies
View Related
Feb 17, 2014
cant write to binary file
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int number;
} something;
void main() {
int numbers[10]={1,2,3,4,5,6,7,8,10,12};
[Code] .....
View 2 Replies
View Related
Dec 10, 2013
program that I am working on. I want to use fgets() in my program so I could handle multiple words from a text(to be able to handle spaces). I get a weird result when running the program.
Code: #include <stdio.h>
#include <stdlib.h>
//#include "kim.h"
#include <string.h>
[code]....
View 4 Replies
View Related