C/C++ :: How To Structure And Write A Class For Text File Streams

Apr 20, 2014

I need to make a program which reads multiple lines from a text file and stores that information in a vector of structs(the vector class template also needs to be custom made).

One of my requirements is to have a class dedicated for I/O for the text file. At the moment I can't seem to get a way to input the data from a file into a vector from an InputOutput class. This is my code:

InputOutput.h

#include <ostream>
#include <fstream>
#include "Share.h"
#include <string>
#include "Vector.h"
class InputOutput {

[Code] ....

This is the menu selection function in the menu.cpp where i figured i would call the Input file and store it from case 1. I think I'm doing it wrong though. Is there a better way of doing this because at the moment i am getting some errors such as error LNK2019.

void Menu::UserMenuSelection() {
Vector<Share> shares;
std::ifstream infile;
switch (menuOption) {

[Code] ....

View 2 Replies


ADVERTISEMENT

C++ :: Program That Reads Text From A File And Outputs Each Line To The Screen - I/O Streams

Jun 15, 2013

I seem to be missing a concept or 2 here ... I am tasked with writing a program that reads text from a file and outputs each line to the screen as well as to another file PRECEDED by a line number ...

In addition, I have to Print the line number at the start of the line and right-adjusted in a field of 3 spaces ...

Follow the line number with a colon then 1 space, then the text of the line.

Another kicker, is I have to grab the data 1 character at a time and write code to ignore leading blanks on each line.

Here is what I have so far:

#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>
using namespace std;
int main() {
char next;
int count = 0;

[Code] ....

View 7 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++ :: Multiple Data Streams In One Class - Handling All In One Member

Jan 24, 2014

I have to implemente the to_string method. Whats the fastest way? Stringstreams. But I have to use C++ without any headers, so I need to implement the stringstream class. How can an stringstream hold one float? An double? Hoq cqn I implement an strigstream myself?

View 8 Replies View Related

C :: Importing Text File To Structure

Nov 21, 2013

I am trying to take a Text File mydata.txt and import it into a structure however when I try to run this code it just closes out.

Code:
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct hard_disk{
char name[18];
double size;

[Code] .....

View 4 Replies View Related

C++ :: How Many Streams With A File

Sep 14, 2014

I wanted to know if i could use two streams with a file

fstream fS;
ifstream read;
fs.open("text.txt", ios::in | ios::out | ios::app);
read.open("text.txt", ios:: in);

my aim is to read a record from text.txt in to fs and compare it with other records of text.txt by read

bool checkID(char userID, inStream read);
read.open("text.txt", ios::in);
while (read.good()){
read.getline(id, size);
if(strcmp (id, userID)==0)
return true;
}
return false;

View 8 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 :: How To Do Arithmetic On Structure Members Read From A Text File

May 19, 2014

I am trying to use arithmetic on structure members read from separate text files into separate functions. I'm trying to do the math in the main function after calling the other two functions. I'm trying to divide nato_attack by pact_defence.

The warning I'm getting is: both are being used uninitialized in this function.

The goal is to read/write the info in the text files and to use them as any other variable, i.e. add, subtract, etc. in other locations in my code, i.e. functions, modules.

I "borrowed" most of this code from a youtube lesson and modified it for my needs.

Below is the contents of the two text files and my code.

This is for NATO
6
12 4th_mech_div mech div 14 16 7
12 5th_mech_div mech div 10 8 7
12 3rd_mech_div mech div 5 6 6
12 1st_mech_div mech div 4 6 7
12 2nd_mech_div mech div 12 14 7
12 6th_mech_div mech div 8 12 6

This is for Pact
3
10 1st_Guards armor div 12 8 6
10 2nd_Guards armor div 12 9 6
10 3rd_Guards mechanized div 10 9 6

Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *unit_id;
char *unit_type;
char *unit_size;

[Code] ....

View 9 Replies View Related

C/C++ :: How To Assign Values From Text File To Structure Of Arrays

Sep 5, 2014

have to do an election program in C.

There are 7 candidates and 365 votes in total. I need to do this using an array of structures. I need to read from a text file each of the names of the candidate and the number of votes they get. At the end i need to output the winner of the election.

Here is a sample of my code so far

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct candidates {
char name[20];
int votes;

[code]....

Here is a sample of my text file:

Robert Bloom
John Brown
Michelle Dawn
Michael Hall
Sean O’Rielly
Arthur Smith
Carl White

1 2 4 5 1 2 3 4 4 1 2 3 7 4 4 5 3 7 7 7 7 7 7 7 7 7

Each candidate gets +1 vote for their number electionCandidate[0] for each one he gets one vote and so on for the rest. 365 voters in total.

I was able to input the name for each Candidate from the text file. Now the problem is putting each vote to the corresponding candidate. Also any vote that is above 7 is a spoilt vote which i am trying to count in the above code. The code compiles but it crashes.

I am using while(!feof) but it seems that its not working or this is not the correct way.

View 6 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/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

C/C++ :: Simultaneous Reading And Write From And To Text File Using Multithreading

Feb 13, 2013

Simultaneous Reading and write from and to a text file using Multithreading in c/C++

View 1 Replies View Related

C/C++ :: Write A Program That Reads Four Float Numbers From The Input Text File

Apr 3, 2015

I need to write a program that reads four float numbers from the input.txt file, then it prints out the greatest of the four numbers into the output.txt file. I did everything, but the numbers don't print out.

#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;
ofstream outFile;
float number1, number2, number3, number4;

[Code]...

View 2 Replies View Related

C :: How To Write In DOS Encoding Using Class FILE

Dec 12, 2013

How to write in DOS encoding using class FILE ?

View 5 Replies View Related

C++ ::  Write Hex In Text File To Other File

Jul 28, 2014

getting hex from a text file and writing it to a different file. I made a program to dump hex from files into a text file and now I need it to save the hex to the original file. Here is my code to save the hex:

int main(int argc, char * argv[]) {
ofstream myFile;
ifstream file(argv[1], ios::in);

[Code].....

It never outputs the correct contents of the file that has been dumped. I've checked with other hex editors and the hex dumper is working correctly.

View 19 Replies View Related

C++ :: Using FILE Class To Write Into Txt File In DOS Codepage

Dec 8, 2013

I'm using

CString text;
CString file_name;
text = "My text.";
file_name = "MyFile.txt";
FILE *fp;
fp = fopen(file_name, "w+");
fprintf(fp, text + "
");
fclose(fp);

There will be MyFile.txt in Windows codepage.

How to write to txt file with DOS codepage?

View 9 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++ :: Write Program For Class That Reads From Input File?

Mar 7, 2013

I am trying to write a program for class that reads from an input file the month, year & total collected. I am using notepad for the input file.

This is what the notepad file looks like
-----------------------------------
March 2013 63924.62

Why does it give me random numbers in the output rather than what the notepad has?

Also, the outfile is completely blank.

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

[Code].....

View 2 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++ :: Class Instances To Text File

Mar 29, 2015

Code:
#include<iostream>
#include<conio.h>
#include<string.h>
#include<ostream>
#include<fstream>
#include<iomanip>
using namespace std;
class MathProblem {

[Code] ...

So my program is quite simple, have the user answer the answer to a question, and then compare with the correct answer. I needed to implement an inheritance of MathProblem in my second class aswell.

The program runs as I intend but I wish to have the data forwarded to a text file. After that I must read back to the command prompt the text file contents. It's fairly easy for simple statements but I don't understand how to forward all my data from my classes.

View 3 Replies View Related

C++ :: Read Info From Text File Into Class?

May 22, 2014

I'm having trouble in getting my program to read from a file and put all the proper data into its proper class variables. I have a class (called Champion) that has string variable for a name and a vector of strings for items. I also have a vector of Champion that holds multiple champions. Here's my code:

Champion.hpp
#ifndef CHAMPION_HPP_INCLUDED
#define CHAMPION_HPP_INCLUDED
#include <iostream>

[Code].....

View 3 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# :: Method Of Mapping Text File Dictionary To Class Fields

Jan 21, 2014

So I'm rewriting an old project of mine, and I'm trying to determine if there's truly any better way to map the data taken from a text file "dictionary" into the correct class fields for further processing. For example:

FNAME=MY_FIRST_NAME
ADDR=123 SOMEWHERE LN, NOWHERESVILLE, TX 01234
LNAME=MY_LAST_NAME
TOTCALLS=47

In each of these, I'd need the "value" (MY_FIRST_NAME, MY_LAST_NAME, etc) from the "keys" (FNAME, LNAME, etc) to be mapped to the proper class fields. Say, for example, I had this:

Class DataProcessing {
public string Address;
public string FirstName;
public string LastName;
public int TotalCalls;
...
}

I would need DataProcessing.Address to be set to the value in the ADDR key/value pair. The same would be true for each other field. The problem is that based on the text file's source (which isn't under my control, and won't be changed anytime soon), the key/value pairs are not always in the same place...so a second file could have the data as such:

TOTCALLS=47
ADDR=123 SOMEWHERE LN, NOWHERESVILLE, TX 01234
LNAME=DARKPOETCC'S LAST NAME
FNAME=DARKPOETCC'S FIRST NAME

Any smarter way to do this than looping through each line that was read in from the file, and determining where it belongs, such as (pseudo code follows):

IF FieldName == "TOTCALLS" THEN
//Assign to TotalCalls field
ELSEIF FieldName == "ADDR" THEN
//Assign to Address field
ELSEIF FieldName == (You get the picture...)
//Do thing N_Field

View 9 Replies View Related

C++ :: Write A Class Definition For A Fraction Class

Nov 25, 2013

Write a class definition for a Fraction class. Its member fields are num and den, both of type int. The constructor builds the default fraction 1/1. It has the following operations:

void plusEquals(Fraction second);
//Adds the second fraction to this fraction like the operator += void minusEquals (Fraction second);
//Subtracts the second fraction from this fraction void timesEquals (Fraction second);
//Divides this fraction by the second fraction void dividesEquals (Fraction second);
// Divides this fraction by the second fraction void reduce();
// Reduces this fraction to lowest terms double todecimal();
//returns the decimal value of this fraction void scan(istream&);
//scans a fraction written with a slash as in ¾ void print(ostream&);
//prints a fraction using a slash Fraction();
//constructs a default fraction with denominator 1, numerator 0 Fraction(int n, int d); //constructs a fraction given value for num and den

2. Write a menu-driven driver program designed to allow thorough testing of your Fraction class.

View 2 Replies View Related

C :: Predictive Text Program - Data Structure

Jan 19, 2014

Been given an assignment to create a predictive text program. What data structure I should use for this and the steps I should take when I make the program?

View 12 Replies View Related

C++ :: I/O Streams And Data

Apr 27, 2012

The goal is to write a c++ program to read the employee.dat(which is this text file below) file created and produce a duplicate copy of the file named employee.bak.

Anthony A 10031 11.82 12182010
Burrows W 10067 12.14 692011
Fain B 10083 10.79 5182011
Janney P 10095 12.57 9282008
Smith G 10105 9.50 12202006

and then the second question is to modify this program to accept the names of the original and duplicate files as user input.

#include <iostream>
#include <fstream>
#include <cstdlib> // for exit
#include <string>
using namespace std;
int main() {
ifstream inFile;
ofstream outFile;

[Code] .....

View 1 Replies View Related







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