C++ :: Read And Write Multiple Class Objects From File - Fstream Won't Work Correctly

Apr 28, 2014

So I have a small program which is supposed to write and read multiple objects from a file, but for some reason it doesn't write the information when I use "fstream" to create the object from the fstream class, but it does when I use "ofstream". Also, it doesn't read the information from the file no matter if I use "fstream" or "ifstream". I watched a video where this exact code worked just fine, but it just won't work when I run it. I'm using Dev C++ 4.9.9.2, I don't know if that has anything to do with it, I also tried it with Code::Blocks, didn't work either.

Here's the code.

#include <iostream>
#include <fstream>
using namespace std;
class Person

[Code].....

View 3 Replies


ADVERTISEMENT

C/C++ :: How To Write Class Objects To Text File Using Fstream

Jan 15, 2014

I need to a simple example for writing class' objects to a text file using fstream , I tried to search the forums and I found binary examples but not for to a text file.

View 8 Replies View Related

C++ :: Write And Read Class Objects In Binary File?

Jan 11, 2013

if i have 2 variables for which values are given by the user,then,does the information get stored into the file in the name of the variable,or just like packs of information.....if the former is true,how to extract the information of a particular variable alone from the whole file?

View 4 Replies View Related

C++ :: Fstream Doesn't Work In Combination With Linked Lists

Jan 1, 2014

Im about to program a RPG and, of course, the player has got an inventory. The items in this inventory are stored in a linked list and have IDs in form of strings.

player.cpp: (just the most relevant)

#include <list>
#include <string>
using namespace std;

[Code] ....

Of course, when the player saves the game, the inventory has to be saved too.

game.cpp(just the m. R.)
#include <fstream>
using namespace std;
class CGame{

[Code] .....

Now, in my code I write the linked list into the file savegames.sav

void CGame::save(CPlayer* player){
m_out.open("savegames.sav", ios::out);
m_out.write((char* ) &player->m_inventory, sizeof(player->m_inventory));
m_out.close();
}

In another function i load the list:

void CGame::load(CPlayer* player){
m_in.open("savegames.sav", ios::in);
m_in.read((char* ) player->m_inventory, sizeof(player->m_inventory));
m_in.read();
}

And when i access the loaded linked list with the .empty() function or so, Windows says the following:

<Mygame.exe> funktioniert nicht mehr (in English: doesnt run anymore).

I tried so much to fix that but i dint reach my aim.

View 2 Replies View Related

Visual C++ :: Can't Make Maze Work Correctly With Arrays

Apr 30, 2014

I have to make a maze with arrays, but i cant seem to move down or up or at all. Here is what I have so far

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

[Code].....

View 5 Replies View Related

C/C++ :: Columns In Fstream Data Reading Zero (Array Of Payroll Objects)

Dec 4, 2014

Basically for homework, we gotta make an array of payroll objects, using the general format I've made below...

I'm having trouble. The .dat file we are given to test looks like this:

40.0 10.00
38.5 9.50
16.0 7.50
42.5 8.25
22.5 9.50
40.0 8.00
38.0 8.00
40.0 9.00
44.0 11.75

When I execute the program, it shows the first column of objects, but will always replace the 2nd column values with a 0.

It's definitely NOT reading that column, and using the constructor to set it to zero. I don't know why it's not reading that column though...

(TL;DR.... Currently it reads "Employee #1: 40, 0" rather than "Employee #1: 40, 10.00"... etc)

Here's my code.... (not 100% done yet just testing datafile output atm)

#include <iostream>
#include <fstream>
using namespace std;
class Payroll {
private:
double payRate; // holds an employee hourly pay rate

[code]....

I want it to read the other column without giving me zero />

View 2 Replies View Related

C/C++ :: How To Write Byte In 8051 Correctly

Nov 16, 2012

i have a hex code and need to write it in 8051

if the hex code is
"11 22 33 44 55 66 77 88"
"88 77 66 55 44 33 22 11" (hexadecimal)

but the result is

11 22 33 44 55 66 77 88 FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

it is should be

11 22 33 44 55 66 77 88 88 77 66 55 44 33 22 11
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

problem is ...
it can not write second hex code
just only write first hex code

i use serial port , VB6 ,keil C

View 4 Replies View Related

C :: Read / Write To File

Mar 3, 2013

How can I erase the data stored in /tmp/a.txt after every run of this code?

Code:

#include <stdio.h> // IO operations
#include <unistd.h> //for pid, ppid, fork functions
#include <sys/types.h> //for pid_t type, kill function
#include <stdlib.h> //for exit function
#include <signal.h> //for kill function
#include <sys/types.h>
#include <sys/stat.h>

[Code]...

View 3 Replies View Related

C :: Create File To Write Text And Read Back The File Content

Mar 15, 2013

The Objective Of This Program Is To Create A File To Write Text And Read Back The File Content. To Do That I Have Made Two Function writeFile() To Write And readFile() To Read.The readFile() function works just fine but writeFile() doesn't.

How writeFile() function Works? when writeFile() function Execute It Takes Characters User Type And When Hit Enter(ASC|| 10) It Ask "More?(Y/N)" That Means What User Want? Want To Go Next Line Or End Input?

If "Y" Than Inputs Are Taken From Next Line Else Input Ends.

But The Problem Is When Program Encounters ch==10 It Shows "More?(Y/N)" And Takes Input In cmd variable.If cmd=='Y' I Mean More From Next Line Than It Should Execute Scanf Again To Take ch I Mean User Input.But Its Not!!! Its Always Showing "More?(Y/N)" Again And Again Like A Loop.

Code:
#include <stdio.h>
void writeFile(void);
void readFile(void);
int main(){

[Code].....

View 5 Replies View Related

C++ :: Read From A Txt File And Then Write A New Text File With Sorted Line

Jun 11, 2014

I have a .txt file which I want to read from and then write a new text file, this time with sorted lines. It is easy to sort one value, but what about sorting entire lines based on one value?

I want to sort the lines based on the FIRST value.

Example text file contents:

values;data
5.01 100
2.9 342
2.69 43534
10.1 43
6.8 45324

Output: Text file containing

2.69 43534
2.9 342
5.01 100
6.8 45324
10.1 43

It's easy to do this if there was only 1 number on each line, but I do I sort all the lines based on the first number in each line?

View 2 Replies View Related

C :: Opening File - Read / Write

Aug 5, 2013

this is my read/write functions based the read from the last post! then went nuts with it! used the %19s%*s on the write to the file, solved all the probs on the read side! but any refining on this would be great. This is another program that i started with the forums, and started going my own direction!

Code:

void WriteAccount(int account,char *a[])
{
FILE *fptr;
char Memo[20];
char buff[20];
double Amount;
struct tm *sTm;
}

[code]....

the a[] is a string array with bank account names, and Account is the number on the menu based list.

View 7 Replies View Related

C++ :: Read / Write From File (fasta)

Dec 10, 2014

I'm looking to write a program in C/C++ to traverse a Fasta file formatted like:

>ID and header information
SEQUENCE1
>ID and header information
SEQUENCE2

and so on

in order to find all unique sequences (check if subset of any other sequence) and write unique sequences (and all headers) to an output file.

My approach was:

Prep: Copy all sequences to an array/list at the beginning (more efficient way to do this?)

Grab header, append it to output file, compare sequence for that header to everything in the list/array. If unique, write it under the header, if not, go on.

However, I'm a little unsure as to how to approach reading the lines in properly. I need to read the top line for the header, and then "return?" to the next line to read the sequence. Sometimes the sequence spans more then two lines, so would I use > (from the example above) as a delimiter? If I use C++, I imagine I'd use iostreams to do the reading.

View 1 Replies View Related

C/C++ :: How To Read And Write Certain Characters From A File

Oct 12, 2014

How to read some characters from file, I know we can move a pointer to some position using seekg() & seekp() function, get current position of the pointer through tellg() & tellp() functions. By moving the pointer to appropriate position using seekg(), we can read the whole line using getline() function. But is there any function which read certain characters from the current position of the pointer and write certain characters from current position of the pointer.

View 1 Replies View Related

C++ ::  How To Read And Write Certain Characters From A File

Oct 12, 2014

How to read some characters from file, I know we can move a pointer to some position using seekg() & seekp() function, get current position of the pointer through tellg() & tellp() functions. By moving the pointer to appropriate position using seekg(), we can read the whole line using getline() function. But is there any function which read certain characters from the current position of the pointer and write certain characters from current position of the pointer.

View 3 Replies View Related

C++ :: Read And Write The Values Of File?

Sep 22, 2012

the output of the password text file contains like this:

username
776655443322 (password encoded)

How to write the value in last line (3rd row) of the file. after that read and write or alter 3rd row value. I need logic using "C" language.

View 3 Replies View Related

C++ :: Read From Txt File One Bit At A Time And Then Write Into Binary File

Apr 22, 2013

I am trying to get the code to read from the txt file one bite at a time and then write this bite into the binary file but i cant seem to get it working.

FILE *fpcust, *fpcustbin; //<<<<<-----point to both sales and customers text files, and the new .bin files for both
char buffer;
int ch;
int ch1;
fpcust = fopen("c:customers.txt", "r"); //<<<<-----pointing to the file
fpcustbin = fopen("c:customers.bin", "wb"); //<<<<<-----pointing to the new binary file, opening in writing binary

[Code]...

View 3 Replies View Related

C :: How To Read And Write A File Using Serial Port

Dec 29, 2013

Explain me a working code to read and write a file using serial communication.and i need to store that file.I know normal file handling in C, but how it is through serial port i am not getting.

View 1 Replies View Related

C :: File Write And Read As A Function Error

Sep 27, 2014

called object 'fptr_in' is not a function or function pointer
called object 'fptr_out' is not a function or function pointer

what can i do for the errors?(i mustn't use loop and arrays for the code)

Code:

#include <stdio.h>
void sum_of_2nd_and_3nd( FILE* fptr_in, FILE* fptr_out);
int main()
return 0;
}
void sum_of_2nd_and_3nd( FILE* fptr_in, FILE* fptr_out){
}

[code].....

View 2 Replies View Related

C++ :: Allow User To Open File And Either Read Or Write To It

Jan 30, 2014

So I've been turning my programs into classes and I run into errors.

So this program is supposed to allow the user to open the file and either read or write to it.

I've omitted the read part from it as I want to attempt that on my own .

I get these compile errors:

fileclass.cpp:13: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp:21: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp: In member function ‘int file::choice()’:

[Code] .....

View 10 Replies View Related

C++ :: Read In Binary Files Then Write To Another File

Oct 11, 2014

I would like to read in binary files, then write them to another file.

I write a code, what works perfectly, if I would like to just copy the file to another. But I want to make it a little other.

If I open a file in hex-editor I also can see the ASCII values. But I would like get the ONLY the hex values to the other file.

For example:

d5 57 4f ad 30 33 0b 4e 49 a7 05 18 c4 90 66 d8 45 ac 39 3e 7d f1 a8 02 80
14 20 90 6e 20 12 38 0c 65 4a 28 d2 80 72 04 20 a9 4a 82 84 60 6a 0b 25
59 4c 30 c8 69 c0 ec fa 36 ed 3a da b1 9a 82 02 e0 bb 7e 41 87 02 f6 10 34
eb 95 93 63 01 6b 8d e1 d7 43 c3 df 92 5d 8a ed 57 61 4e 36 07 2a d7 56 2b
b5 0e 55 83 b4 76 8c b7 61 77 0e c9 76 0c 81 1b 01 63 0c 8b 73 57 d5 6d 4c
0c c2 0d 52 45 18

How could I make it?

View 4 Replies View Related

C++ :: Program That Allow User To Read / Write Onto File

Jan 28, 2013

So I'm trying to create a program that allows one to read/write on to output.txt. I though I had everything set up right, but its only writing one word to the text file. Heres the code.

#include "stdafx.h"
#include <iostream> //Needed for User Input
#include <fstream> //needed for ofstream
#include <string> // needed for strings
#include <windows.h> //needed for Sleep
#include <cstdlib> //Needed for return EXIT_SUCCESS;

using namespace std;
int main() {
ofstream outputfile; //allows to read and write to files

[Code] .....

If I type Puppies Are Cute and go to output.txt, the only thing written in the text file is Puppies.

View 9 Replies View Related

C/C++ :: Create And Write To Text File And Then Read From It?

Mar 26, 2014

I have looked through te tutorials here, and even google it, as well as tried to follow the power points from my class..but I still can't seem to figure out how to make this code work correctly.. Basically I have to create a file called grade and write to it a student name and their grade score, and then read from the file all students names and there grade and display this on the screen as well as calculate the grade average for all of the students and display it.

Well I am able to write to the text file, but I can't seem to get the rest to work. I can't figure out how to read from the text file..Here is my code below.

write a sample code that does something similar write to text file string and numbers and then reads from it.

#include <iostream>
#include <fstream>
using namespace std;

[Code].....

View 6 Replies View Related

Visual C++ :: Read And Write Values Into File?

Sep 22, 2012

correct the below code:

file already contains entries : 1st row username; 2nd row password.

check status required to write at third line and need to read or alter the check status value.

Currently this code is working if already there is a value for check status, then it is overwriting else UI hanging.

Code:
WriteCheckStatusToFile(BOOL& locVar) {
FILE *l_pFile = NULL;
CString l_strRememberCheck;
l_strRememberCheck = GetExePath() + _T("password");
CString sVar;
sVar.Format(_T("%d"),locVar);

[code]....

View 2 Replies View Related

C++ :: What Data Type That Will Read In Letters And Numbers Form Fstream

Sep 11, 2013

What data_type will read in a string of letters mixed with numbers using fstream :);

View 1 Replies View Related

C :: Write / Read Tree Nodes Into A Binary File

Oct 20, 2013

I have a binary tree, and I want to store all information its leaves stores into a binary file and then read it.

What I tried:

A function that reads the tree, and whenever it finds a leaf, it writes in the binary file:

Code:
[...]
if(tree->right == NULL && tree->left == NULL){
fwrite(tree, sizeof(tree), 1, output);
}
[...] a

And in the main function:

Code:
[...]
for(i=0;i<5;i++){
fread(tree, sizeof(tree), 1, output);
printf("This is the leaf number %d", tree->num);
}
[...]

I'm trying to read 5 nodes just to see if its working but it isn't. The output shows the first node of the tree everytime.

View 2 Replies View Related

C++ :: User Can Read Or Write Into A File That Is Not Hard Coded

Jan 16, 2014

int main (){
string filename;
cout << "Enter the file name: ";
cin >> filename;
ofstream myfile( filename.c_str(), ios::app);
}

Here is the code and my original question was how do I make it so that the user can read or write into a file that is not hard coded.

hard code example:

string filename;
cout << "Enter the file name: ";
cin >> filename;
ofstream myfile( "file.txt", ios::app);

View 13 Replies View Related







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