C++ :: Displaying A Struct From A File (binary Mode)

Sep 28, 2013

I have a file in which I have written a structure called "Record".

Here is the struct definition:

Code:

#define IDSIZE 10
struct Record{
char id[IDSIZE];
int score;
};

Here's the code where I wrote to the file:

Code:

Record record;
char* id = "H12345678";
int score = 50;
record.id = id;
record.score = score;
file.write((const char*)&record, sizeof(record));
}

Here's a screenshot of the file in windows: [URL].... To the left is the id, 9 characters. To the right, well I'm assuming that's the score that I wrote.

And here's the problem, reading from the binary file:

Code:

Record record;
fstream file(argv[1], ios::in | ios::binary);
if(!file){
cerr << "Could not open the file." << endl;
return 1;
}
char* id = new char[IDSIZE];

[Code]...

The ID reads perfectly. The score...always returns 0, despite that it should show "50".

View 4 Replies


ADVERTISEMENT

C++ :: Writing Data On A File In Binary Mode Multiple Times

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

C++ :: Reading A Struct From A File (binary)

Sep 28, 2013

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]....

View 1 Replies View Related

C++ :: How To Read Struct From Binary File

Oct 12, 2014

I have this file want to read it source code [URL] ....

View 10 Replies View Related

C/C++ :: Getting Info From Binary File Into A Struct

Apr 19, 2015

Your goal is to open this struct of students (the binary file). Once open, the first job is to print out each student. Afterwards, display all students with a gpa of 3.5 and higher. Finally, display all students with the major of Computer Science or Game Programming. Be sure to title each category of items.

He gave us a .cpp file and a .h file that we're suppose to use to run the program. He doesn't want us to change his original code, except we can add the needed code to run his code (i.e. Struct & Main).

My problem is that I get an Abort() crash message in VS2013 when I try to run it. Now, in line 95 of the cpp file if I replace the char* with "", it runs fine showing the GPA with blank entries for the other print parameters. I've read the Binary Files chapter of the Pearson textbook several times and it doesn't have anything to say about working with char pointers.

Header file:

#pragma once //Professor's code
struct StudInfo //Start of my code {
char* fName;
char* lName;
char* major;
double gpa;

[Code] ....

View 7 Replies View Related

C :: Write Array Of Struct To A Binary File

Mar 6, 2015

im trying to write an array of struct to a binary the array of struct is filled with data from a text file .

the program gives no errors or warnings but does not write anything at all to the binary file here is the main

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct matches
{
char teamA[20] ;
char teamB[20];
int scoreA ;
int scoreB;

[Code]....

View 6 Replies View Related

C++ :: Write Struct With String In Binary File

Jan 11, 2015

I have a structure :

struct Entrainement{
string muscleCible, nom, unitesObjectif;
double objectifActuel, dernierObjectif, objectifInitial, capaciteInitiale, progression[10];
};

and I want to write in a binary file the structure. The following works perfectly....

(*pointeurFichierEntrainementBin).write((char*)&exercice, sizeof(Entrainement));

except when the string exceed 11 characters. I guess it's because it has to pick a fixed sized for the string? but what if I want to always be able to have string up to 200 character? because now I can't exceed 11..I know writing a string with c_str() works, but I would like to write/read the structure in one shot.

View 1 Replies View Related

C++ :: Error In Reading Struct From Binary File

May 20, 2013

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] .....

View 3 Replies View Related

C :: Parsing Binary Data File By Casting A Buffer - Accessing Double From Struct

Jan 4, 2014

I am parsing a binary data file by casting a buffer to a struct. It seems to work really well apart from this one double which is always being accessed two bytes off, despite being in the correct place.

Code:

typedef struct InvoiceRow {
uint INVOICE_NUMBER;
...
double GROSS;
...
char VAT_NUMBER[31];
} InvoiceRow;

If I attempt to print GROSS using printf("%f", row->GROSS) I get 0.0000. However, if I change the type of GROSS to char[8] and then use the following code, I am presented with the correct number...

Code:

typedef struct example {
double d;
}

example;
example *blah = (example*)row->GROSS;
printf("%f", blah->d);

View 7 Replies View Related

C/C++ :: Displaying Array Elements Inside Struct

Feb 13, 2015

How can I display the elements of an array inside struct something like this

struct ABC {
double a[3];}
void show( const ABC & x );
ABC x{18, 'W', { 1.1, 2.2, 3.3 }};
show(x); would output {18, ‘W’, {1.1, 2.2, 3.3}}

View 10 Replies View Related

C/C++ :: Displaying Binary Tree Diagram?

Nov 15, 2014

I am making a Binary Tree program for a Data structures class proyect. The program consist of displaying a Binary tree diagram and also to give the inorder,postorder and preorder of the diagram. I made the program program give me the inorder,postorder and preorder of the binary tree. However I am having problems with the display. I am trying to use breadth first search for the display, and I made the funtion for BFS but I am stuck on how to proceed.My code is the following:

#include<stdlib.h>
#include<stdio.h>
#include<iostream>

[Code].....

View 1 Replies View Related

C++ :: Displaying Last TWO Largest In A Binary Search Tree?

Dec 12, 2013

How would I display the last two largest nodes in a binary search tree recursively?

View 1 Replies View Related

C++ :: Using Binary Write To Save A Struct

Aug 13, 2013

I'm still reading Prata's book and there is a section where they explain how to write files using the binary mode.

They define a struct

Code: const int LIM = 20;
struct planet
{
char name[LIM];
double population;
double g;
};
planet pl;

And they say you can use the binary mode to write the whole struct at once (in text mode you need to specify every member).

Code: ofstream fout("planets.dat",
ios_base:: out | ios_base::app | ios_base::binary);
fout.write( (char *) &pl, sizeof pl); I don't understand why they use Code: fout.write( (char *) &pl, sizeof pl) instead of just Code: fout.write( pl, sizeof pl)

View 4 Replies View Related

C :: Binary Search Tree - Struct Data

Oct 24, 2013

I was working with binary search tree and came up with the solution:

Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct data {
int x;
struct data *left;
struct data *right;

[Code] .....

View 6 Replies View Related

C :: Binary File Write By User Input Then Printing Binary File Data Out

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

C++ :: Opening Files In Hex Mode To Compress File Size

Sep 10, 2014

I have an idea which i will try to implement in C++,I want to compress file size through making my own numbering system which is 80-based number, I do really want to know whether this even possible ? I learnet that Hexadecimal uses symbols like A, B, C, D, E, F to represent 10,11,12,13,14,15 -- and that's what i want to do to my own numbering system but in a bigger scale .if possible: How can i use C++ to open a file in Hex mode ?

View 6 Replies View Related

C++ :: Displaying File The Same Way It Was Made?

Apr 18, 2014

If I create a file that looks like this for example,

car 2 4 6 8
truck 1 2 3 4 5 6
plane 4 5 6 7 9

Whenever I go to open it back up like this,

ifstream inputFile;
string line;
inputFile.open("file.txt");
while (inputFile>>line) {
cout << line << endl;
} inputFile.close();
return 0;
}

It displays output like this,

car
2
4
6
8

How can I get it to display like above?

View 2 Replies View Related

C++ :: Displaying A Text File (Echo)

Jul 19, 2013

So I'm really stuck trying to figured this bug on the program that is preventing me from displaying the text of my program..

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>

using namespace std;
int main () {
ifstream infile;
ofstream offile;

[Code] ....

View 3 Replies View Related

C++ :: Manually Loading And Displaying BMP File

May 25, 2012

I want to display a bmp file of resolution 600*420 (using c compiler like code block ) by manual loading. I tried to display the image by loading the pixel value. but this method has a limitation that it take up pixel value of image upto 64kb size.

So now i am trying to load it manually. What are the steps that i has to follow, what are the header files that i has to use ?

My system specification are windows xp professional , intel pentium 4 cpu. I don't know where to start ,does this method has any limitaton ,...etc ........

View 2 Replies View Related

C :: Displaying A String Loaded From A Text File

Jan 19, 2014

I'm working on a program that can load all words from a dictionary "English2.txt" (it's attached to the post), then put every word into a 2-dimensional matrix (every line is reserved for one word) and display them. After loading every word into a matrix, when I try to display first 8 words with printf, I can't do it without '' at the end of a line. Otherwise only the 8th word is displayed... What's more, when I try to read the length of the first word with strlen, it says, that it has 4 characters (in fact there are only 3 and it's the word "AAA"). What could be the reason for that?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int main()
{
FILE *fp;
char buffer[32];

[Code]...

View 4 Replies View Related

C++ :: Code For Displaying Text File Not Working

Dec 6, 2013

i have turbo c++ 3.0 installed . my program is compiled without error and is running. but when i choose option to display scores in the consol , it hangs . check my code. i have to run this on same compiler. i just want to display all the contenets of text file .

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

[Code].....

View 4 Replies View Related

C# :: Adding And Displaying Data From A Text File

Mar 6, 2014

Is it possible to display data from a textfile and also save data in to a textfile, reason being is for me to create back-ups of data into a textfile hidden somewhere in a safe location in the local disk or server in case of database error and failure.

View 3 Replies View Related

C# :: Intellisense Not Displaying All Element Property File

Jan 28, 2015

It's a shock to me seeing the partial display of variables being picked by intellisense. I am working in VS2013 environment. I have a project, in the project Settings file I created string variables of about 100 to be accessed across the files for the project, like this:

A example of thew variable it worked for

public static String TradeportAccordionSystemAdminInstitutionManagement = Settings.Default.[b]TradeportAccordionSystemAdminInstitutionManagement[/b];

An example of the variable it did not work for

public static String TradeportAccordionSystemAdminInstitution = Settings.Default.

So, does it mean that the Settings file has a limit of variables that should be declared in it?

View 2 Replies View Related

C++ :: How To Save Struct To File And Then Read It Back From The File

Aug 5, 2013

How to save the struct to file and then read it back from the file ?

Code:
#include <conio.h>
#include <stdio.h>
struct student {
int person;
int egn;
float AvergeGrade;

[Code] ....

Have average grade: %f", person[i].FirstName, person[i].LastName, person[i].egn, person[i].AvergeGrade);
}
//Save to file
getch ();
}

View 2 Replies View Related

C/C++ :: Sizeof (struct) Returns 6 More Bytes Than Actual Struct Size?

Sep 14, 2014

#include <stdio.h>
#define MAX_USERS 20
struct {
char ID[10];
char Name[40];
int Pos;

[Code] .....

I was attempting something weired with address to move data around when I discovered that the size of the array is not what I expected. I am passing this structure as &Users to a function that declares it as a void *, then I can deal with chunks of data (memmove) and not have to worry about index or things like that. However...sizeof is returning something I do not understand.

View 9 Replies View Related

C :: Writing A Struct To A File

Sep 28, 2013

I am trying to write a structure to a file. Example say the structure has two variables x and y . I want to write a function which modifies these variables and stores the modified version on a file. Such that next time I call the function . it has the values from the previous write. Here's an example of my code .

Code:
// initialize the structure struct->x = 0, struct->y = 0
File *fp = fopen("filename", "r+");
struct MYSTRUCT mystruct = (struct MYSTRUCT*)malloc(sizeof(MYSTRUCT))

//check
fread (mystruct, sizeof(MYSTRUCT), 1, fp);

// do some calculations.
fwrite(mystruct, sizeof(MYSTRUCT), 1, fp);
fclose(fp)

//return some value
}

The problem is that each time I run the program it shows the initialized value of the variables and not the value from last write. I guess the write isn't successful because when I open in w+ mode. i get the error file could not be opened and then i have to delete the file and re create it....

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved