C++ ::  Reading And Writing To File Using Same Fstream

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


ADVERTISEMENT

C++ :: Fstream - Writing In The Middle Of A File

Sep 7, 2012

I want to edit the contents of a file using fstream, but none of the modes in the fstream constructor work for me.

If I use ios::trunc, then the whole file is deleted - but I want to retain the contents and edit it. If I use ios::app, then I can only add data to the end of the file - but I want to edit the data in the middle of the file. If I use ios::ate, then the whole file is deleted again, similar to ios::trunc.

How can I create an fstream object without deleting the contents of the file, whilst still being able to move the pointer arbitrarily around in the file with seekp() (and not just placing it at the end as with ios::app)?

View 3 Replies View Related

C++ :: Writing Text Data To A Binary File With Fstream

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

Visual C++ :: Copying One Bmp File To Another Using Fstream - Access Violation Reading Location

Dec 31, 2014

I would like to copy one bmp file to another using plain fstream (none 3-rd party library). I've managed to come up with something but I keep getting error:

Unhandled exception at 0x504A3442 (msvcr120d.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00380000.

when i try to write the content of the bmp files not the header, namely error occurs in this line:

Code : ofs_differential.write((char*)&picture, (bfh_warm.bfSize - bfh_warm.bfOffBits));

Also i am not sure if i am doing it in a right way, meaning that even after solving this error i will copy the bmp file successfully.

Code:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
struct BITMAPfileHEADER {
unsigned short bfType;

[code]....

View 3 Replies View Related

C++ :: Reading And Writing Bit To A File

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

C :: File Reading And Writing

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

C :: Reading And Writing To File

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

C++ :: Reading And Writing To The Same File?

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

C/C++ :: Reading And Writing Bit To A File

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

C/C++ :: Reading And Writing PPM File Image

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

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 View Related

C++ :: Reading And Writing Data In Text File

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

C++ :: Reading / Writing File On Multiple Threads?

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

C++ :: Reading File Then Writing To It If Duplicate Not Found

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

C++ :: Writing Integer Array To File In Binary Form And Reading It

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

C++ :: Reading GLOBE File - Access Violation Writing Location 0x00000001

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

C :: Visual Studio 2012 - Writing Data Into TXT File / How To Stop Reading Char

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

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 :: Writing Array Into File And Reading Array From File

Apr 20, 2014

i've been trying to write array into file and read them into the same program again, but the output is all wrong.

Code:

#include<stdio.h>
int main()
{
FILE *fp;
char name[3][7];
int x;
}

[code]....

View 1 Replies View Related

C++ :: Fstream Class Not Reading Word For Word

Aug 6, 2013

int countTextWords(ifstream * file)
{
string textWord;
int wordCount = 0;
while((*file) >> textWord)
{
wordCount++;
}
return wordCount;
}

for some reason, (*file) >> textWord will not read words into the string. What am I doing wrong?

View 9 Replies View Related

C++ :: Create File With Fstream?

Feb 18, 2014

For some unknown reason when i try the following code it doesn't create a file. (the program will simply get two inputs from the user and then use http to obtain the html/php text and then output it to a file)

code:

#include <SFML/Network.hpp>
#include <iostream>
using std::cout;

[Code].....

View 3 Replies View Related

C++ :: Reading And Writing Files?

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

C++ :: Login With TXT Writing / Reading

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

C/C++ :: Reading And Writing Files

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

C++ :: How To Use A User Entered File In Fstream

Feb 14, 2014

So, I am making a name-database-type-thing and I want the user to be able to enter there own file. Like, here:

string fileName;
string name;
cout<<"What is your file name?"<<endl;
getline(cin, fileName);
ifstream nameFile(fileName);
getline(cin, name);
nameFile<<"Name: "<<endl;

Well, that last part is in a loop, so you can enter an unlimited number of names to the .txt file.

So, it won't let me use the string in ifstream. How can I fix this? This maybe unclear, so if it is just say so and I will edit it.

View 6 Replies View Related

C++ :: Link Between (STL) Container And (Fstream) File I/O?

Nov 9, 2012

I did some of the projects using STL container and it is pretty much efficient to retrieve the data's information quickly (i.e. sets, sets & maps). I read many C++ OOP book and none of them make me clear about the Link Between (STL) Container and (Fstream) File I/O. Can they link together ?

I am going to design a small Library Book Management Project. I am planning to develop it using the STL Algorithms & containers. Objective of this project is to keeping the records of books issued and deposited by students in a particular date. User will be given a facility to retrieve student's information, book information and he/she can modify/update specific book's, student's information by changing issue date, deposited date, Book's publication etc.

My intention is to create a File I/O where user can input & update/modify daily transaction inside the file called (xx.dat) file in project directory. Is it possible to create 'set' container inside the file (xx.dat) ? Or, It's impossible ? Or, do i have to do this in other way?

View 6 Replies View Related







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