C++ :: Reading File And Storing In Class Objects?

Aug 18, 2013

I've a text file : Random.txt which comprises of
Jade
12MS234
Male
18
Rocky
12MS324
Male
18
Marx
12MS632
Male
18

Now in my program i've a class
class stud
{
char name[10];
char reg[10];
char gender[10];
int age;
};

Now I've to write a code in c++, where i've to read the given data and store this into 3 objects of stud class(array of objects) ...then further i've to manipulate the above stored data. I think i'm getting error while storing...variables are showing random characters... give me the code.for this program..in C++

View 2 Replies


ADVERTISEMENT

C :: Storing A Value In Array From Reading In From A File

Sep 12, 2013

Is something like this possible and if not how can I make this work?

fscanf(in, "%d %d %d", &i, &x, &arr[i+x]);

View 4 Replies View Related

C++ :: Reading And Storing CSV File Data

Feb 21, 2013

I am working on a small c++ project where i read from a csv file with information in this format:

string,string,int
string,string,int
string,string,int

And all of the above information is one person. As is "John,Peter,23" is first name, last name, age. When there are multiple people in a csv file how can i parse through and separate the information in a string and int for later use?

View 4 Replies View Related

C++ :: Reading A File And Storing It In A String

Feb 7, 2013

I know how to do this in c++ with fstream and std::string and getline and so on and so forth. Im writing my code solely in c however. I can't get g++ installed so figured it was a good excuse to learn c instead of using the equivalent c++ abstracts.

My problem is, I'm making a game in c that I have made in c++ but have ran into an issue with my map. I want to read in my map from a file which just looks like this:
Name of Town
* * * * * * *
* * * * * * *
* * * * * * *
etc...

so i tried using fscanf to first read in the name of the town (stored in a char*) then read in the characters (in this case '*')(not including white spaces becuase i can just print those) into another char*. what is the better way to do this?

View 16 Replies View Related

C++ :: Storing Words In TXT File And Reading Them?

Apr 12, 2013

I would like to store the titles of a CD and then read them. I have started a program but not sure how to display or make sure it is storing it in the .txt file.

#include <iostream>
#include <fstream>
#include <cstdlib>

[Code]....

View 3 Replies View Related

C/C++ :: Reading CSV File And Storing In Variables

Mar 16, 2015

how to read a csv file where the data is seperated with commas so the csv file looks like:

programming,03,60,141,01,W,2015
programming,03,60,141,30,W,2015
Algebra,03,62,102,02,S,2013
Religion,08,98,938,20,F,2014

So i need the name of the course in one variable, the course code (ex 0360-141-01 for line 1) in another variable and the term (ie W2015 for line1). So i got the name done but cant figure out the course code since i need more than one value that is seperated by a comma. my code is:

#include<stdio.h>
#include<string.h>
typedef struct CourseInfo {
int courseID;

[Code].....

View 2 Replies View Related

C++ :: Reading And Storing A Sentence From TXT File

Dec 11, 2012

I've created a class that is supposed to store first name, last name, date of birth, date of death, and a fact about a person (all variables within the class). Im trying to fill these variables with a read function. it reads a .txt file like this

Firstname Lastname 1987 1988 this guy did this

The problem is, I don't know how to handle the last variable. the variable needs to hold the entire "this guy did this" sentence. i made it a string, just because i was clueless, and as expected, it only holds "this"

this is my .h:

#include <string>
#include <fstream>
#include <iostream>
using namespace std;
class Person {
public:
Person();
Person(const Person & person);

[Code] ....

Here is the read function in the .cpp:

bool Person::Read(ifstream & input) {
return (input >> fname >> lname >> dob >> dod >> fact);
}

View 3 Replies View Related

Visual C++ :: Reading And Storing TXT File

Feb 12, 2014

I am trying to read and store a txt file with format below:

2.5;abc;2,4000
2.5;bef;3,2000
2.5;ref;3,1000

I try the fscanf( pFile, "%d;%s;%d,%d", buffer,buffer2,buffer3,buffer4 ); to store 2.5 to buffer, abc to buffer2, 2 to buffer 3 and 4000 to buffer 4 on first line.

However, it doesn't work. How can I achieve this? I want to read the 2nd line and so on.

Code:
char buffer[100]="";
char buffer2[100]="";
char buffer3[100]="";
char buffer4[100]="";
FILE * pFile=NULL;
pFile = fopen("C: est est.txt", "r");

[Code] ....

View 11 Replies View Related

C++ :: Reading Data From File Then Storing It To Reuse?

Mar 27, 2013

Im tasked with reading a data file, this is an example snippet

list of trophy winners
year facup leaguecup 1stdiv 2ndiv
1960/61 Tottenham Hotspur Aston Villa Tottenham Hotspur Ipswich Town
1961/62 Tottenham Hotspur Norwich City Ipswich Town Liverpool
1962/63 Manchester Utd Birmingham City Everton Stoke City

The file starts in 1892 and is up to 2011/12, there is data missing for some years due to the wars etc,

once ive read the file, i need to store the data, for me to re-use.

There are a lot of useful link regarding reading data in, but they tend to be with very small files with say 10 lines of numbers.

View 1 Replies View Related

C++ :: Reading Integers From File And Storing Into Array?

Oct 28, 2014

My main issue is that when I store just one value into a simple integer variable, it gives me a big negative number.

My goal is to store a file that reads like this into arrays:

2014 1 23
2012 2 15
2013 1 25
etc

Where I would have the first column in an array, the second in another and the third in third array since I am not allowed to use multidimensional.

View 5 Replies View Related

C/C++ :: Reading Input From A File And Then Storing It Into Array?

Feb 16, 2015

I need to read input from a file , which contains multiple sentences of varying lengths. After each new line char, i need to store that sentence into an array.

#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>

[Code].....

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

C :: Reading Lines From A File And Storing It In Array From Typdef Struct

May 4, 2013

I'm trying to read in lines for a file and storing it in an array. This is what I have so far. Am I allocating the memory correctly?

Code:

typedef struct {
/* declartion of variables */
} App
typedef struct {

[Code].....

How do I access the array content? I'm suppose to strtok the contents from the lines stored in the array and use it again.

View 2 Replies View Related

Visual C++ :: Class For Storing Values Inside XML File

Oct 9, 2014

In VC++ is there any inbuilt class for storying values inside an XML File, without using CLI?

View 14 Replies View Related

C++ :: Storing Objects Using Vector

Oct 31, 2014

I have two classes, Parent and Child, where Parent contains a vector that is used to store instances of Child. When I create an instance of Parent, three instances of Child are also created in Parent's constructor and stored in the vector. I understand that push_back() creates a shallow copy of each Child instance and that Child's destructor is called each time the loop (inside Parent's constructor) iterates. The problem is that because Child's destructor is called each time the local variable child goes out of scope, the memory previously allocated in Child's constructor is destroyed and when Child's destructor is called again later on in the program to get rid of the copy stored in vector, the program crashes. I can fix this by overriding the default copy function or by storing pointers to objects instead of copies of objects. I don't really need to use vectors in this case since I always have three children in one parent but I'm doing this as a learning exercise and would prefer to use vectors.

#include <iostream>
#include <vector>
class Child {
public:
Child() {
std::cout << "child constructor called" << std::endl;

[Code] .....

View 3 Replies View Related

C++ :: Storing Objects Of Hierarchy In A Vector

Jun 6, 2012

I have a hierarchy of Actions (NonMovingActions and MovinActions each with sub-hierarchies). Actions class has an abstract function.

Code:
class Action {
public:
Action(Agent& agent, ...):agent(agent),..{}
virtual ~Action(void);
virtual bool run()=0;

[Code]...

It appears that C++ does not allow this (in Java it was possible). Compiler objects that Action class is abstract (and cannot be instantiated?!)

Code: error C2259: 'Action' : cannot instantiate abstract class

1- May I know what do I not understand here? We cannot refer to sub-class instances with a reference of parent class type?

2- Should I use vector of pointers instead or what?

View 6 Replies View Related

C :: Reading A File Line By Line And Storing It Backwards Into A List

Sep 25, 2013

So I'm reading a file line by line and storing it backwards into a list. So if the file has has this format...
1
2
3
4

The code should store each line in a list as such...
4, 3, 2 ,1

Instead the code will store the last variable in all nodes. So the final list will look like this...
4, 4, 4, 4

Here is my code...

struct node *head = NULL;
int i;
while(read(in, &i, sizeof(int)) != 0) {
struct node *temp = malloc(sizeof(*temp));
temp->line = &i;
temp->next = head;
head = temp;
}

View 4 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/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++ :: 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 View Related

C++ :: Reading And Storing Newline Character Alongside Data

Jan 6, 2014

I am reading data from a text file into a program. I am well aware of the subtle distinctions in the mode of data input/entry when using the stream extraction operator, the get() function, and the getline() function.

My problem is that all of them do not read and/or store the newline character alongside the data read!

Any function that reads and stores data and the terminating newline character together??

View 1 Replies View Related

C++ :: SerialPort Communication - Reading Data From Port And Storing In One Document

Jun 27, 2014

I am doing SerialPort communication program where i am opening the port and reading data from port and storing in one document.

I debug my program and check that my read function is reading the data properly but when checking the document i find only unicode.

Suppose I am sending 1,2,3,4 to port i able to read same but my document showes þÿÿ þÿÿ þÿÿ þÿÿ

//My Read Function which read data from serial port
tInt serialport::Read(tVoid* pvBuffer, tInt nBufferSize) {
tInt nBytesRead = 0;

[Code] ....

Why I am getting unicode instead of 1,2,3,4 in my document ....

View 3 Replies View Related

C++ :: Deleting Array Of Derived Class Objects Through Base Class Pointer

Mar 21, 2015

In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,

Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;

[Code] ....

This sample code does exactly what I want. So does the author mean the way I did is undefined?

View 1 Replies View Related

C++ :: Creating Array Of Pointers To Base Class To Point To Derived Class Objects Dynamically

Jan 16, 2013

Please consider the following code :

#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;

[Code] ....

As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.

View 2 Replies View Related

C++ :: Initializing Inner-objects Of Base Class From Driven-class Constructor

Jan 6, 2015

Let's say I have a Car object , and it contains inner Engine object.

Code:
struct Car{
Engine mEngine;
};

In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:

Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}

Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?

Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};

The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .

View 5 Replies View Related

C++ :: Writing / Reading String Objects To / From Files

Jul 7, 2013

I am using the code below to write a single instance of object "Employee" to a file in Binary mode. The write part seems to work fine, however when I try to read the single employee object from the file into memory I get a double free or corruption error.

I think this has to do with the fact that I am using a string data member in the Employee class but I don't understand what is going wrong. I have read that strings can vary in length and use dynamic memory allocation but if I write a single employee object to a file with data member 'name' equal to "John", it should be the exact same size when I read it back in right?

The code below works with no issues when I omit the string data member. Why is that? Where is the memory for the string object being "double released" when I read the employee object back into memory from the file?

I am using Linux Mint 15, Eclipse June and GCC 4.7.3 with the -std=c++11 option.

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

[Code].....

View 3 Replies View Related







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