C :: Writing Binary Data Or Reading From A File
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
ADVERTISEMENT
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
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
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 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
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
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
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
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
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
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
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
Dec 6, 2013
Following is the program I wrote it basically takes 9 inputs and then save them into binary file. then print out the data stored in binary data and find inverse of it then print the inverse out. but its stuck in a loop somewhere.
Code:
#include <stdio.h>
int main() {
int a[3][3],i,j;
float determinant=0;
int x;
FILE *fp = fopen ("file.bin", "wb");
[Code] .....
View 6 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
Oct 16, 2013
I have a file of data where each line consists of 4 parameters (degree, alpha, beta and x) and a value corresponding to these, organized in increasing order of the parameters. Here's the test file I'm using:
0 1 1 0.5 3.5
1 1 1 -0.5 -0.5
1 2 0 0.2 4.5
1 3 0 0.2 -4.5
2 0 0 1.5 2.1
2 1 0 0 5.6
My code is supposed to look for a certain set of parameters (currently set to 1 2 1 1.5, which is not contained in the file) and its value. If it's there (i.e. the file contains the value for the right parameters) the program is done. If not it should calculate the value using a function JacobiPoly() and insert the wanted parameters and corresponding value in the appropriate place in the file.
I have a code in which I think everything works except the writing to file. I use an fstream so I can both read and write and open it with ios::out||ios::in. I'm also aware I need something similar to fseek in between input and output but according to [URL] .... reading and writing to the same file using the same fstream, seekp() should suffice. I'm using seekp(), but the output (in the form inout << (...)) doesn't work.
Here's the code:
fstream inout;
inout.open("inouttest.dat", ios::out | ios::in);
int degree=1, readDeg;
double alpha=2, beta=1, x=1.5, value, readAlpha, readBeta, readX, readVal;
bool foundVal=false;
streampos beforeRead=(streampos)0;
[Code] ....
View 3 Replies
View Related
Dec 7, 2014
I am attempting to read a ppm file. When i do it i try to write it back in another file just to see how's done and i get a terrible result. I assume the problem is something with the casting i do to the variables.
This is my image class
Image:: Image(unsigned int width, unsigned int height, bool interleaved) {
buffer = new Component [3*width*height];
this->height=height;
this->width=width;
this->buffer=Component();
[Code] .....
View 4 Replies
View Related
Aug 7, 2013
I'm currently working on a server for handling clients in a 2d online game and I wrote some regular fstream file code for handling the file that stores their information and I was about to implement it into the server, then I realized there might be a problem having it open multiple times concurrently, so I googled it and came up with
posts like
[URL]
I'm wondering if I can just treat it like everything else or will I have to do something specific for opening on multiple threads?
p.s. I did read those posts but I'm very new to multithreading
View 16 Replies
View Related
Jan 22, 2014
I'm having issues with writing to a file. more precisely, i'm trying to determine if a set of numbers are in the file. if they are already in it, don't write to the file. if not, write to the file. My problem is my program is writing all numbers, even duplicates. The sort is working.
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
//Counter Stats
int STR;
[code].....
View 9 Replies
View Related
Aug 21, 2014
I would like to write a complete structure array to a file and read it back, recovering all the data. I have tried the following:
Code:
#include <stdio.h>
#include <string.h>
#define NUM 256
const char *fname="binary.bin";
typedef struct foo_s {
int intA;
int intB;
char string[20];
[Code]...
//---------------------------------------------------- but the mac field is reading back some random value repeatedly. Why is that? And how do I fix this?
View 8 Replies
View Related
Jan 15, 2015
I am working Visual C++ 2012. I am trying to read a GLOBE file using the following code.
#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include "stdlib.h"
#include "string.h"
#include "cmath"
#include <errno.h>
using namespace std;
/* Conversion useful */
[Code] ....
I am getting the error in the if condition (bold).
View 12 Replies
View Related
Mar 21, 2013
Im programming client/server app that client provide the file name then the server send it to client then the client will save it ..this is part of code in client
Code:
char buffer[1024];
printf("FIle is being downloaded ...
");
printf("%s
",buffer);
}
[code],...
So i have 2 problems ::
1st one is when i write to file the file permission i cant define it with data type mode_t ,so the file does not open at all after creation...
2nd one is: the data in buffer is less than 1024 ,the data wrote to buffer but with garbage data . How to make the file read only the real data with garbage ??
View 5 Replies
View Related