C++ :: String Stream Object And Text File Data Access

Jan 21, 2014

I have two puzzling issues I am dealing with.

Issue 1: I am using a stringstream object in a block of my program that needs to be visited repeatedly depending on a user's selection from a menu. I want the contents of this stringstream object to be cleared any time control gets to this part of the program. I have tried the clear and flush functions to no avail.

Issue 2: I am reading data from a source text file that would be regularly changed during the course of program run. After the program run is over, I am supposed to save the results(which is basically the source text file AND all updates) in a destination file. This destination file would then serve as the source file when next the program is run. In other words, I want a scenario where my results overwrite the original contents of the source file; implying that my source and destination files are now one, pretty much. How can I do this?

View 7 Replies


ADVERTISEMENT

Visual C++ :: File Stream Change Its Address During Writing Text Contents To The Stream

Apr 29, 2013

I wrote a program to write text contents to file stream through fputs, the file stream address was changed in the middle of writing text content to the stream (11% text content have been put into the file stream), that cause the file stream pointer can be evaluated problem and raise exception on stream validation code in fputs library function, my question is what things could go wrong to make file stream pointer changed its address to something else or a NULL pointer if the file stream have not been flushed and closed.

View 5 Replies View Related

C++ :: How Could Object Access Its Private Data Members From Outside

Nov 12, 2013

How does an object access its private data members in copy constructor.

The relevant part of the code: Code: C::C(const C &obj)
{
x = obj.x;
y = obj.y;
}

Normally the object1 called "obj" cannot access its private data members outside. But in this situation it can access. How can it be explained?

Here are the complete code:

Code:
#include <iostream>
using namespace std;
class C{
public:
C(int,int);
C(const C &);

[Code] .....

View 7 Replies View Related

C/C++ :: Create Class Instances From Text File Stream

Nov 10, 2014

I am trying to do some exercises but am struggling at the moment. The first task was to create a class (Planet), then allow the user to edit their entry or view it.

The second one is to create class instances from a text file. The file contains a new planet on each line in the form: 'id x y z' where x/y/z are its coordinates. As the file can have more then one lines, it has to dynamically create an undefined amount of class instances.

To do this I used 'new' and it works ok - it prints each one out to the screen as you go so you can see it working. However... I'm trying to get into good habits here and am encapsulating the class which is where I am getting stuck. I can read from the class but cannot put the values from the file into the class.. ..using the member functions I have created anyway.

My code so far is:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Planet {
private:
int id=0;
float x_coord=0.0, y_coord=0.0, z_coord=0.0;
public:
int GetID(){return id;}

[code]....

If I change the SetID etc to just p->id, p->x_coord etc it works fine. But I'd rather find a way to do it that keeps the encapsulation. Using p->z_coord etc requires that you change the class variables from private to public.

The question I have been given is this:

Define and implement a function, generate planet, that takes a stream argument that has already been connected to a file (i.e. the argument is istream& fin). The function must create a new instance of planet using new and read its details from the next line in the file.Each line of the file is in the format id x y z.The function must return the newly created planet.

Also, how would you go about 'viewing' one specific class instance once they've been created? So say the file had 5 lines, line three was '4 6 2 6'. How would I go about viewing that planet afterwards? I don't think thats required but... I'm just wondering Although I'm also wondering, are we actually creating a new class instance for each line here? Or just destroying the previous one?

View 14 Replies View Related

C++ :: Stream Data To A File And Then Return To File To Add Further Data?

Aug 23, 2012

I am trying to stream data to a file, and then return to the file to add further data. When I add data the second time, I then want to update the value of the second byte in the whole file. I can't seem to do this!

Here is my sample code:

Code:
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
int g = 7;
int x;
fstream out1("file.dat", ios::out | ios::binary | ios::trunc);

[code]....

The output I get is "1, 2, 3, 4, 5, 6", but I want to be getting "1, 7, 3, 4, 5, 6", because in "out2", I seekp to the second integar entry, and change it to "7".

I have also tried using ios::ate in the constructor for "out2", but this gives me the out put "4, 7, 6, 6, 6, 6", which is suggesting that when I create my fstream object "in", any seekg commands are relative to the beginning of the "out2" stream, rather than the "out1" stream.

View 3 Replies View Related

C++ :: File Stream And String Extraction?

Apr 7, 2014

I am currently trying to read in a file that takes in the input in the following form.

Code:
HANK>25 BOB>31 AL>54
BILL>41 ABE>63 JEFF>50

I have tried the following solution:

Code:
#include<ifstream>
#include<iostream>
using namespace std;
struct node
{
string name;
int age;
);
int main ()
{

[code]....

The problem is a.name, it's extracting the entire string before the space character causing p.age to contain "BOB" and so on. I've tried using p.get(p.name, sizeof(p.name), '-') with '-' as the delimiter and p.getline() using a character array instead of a string. How would it be possible to only copy the string into a.name before the '>' character?

View 1 Replies View Related

C++ :: Access To Data Of Array While Have Its Name As String

Jul 31, 2013

The problem is as following. First, I have 21 arrays named: integer abee1, abee2, ... , abee20 and myarray all with the same dimension of [51][4]. Next, in a loop of 20 circles, I fill myarray with random integers then copy it into abee1-20 (each one per iteration). Then, for each array (abee1-20), I want to calculate sum of all numbers in second column as cost of that array and store it in the a new array called arrayCosts[20][2] ( char arrayName, int arrayCost)- the name of array in the arrayName column and cost of that array into arrayCost column . After that, I want to sort arrayName based on the cost and extract the 3 top array with lowest cost. I know that I should do a search in the array to find these three. Finally, copy these three into 3 new array called elite1, elite2, elite3.I do not know what to do.

1-The following code is for making the sting name of abee1-20 in string made in each step of the loop.

for ( cc = 1 ; cc < 20; cc++)
{ string String = static_cast<ostringstream*>( &( ostringstream () << cc) )->str();
string array_name = "abee" + String;

I want to use array_name (for example in the first loop which array_name is “abee1” ) for reaching the abee1 (which is a filled array) after dong a search in the array called “arrayCosts”. This search retrieves the name of array but it is string and I want to have the array with the same name.

View 2 Replies View Related

C++ :: Debug Error When Taking A String And Casting It To Float With File Stream

May 9, 2014

Debug Error

ProjectsFinal ProjectGrItemDebugGrItem.exe

R6010 - abort() has been called

I was going over this with a friend and it seems as though getline() is not reading anything in and thus throwing the abort error. I'm not sure why this is because I've included the textfile, with the correct name of course, in both the regular file location and the debug folder. I ask for user input and the user then inputs the name of the file they want, I do some required things behind the scenes and display the results for them in a cmd window. I've included pastebin files for both my header and cpp files because it is far to large for one post I shall, however, post the full code in the comments.

Quick Code

The problem occurs on line 159. I'm assuming once this line is fixed, line 163 will have the same problem.

// Read regular price
getline(nameFile, input, '$');
vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
// Read sale price
getline(nameFile, input, '#');
vectorList[count].salePrice = stof(input.c_str());

Pastebin Links : [URL] ....

View 2 Replies View Related

C++ :: Read Byte Of Char Data And Convert It Into Text String Of Binary Data That Represents Hex Value

Dec 26, 2013

I am writing a program where I need to read a byte of char data and convert it into a text string of binary data that represents the hex value...

i.e. The char byte is 0x42 so I need a string that has 01000010 in it. I've written the following subroutine....

------------- My Subroutine ----------------------------------------------------------------------
void charbytetostring(char input, char *output){
int i, remainder;
char BASE=0x2;
int DIGITS=8;
char digitsArray[3] = "01";

[Code] ....

When I submitted the byte 0x42 to the subroutine, the subroutine returned to the output variable 01000010... Life is good.

The next byte that came in was 0x91. When I submit this to the subroutine I get garbage out.

I am using a debugger and stepped through the subroutine a line at a time. When I feed it 0x42 I get what I expect for all variables at all points in the execution.

When I submit 0x91 When the line remainder = input % BASE; gets executed the remainder variable gets set to 0xFFFF (I expected 1). Also, when the next line gets executed..

input = input / BASE; I get C9 where I expected to get 48.

My question is, are there data limits on what can be used with the mod (%) operator? Or am I doing something more fundamentally incorrect?

View 6 Replies View Related

C++ :: Function That Brings In A Stream Object

Sep 23, 2014

I'm supposed to add two new functions to an existing class that I've written early on: readData(ifstream&)and writeData(ofstream&).

The parameters of the program are:
-Create three employee objects as shown,
-Create an ofstream object and open a file. Choose any name for the file that you want. Do not ask the user for the file name. Pass just the file name as the parameter (no path) so that your program assumes the file to be in the same folder as your executable file.
-Send messages to each of the three Employee objects to write themselves out to the file.
-Close the file.
...

If I get writeData(ofstream&) function bit to work! for brevity I've cut all the functions of the class that arent necessary. Here's what I have so far:

Employee.h
#pragma once
#include<string>
using namespace std;

[Code] ....

So I've tried a bunch of different ways to get my objects into the ofstream object to write them to the file, but I'm supposed to use the two new functions somehow...but I'm way lost.

View 5 Replies View Related

C/C++ :: Passing A Stream Object By Reference?

May 5, 2014

I need to create a function in my program to open an input file and another function to open an output file and I need to use the files in other functions so Im trying to pass the stream object by reference but then i need a condition that tells the compiler not to reopen the file because then it will delete everything and make me input the file names again. Heres the two functions.

void InputFileOpen(ifstream &inFile) {
string file_name;
if(!inFile.is_open()){
cout<< "Enter the input file name: ";
cin>> file_name;
inFile.open(file_name, ios::in);

[code]....

View 4 Replies View Related

C Sharp :: How To Access Raw Image Data In Resource File

Nov 14, 2012

I have a .png file that my console app reads and puts into a structure that is saved as a file. The png is always the same so I thought I could just add it as a resource image to a resource file so it would be included in my program and not as a separate file. When I did that, the png shows up as a "SystemDrawingBitmap". If I examine this resource in a watch I see "Base" and "Static" members. Expanding those properties does not give me anything useful such as a pointer to the raw png data and its length in bytes.

If worse comes to worse, I can always make a hex dump of the png contents and then put the hex code into a CS file using static initialization.

Surely there is some way I can access the raw data internally and read the bytes into a byte array using C#

View 2 Replies View Related

C++ :: Overloading Stream Operator - Return Memory Address Instead Object

Jul 26, 2012

Try to implement overloading << operator. If I done it void then everything work fine (see comment out) if I make it class of ostream& then the operator return to me some memory address.

Code:
#ifndef Point_HPP // anti multiply including gates
#define Point_HPP
#include <sstream>
class Point {
private:// declaration of private data members
double x;// X coordinate
double y;// Y coordinate

[Code] .....

View 7 Replies View Related

C++ :: Access Private Data Of Base Class Without Access Modifier

Sep 9, 2013

if we don't provide the acces modifiers for base class and we need to manipulate the private data of base class in derived class. Is there anyway to acces the private data members? Here's a coding example

class A {
private :
int a;
};
class B : public class A {
public :
void displayA() { cout<<a<<endl; }
};

how i can acces the a of base class A in derived class B without acces modifiers.

View 16 Replies View Related

C :: Search A File For Specif Data And Update It (random Access)

Nov 9, 2014

The file name is "movie.dat" and currently i was able to save data into the file in this order:

[movie_code] [movie_dur] [movie_title] [movie_rating] [movie_dir] [movie_genre] [movie_status]

[12345] [120] [Movie] [PG13] [Director] [Comedy] [Active]

I want to search for the "movie_code" and change the [movie_status] from Active to Inactive.

So lets say for example i have a movie code 12345 saved in my movie file. I want to change the value from "Active" to "Inactive"for argument sake.

This is the code i was trying to do it with:

Code:
FILE *movie_fp;
movie_fp = fopen("movie.dat", "r+b");
int m_code;
MOVIE movie_data;
printf("*** Welcome to the movie updater! ***

[Code].....

View 3 Replies View Related

C++ :: Data Input Into A Text File While Not Deleting Original Data

Apr 19, 2013

I want to input data into text file while not deleting the original data in the file and I use something as

ofstream writefile;
writefile.open("example1.txt");
if (writefile.is_open()) {
for(j=0; j<N; j++) {

[Code] ....

But this will delete the original data.

View 3 Replies View Related

C++ :: How To Access Excel File From A Program - Search It For Data And Return Info

Nov 12, 2014

I have been asked to create a program that accesses an inventory file done in excel, look for a alpha-numeric code, which will be inputed by the user, and return to the user informations related to the position of that code, and informations related to the header of the column in which the code resides.

An example of the function I need the program to perform would be:

User inputs: 1429-R1

And the program returns: Marco's 6A, 8/21/2014, 3/7/2014.

using the following example of file:

Marco's 6A
Assessment 8/21/14 3/7/14
1584-R1 1584-R1 1584-R1
1461-2R1 1461-2R1 1461-2R1
1429-R1 1429-R1 1429-R1

View 1 Replies View Related

C++ :: Reading File With Delimiter - Storing Data To Object

Dec 5, 2014

I am trying to read a file use the data line by line to create into an object. The current file I have is like this and the code reading the file will be found below.

1223 Fake1 Name1 60 70 80 24 89 add1 Male
1224 Fake2 Name2 61 70 81 80 24 add2 Male
1225 Fake3 Name3 63 70 82 80 89 add3 Male
1226 Fake4 Name4 63 70 83 80 88 add4 Male

The problem I am having is that I need to put delimiters in the file so that a person can have more than one name and also the address can now hold multiple strings until the delimiter.

I would like to change my file to this;

1223 : Fake1 Name1 : 60 : 70 : 80 : 24 :89 : This will be address1 : Male
1224 : Fake2 Name2 : 61 : 70 : 81 : 80 :24 : This will be address2 : Male
1225 : Fake3 Name3 : 63 : 70 : 82 : 80 :89 : This will be address3 : Male
1226 : Fake4 Name4 : 63 : 70 : 83 : 80 :88 : This will be address4 : Male

How can I update the code below so that it can use the delimiters to create an object?

void loadFile(Person people[], int* i) {
ifstream infile("people2.txt");
if ( !infile.is_open()) {
// The file could not be opened
cout << "Error";

[Code] .....

View 5 Replies View Related

Visual C++ :: Get Data From Input Stream?

Jan 8, 2014

Code:
#include <fstream>
#include <string>
using namespace std;
ifstream inf("file name");
string line;
while (inf.good()) {
getline(inf, line);
}

instead of line by line i want the complete data from file to the string.What modification i have to do?

View 3 Replies View Related

C++ :: Data File Handling Error - Character Strings Not Copied On Text File

Nov 24, 2013

I have this code for a computer project... (store management) but the character strings are not copied on text file..

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Store {
public:
char *item_name[5];
store()

[Code] .....

Now when i run the program, it gives a error :::
ERROR
address 0x0

How can i write these strings to the text file?

View 2 Replies View Related

C++ :: Reading Stream Data From PDF Files Compressed In LZW?

Nov 28, 2013

I'm trying to read stream data from a stream in a pdf file compressed in LZW. My first step is to read this compressed data into a buffer. The second step is to decompress it. I know how to decompress it, it is the first step I am having trouble with.

How should I read this data? If I read each character into a char the numerical output will not exceed 256. LZW compressed output should exceed 256.

I read the pdf in binary mode and have tried the read function like below.

From what I understand (don't quote me), I should be reading in 12 bits at a time. Is that correct and if so should I be using a bitfield. If I do need a bitfield then how to I read the data from the stream into a bitfield of 12 bits without restricting the binary value of the characters being read from the compressed stream.

Code:
ifstream inputfilestream;
inputfilestream.open("myfile.pdf", ios::out | ios::binary);
char mychar; //unsigned char gives an error though it can be //converted to unsigned char later.
while (inputfilestream.read(mychar, 1)) {
// do something with the char.. I have stored it in an int vector
}

View 14 Replies View Related

C/C++ :: Getting Data From File - Count Number Of Words In A Text File

Mar 27, 2014

I have a program I have to do that counts the number of words in a text file. I have tried the code on 2 computers now since my programming teacher told me the code was fine. Here is my code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
ifstream infile;
infile.open("tj.text" , ios::in);

[Code] .....

View 4 Replies View Related

Visual C++ :: Stream Data From System Windows Controls

Dec 19, 2012

I need make a CBitmap or a streamdata from a System.Windows.Controls::Image(rendered out put)in a SDI mfc application (supporting clr). Here is my codes:

Code:
using namespace System;
using namespace System::IO;
using namespace System::Collections::Generic;
using namespace System::Windows;
using namespace System::Windows::Controls;

[Code] .....

By this codes i can read datastream from bitmapSource but in this way somthing goes wrong in most of GIF animations. After debug i found out i need 3 more things too decode a gif animation - (x,y) position and size of each frame and disposal method for each one - finally i just found a way to draw tru images on a form window by this codes:

Code:
System::Windows::Window^ mainWindow;
mainWindow = gcnew System::Windows::Window();
mainWindow->Title = "GIF Imaging Sample";
//ScrollViewer^ mySV = gcnew ScrollViewer();
array<System::Byte>^ pixels = gcnew array<System::Byte>(bitmapSource->PixelHeight * Stride);

[Code] ...

When i change

Code: bitmapSource = decoder->Frames[0];
to
Code: bitmapSource = decoder->Frames[01];

frame[1] has drawn perfectly on window(i think some how Image class takes care about - (x,y) position and size of each frame and disposal method for each one ),so im wonder if how can i make a CBitmap or a data stream from System.Windows.Controls::Image class to use in mfc app.

View 2 Replies View Related

C++ :: How To Add Array Elements To String Stream

Oct 17, 2013

This is my code:

#include <iostream>
#include <string>
#include <istream>
#include <sstream>
using namespace std;
int main() {
string groups[3] = {};

[Code] ....

It outputs jibberish. I can do what I need to do but I would need to declare more variables and write more cout's, isn't there a way to add these elements to a stringstream or streambuffer? My goal is to write this program and make it as comprehensive as possible but also with very few lines.

View 1 Replies View Related

C++ :: Stream Buffer To Base64 String

Oct 2, 2013

I have problem with this code:

#include <windows.h>
#include <stdio.h>
#include <gdiplus.h>
#include "ScreenCap.h"
#include <wchar.h>
#include "base64.h"
#include <sstream>

using namespace Gdiplus;
int GetEncoderClsid(WCHAR *format, CLSID *pClsid) {

[Code] ....

This function return me a long series of Y ended with other chars but not the base64 string.

I think the problem is std::string encodedstring = base64_encode(buffer, dwBufferSize); but I can't figure out the problem.

The base64 function is: [URL] ....

View 2 Replies View Related

C++ :: Optional Parameter In String Stream

Sep 2, 2013

I'm reading in a data set using an ifstream, then fetching line by line to a stringstream:

std::ifstream sfile(filename);
std::string test;
std::getline(sfile, test);
std::stringstream sline(test);

(I'm not sure why I used an intermediate string; it's pretty much legacy-code at this point, which I just reuse every time. Still works, so why change it!)

The problem is I'm using two types of data sets now, and the difference is one (optional). Most data files just have an arbitrarily large number if the second must be ignored, but others have nothing.

In the normal case, I'd simply use sline >> d >> L; to extract the parameter values. However, I'm not sure how this line will behave if the second parameter is omitted. Will it read nonsense? How do I check whether or not the parameter was set or not?

View 7 Replies View Related







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