C/C++ :: Reading Binary Numbers From File?
Feb 9, 2014I wanted to know if it is possible to read binary numbers(0's and 1's) from a file(text,picture, anything), and see them in the compiler.
View 14 RepliesI wanted to know if it is possible to read binary numbers(0's and 1's) from a file(text,picture, anything), and see them in the compiler.
View 14 RepliesThis is supposed to read a series of 16 bit binary numbers as short ints in the array memory. However, it only appears to be reading the first number.
Code:
short int memory[256];
void readFile(){
//read input file into int memory[]
FILE *ifp;
ifp = fopen("proj1a.dat", "r"); //input file
[Code] ....
How to store values from a .txt file delimited with semicolons (;) into a class which is then stored into a Binary Search Tree. After browsing Google for a few hours, and trying various examples of people using Vectors, I just can't seem to get my program to work using Object Oriented Programming with an instance of the class Person.
My two classes are Person, and BinarySearchTree as follows:
class Person{
private:
string first_surname;
string second_surname;
string name;
int ID;
[Code] ....
Ok so my text file saves the data of each person in the same order as the class with each value separated by a semicolon.
i.e. First_Surname;Second_Surname;Name;ID;Telephone;Score;
void fillTree( BinarySearchTree *b) {
string input[7];
Person p;
fstream file("scores.txt", ios::in); // reads text file
if(file.is_open()) {
[Code] ....
I understand that I get an error because a vector is saved as integers, and I am using strings, my question is, any other way to read the .txt file and save each data separated by a semicolon, into the Person class?
I have a file in which I have written a structure called "Record".
Here is the struct definition:
#define IDSIZE 10
struct Record{
char id[IDSIZE];
int score;
};
[Code]....
My program writes a vector to a file in binary. I want to erase the vector, then read the file to repopulate the vector then display.
Basically I want to erase the RAM memory and be able to use the file as memory. This is my current code but it is not reading the file correctly. I have checked the file to see that the writing part is working.
void read_from_file(vector<Info> &vector)
{fstream file;
file.open("info.dat", ios::binary | ios::in);
if (file.fail())
{
cout<<" FILE DOES NOT EXIST ";
system("pause");
[Code]...
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] .....
Below is the code for reading a struct that was stored in a binary file. Problem is while reading from file I get the name without first character and age is always equal to zero which it should not be.
#include <iostream>
#include <conio.h>
#include <fstream>
struct abc {
char name[100];
int age;
[Code] .....
I'm reading a binary file and I want to read all of the letters, but I have noticed that it doesn't read all of the letters and I think it is because of appears as data.
View 11 Replies View RelatedBoth 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] .......
My code is working all except one part I'm just completely stuck on and it's probably something simple. There are 14 numbers in the file. The way I have the code now it will tell me there are 14 which is correct but it displays my low number as 0 and it should be 6. If I change if(infile) to if(infile >> num) it will read the correct low number as 6 except now it says there's only 13 numbers and it's not reading the first number in the file which is 23 so it skews the whole program.
#include <iostream>
#include <fstream>
/*This program pulls numbers from a file and displays various information such as the sum of the numbers, average, highest and lowest. */
using namespace std;
int main() {
ifstream inFile;//Declare the variables including the input file stream.
[code]....
The program runs fine but i just want it to read the inputs from a text file rather than user manually entering it! what changes should i make in the codes..
Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
float friction,flow;
[Code] ....
I think it comes from having to align the data in the output file. How to start this is to use a function to read in all the characters using a while loop. Then read in all the digits in a separate function. When it comes to outputting the data in the correct format I'm lost, so for now if I could figure out the functions that would be awesome. A final note we have not gotten to using strings so the I'm trying to use getc, fgetc, and ungetc.
Write a program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a students first name, then one space, then the students last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program will read data from this file and write its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number at the end of each line: the average of the students ten quiz scores.
The output file must be formatted such that first and last names appear together in a left justified column that is 20 characters wide. Each quiz score should be listed in a right justified column that is 4 characters wide, and the average should appear in its own right justified column that is 10 characters wide.
Note that if a student has fewer than 10 scores, the average is still the sum of the quiz scores divided by 10; these students are assumed to have missed one or more of the quizzes. The output file should contain a line (or lines) at the beginning of the file providing appropriate column headings. Use formatting statements to make the layout clean and easy to read.
I need to open a file that has multiple lines that look something like this, " black box 100.01 33.5 78.93". What this shows is the coordinates of the black box. Lines will have different objects with different coordinates. I need to create a loop that will read this file and tell me when a black box is found by displaying a message. I don't need to know how to create this file. I don't need to display the entire file but rather have it search for black boxes.
View 3 Replies View RelatedI am reading certain int's at a time from one int number stored in a file. I'll explain. I am working on an ezpass project and basically I have to store an int in a file, and from that file, write a program that separates numbers into information.
For example:
the number 204601324 is stored in a file. I know how to open a file from terminal, but the issue is reading the individual numbers. The first number is weight class (2). My program has to display that number as the Variable "weightClass." how do I get it to do that? In addition, miles allowed is the number "0460." How do I get that number to display as the variable "oMiles?"
I am trying to read numbers stored in a txt file to populate an integer array. The problem is that the numbers read from a file have to be treated as strings and then I am not able to put them in an integer array.
I tried the stoi function to convert the string in 'box' into an integer but I am getting an error.
Code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
int main() {
int amount=10;
int k[amount];
[Code] ....
How do I get c++ to read a file containing six numbers - a1, b1, a2, b2, a3, and b3 - that are constants for the following equations:
clubangle(degrees) = a1 + b1*0.85*clubnumber
clublength(inches) = a2 + b2*1.05*clubnumber
clubspeed(yards/s) = 1.1 * (a3 + b3 * swingnumber) * (clublength(inches)/40)^2;
I am attempting to read in a file that has 4128 sets of 21 numbers separated by commas and write it into an array. I now know that in order to use fseek, I have to make my array a character array, but I need my function to read in decimals (ex: 0.172635). I'm reading in
0.102598,0.000000,0.000000,0.000000,0.000000,0.307 793,0.000000,0.410391,0.102598,0.000000,0.102598,0 .102598,0.000000,0.000000,0.102598,0.102598,0.8207 83,0.000000,0.000000,0.000000,0.000000
and keep getting numbers like 48 49 50.
Code:
void CSread(char filename[100], char array[22], char array2[22], unsigned int arraysize)
{
char genename[32];
double temp = 0;
FILE *CSfile;
CSfile = fopen(filename, "r");
");
[code].....
I am working on a project for school that has us read in a text file that contains 8 lines of SSN and grades. I am not sure how to go about reading in the lines and differentiate between the SSN and grades in the calculations.
View 1 Replies View RelatedI am trying to make a program that will convert a list of binary numbers from a file into decimal and print the decimal to the screen. I have no problem doing the conversion, the problem comes up when our teacher wants the input file in a format as such:
3
10110101
11111111
10101010
The first number is supposed to tell the program how many different 8bit strings it is going to have to convert, and then the following lines are those binary numbers.
I am not very experienced with file inputs, and I know how to open files and read lines in.. The problem is, how to say "ok the first line says 3, so now I have to convert the next 3 lines" . I am assuming it is just a simple loop that I am missing....
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] .....
My binary file size is 88200000 bytes, but each element should have 2 bytes. So when I use "infile.read( block , size)" , should I be using the size of the file or number of elements (44100000)?
And should block be resized to the number of bytes or the number of elements?
ifstream::pos_type size;
std::vector<int> block;
ifstream infile(filename.c_str(), ios::in|ios::binary|ios::ate);
[Code] .....
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]....
Windows 7, 64 bits, Visual Studio 10.
I have a problem to read a large number of binary files, process them and store them under a new name. The program and routines go very well for 505 files. After reading 506 files, the program now refuses to read the next file. I have 16 Gb of memory and tried to close all other programs and restart the PC. it always stops after 506 files (512 files would be more understanding in a way...).
Here is my code. I have tried many things without success. This is only part of the loop that stops. The if test if (myfile.is_open() returns false by some reason. I can start the process again starting with the file that does not open and then it stops again after 506 files.
char * tfiBlock;
ifstream myfile (OrigFilename, ios::in|ios::binary|ios::ate);
if (myfile.is_open()) {
int lengde = myfile.tellg();
tfiBlock = new char [lengde];
//static char memblock [size];
[Code] .....
Clean up procedure:
delete[] tfiBlock;
Are there any limits to how many files that can be opened, or is it maybe someting to be set in the compiler?
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.
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>.
I have written in a binary file using fstream an using sort value.
unsigned shortwidth=50,height=50;
file.write((char*)&width,sizeof(width));
file.write((char*)&height,sizeof(height));
When i try to read it back from fstream again there are some symbols (binary obviously). How can i get my values back? I want to read those symbols and in a way to convert them to my old width and height values.