I am trying to read from a file and output data to a separate file. Only problem is that when I run the program the output on the file is from the data on the last line of the input file. I also don't want to append the output file since I don't want the data to duplicate when I rerun the program which is why i'm not using ios::app in the output.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//class declaration
class call_record{
public:
string cell_number;
int relays;
int call_length;
double net_cost;
double tax_rate;
double call_tax;
double total_cost;
};
//Prototypes
void input(ifstream & in, call_record &);
void process(call_record &);
void output(call_record &);
//Input function
void input(ifstream & in, call_record & customer_record){
in >> customer_record.cell_number >> customer_record.relays >>
customer_record.call_length;
}
//Process function
void process(call_record & customer_record){
if(customer_record.relays >= 0 && customer_record.relays <=5){
customer_record.tax_rate = 0.01;
} else if(customer_record.relays >= 6 && customer_record.relays <=11){
customer_record.tax_rate = 0.03;
} else if(customer_record.relays >= 12 && customer_record.relays <=20){
customer_record.tax_rate = 0.05;
} else if(customer_record.relays >= 21 && customer_record.relays <=50){
customer_record.tax_rate = 0.12;
}
//net cost of call
customer_record.net_cost = (customer_record.relays/50 * 0.40 *
customer_record.call_length);
//cost of tax on call
customer_record.call_tax = customer_record.net_cost *
customer_record.tax_rate;
//Total cost of call
customer_record.total_cost = customer_record.net_cost +
customer_record.call_tax;
}
void output(call_record & customer_record){
ofstream out("weekly_call_info.txt");
out.setf(ios::showpoint);
out.precision(2);
out.setf(ios::fixed);
out << customer_record.cell_number << " " << customer_record.relays << " "
<< customer_record.call_length << " " << customer_record.net_cost << " "
<< customer_record.tax_rate << " " << customer_record.call_tax << " " <<
customer_record.total_cost << endl;
out.close();
}
int main(){
call_record customer_record;
ifstream in("call_data.txt");
if(in.fail()){
cout << "Your input file does not exist or did not open properly." <<
endl;
} else {
while (!in.eof()){
input(in, customer_record);
process(customer_record);
output(customer_record);
}
}
in.close();
return 0;
}
This is different than the other posts because I am using both an input stream and an output stream in 2 separate functions and I don't think I fully understand how to implement them correctly in my main function.
The problem is that you are always creating weekly_call_info.txt each time output() is called and truncating the existing file. This is because you are not specifying the std::ios_base::app ("append") open mode flag, so the existing content of weekly_call_info.txt is lost. See this page for a helpful table that shows the equivalent C-style mode strings for the different combinations of open mode flags: http://en.cppreference.com/w/cpp/io/basic_filebuf/open
I suggest passing a reference to the std::ostream to which you want to output data to the output() function:
void output(call_record & customer_record, std::ostream& out){
out << customer_record.cell_number << " " << customer_record.relays << " "
<< customer_record.call_length << " " << customer_record.net_cost << " "
<< customer_record.tax_rate << " " << customer_record.call_tax << " " <<
customer_record.total_cost << endl;
}
int main(){
call_record customer_record;
ifstream in("call_data.txt");
if(in.fail()){
cout << "Your input file does not exist or did not open properly." << endl;
} else {
ofstream out("weekly_call_info.txt");
out.setf(ios::showpoint);
out.precision(2);
out.setf(ios::fixed);
for (;;) {
input(in, customer_record);
if (!in) break;
process(customer_record);
output(customer_record, out);
}
}
return 0;
}
If you really do want to open weekly_call_info.txt for writing multiple times, you will want to use the std::ios_base::app open mode flag to append to the existing file. See appending to a file with ofstream.
Related
I've written a readFile function for a project I'm working on. I call it once, load in a file and read in it's contents - works fine
However, when I try to load it a second time, attempting to change the file name - it loads it in, saves it to a static string 'path' that I access in a different function - but then the function is not printing the data
The question is, how do I change the file name, and read it in successfully on the second iteration? The part that has me stumped is that it works once, but not twice
Ive attempted to use cin.ignore(); cin.clear(); cin.sync() on the second iteration of fileName function - but none of them allow a separate file to be read successfully.
Minimum Reproducible Example:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include <iomanip>
#include <iostream>
using namespace std;
static string path;
string opt;
void readFile();
int fileName();
void menu() { // put in while loop - while True
cout << "----------------------" << endl;
cout << "R(ead) -" << "Read File" << endl;
cout << "F(ile) -" << "Set Filename" << endl;
cout << "\nPlease select from the above options" << endl;
cin >> opt;
cout << "\nInput entered: " << opt << endl;
if (opt == "R") {
readFile();
}
if (opt == "F") {
fileName();
}
}
void readFile() { // doing this twice
ifstream readFile;
readFile.open(path);
if (!readFile.is_open()) {
cout << "Could not read file" << endl;
}
string str;
int i = 0;
while (getline(readFile, str))
{
if (str[0] != '/')
{
cout << "DEBUG: Line is - " << str << endl;
}
}
readFile.clear();
readFile.close();
menu();
}
int fileName() {
cout << "File path: ";
if (path != "") {
cin.ignore();
cin.clear();
cin.sync();
}
getline(cin, path);
ifstream file(path.c_str());
if (!file) {
cout << "Error while opening the file" << endl;
return 1;
}
cout << "(File loaded)" << endl;
cout << "Path contains: " << path << endl;
file.clear();
file.close();
menu();
}
int main()
{
fileName();
}
Sample text, saved as txt file and read in using path:
Data1.txt
// standard test file
123,Frodo inc,2006, lyons,"1,021,000.16",0.0,
U2123,Sam Inc,2006, lyons,"21,600.00",13.10,123
A721,Merry Inc,2604, Kingston,"21,600.10",103.00,
U2122,Pippin Inc,2612, reid,"21,600.00",0
U1123,Huckelberry corp,2612, Turner,"21,600.00",13.10,
Data2.txt
7101003,Mike,23 boinig road,2615,48000,12000,0
7201003,Jane Philips,29 boinig cresent,2616,47000,12000,0
7301003,Philip Jane,23 bong road,2615,49000,000,0
7401004,Peta,23 bong bong road,2615,148000,19000,0
7101205,Abdulla,23 Station st,2615,80000,21000,0
The problem comes from reading in one, and trying to read in the other after the first has been executed.
Enter Filename
Hit Readfile
Return to menu, hit Set Filename
Change to Data2.txt
Readfile again. Not working
My tutor told me "That's not how functions work in c++" but didn't elaborate further, and is unavailable for contact.
In general, do not use global variables. The path variable should be passed as a parameter, not kept as a global variable altered between function calls, as this leads to many side effects and is the source of countless bugs. See the following refactoring:
void menu() { // put in while loop - while True
while(true)
{
//Keep this as a local variable!
std::string opt;
std::string filename;
cout << "----------------------\n";
cout << "R(ead) -" << "Read File\n";
cout << "F(ile) -" << "Set Filename\n";
cout << "\nPlease select from the above options\n";
cin >> opt;
cout << "\nInput entered: " << opt << '\n';
if (opt == "R") {
readFile(filename);
}
if (opt == "F") {
filename = getFileName();
}
}
}
void readFile(const std::string & filename) {
ifstream readFile;
readFile.open(filename);
if (!readFile.is_open()) {
cout << "Could not read file " << filename << '\n';
}
string str;
int i = 0;
while (getline(readFile, str))
{
if (str[0] != '/')
{
cout << "DEBUG: Line is - " << str << '\n';
}
}
readFile.close();
//just return to get back to menu
return;
}
std::string getFileName() {
cout << "File path: ";
std::string path;
getline(cin, path);
ifstream file(path.c_str());
if (!file) {
cout << "Error while opening the file" << '\n';
//Instead of returning an error code use an exception preferably
}
cout << "(File loaded)" << '\n';
cout << "Path contains: " << path << '\n';
file.close();
return path;
}
Other notes:
Ideally, do input in output in just one function, not all three as it gets confusing exactly what each function is responsible for.
If you want something to hold a file and print the contents, you can use an class.
The file is checked if it is openable twice, not really any reason to do this just delegate that responsibility to one function.
One of the best things about C++ is RAII and deterministic lifecycles for objects and primitives - use it!! Do not give everything a long life with global variables - use smart parameters and return values instead.
I am working on a text editor project for the 3ds and I am working on adding a scrolling function I have found a way for the text to scroll up by doing this
if (kHeld & KEY_UP)
{
cout << endl;
}
but for a basic implementation of the text scrolling down I need to insert a white space at the top of the imputed text I have tried using the insert() function but it needs to output the text again to show the inserted whitespace this is a large problem I have been stuck on for days and any suggestions you are willing to provide would be great here is my full source code if it will help thanks
// T3XTPAD-V1.0
#include <iostream>
#include <fstream>
#include <string>
#include <3ds.h>
using namespace std;
string Output = "";
int main()
{
gfxInitDefault();
PrintConsole topScreen, bottomScreen;
consoleInit(GFX_TOP, &topScreen);
consoleInit(GFX_BOTTOM, &bottomScreen);
consoleSelect(&bottomScreen);
cout << endl;
cout << "T3XTPAD-V1.0" << endl;
cout << endl;
cout << "[A] Edit:" << endl;
cout << endl;
cout << "[B] Save:" << endl;
cout << endl;
cout << "[Y] Open:" << endl;
cout << endl;
cout << "[UP/DOWN] Scroll:" << endl;
cout << endl;
consoleSelect(&topScreen);
while (aptMainLoop())
{
hidScanInput();
u32 kHeld = hidKeysHeld();
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
{
break;
}
static SwkbdState swkbd;
static char Edit[999999];
static char Save[999999];
static char Open[999999];
SwkbdButton button = SWKBD_BUTTON_NONE;
if (kDown & KEY_A)
{
swkbdInit(&swkbd, SWKBD_TYPE_NORMAL, 2, -1);
swkbdSetHintText(&swkbd, "Edit:");
button = swkbdInputText(&swkbd, Edit, sizeof(Edit));
cout << Edit << endl;
}
if (kDown & KEY_B)
{
swkbdInit(&swkbd, SWKBD_TYPE_NORMAL, 2, -1);
swkbdSetHintText(&swkbd, "Save:");
button = swkbdInputText(&swkbd, Save, sizeof(Save));
ofstream MyFile (Save);
if (MyFile.is_open())
{
MyFile << Edit << endl;
MyFile.close();
}
}
if (kDown & KEY_Y)
{
swkbdInit(&swkbd, SWKBD_TYPE_NORMAL, 2, -1);
swkbdSetHintText(&swkbd, "Open:");
button = swkbdInputText(&swkbd, Open, sizeof(Open));
ifstream MyFile (Open);
if (MyFile.is_open())
{
while (getline (MyFile, Output))
{
cout << Output << endl;
}
MyFile.close();
}
}
if (kHeld & KEY_UP)
{
cout << endl;
}
if (kHeld & KEY_DOWN)
{
cout << endl;
}
gfxFlushBuffers();
gfxSwapBuffers();
gspWaitForVBlank();
}
gfxExit();
return 0;
}
the program should read from 2 files (author.dat and citation.dat) and save them into a map and set;
first it reads the citationlist without problem, then it seems to properly read the authors and after it went through the whole list (author.dat) a floating point exception arises .. can't quite figure out why
seems to happen in author.cpp inside the constructor for authorlist
author.cpp:
#include <fstream>
#include <iostream>
#include "authors.h"
using namespace std;
AuthorList::AuthorList(char *fileName) {
ifstream s (fileName);
int idTemp;
int nrTemp;
string nameTemp;
try {
while (true){
s >> idTemp >> nrTemp >> nameTemp;
cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
if (!s){
cout << "IF-CLAUSE";
throw EOFException();
}
cout << "WHILE-LOOP_END" << endl;
}
} catch (EOFException){}
}
author.h:
#ifndef CPP_AUTHORS_H
#define CPP_AUTHORS_H
#include <iostream>
#include <map>
#include <string>
#include "citations.h"
class Author {
public:
Author (int id, int nr, std::string name) :
articleID(id),
authorNR(nr),
authorName(name){}
int getArticleID() const {
return articleID;
}
std::string getAuthorName() const {
return authorName;
}
private:
int articleID;
int authorNR;
std::string authorName;
};
class AuthorList {
public:
AuthorList(char *fileName);
std::pair<std::multimap<int,Author>::const_iterator, std::multimap<int,Author>::const_iterator> findAuthors(int articleID) {
return authors.equal_range(articleID);
}
private:
std::multimap<int,Author> authors;
};
#endif //CPP_AUTHORS_H
programm.cpp:
#include <iostream>
#include <cstdlib>
#include "citations.h"
#include "authors.h"
#include "authorCitation.h"
using namespace std;
int main(int argc, char *argv[]){
CitationList *cl;
AuthorList *al;
//check if argv array has its supposed length
if (argc != 4){
cerr << "usage: programm article.dat citation.dat author.dat";
return EXIT_FAILURE;
}
//inserting citation.dat and author.dat in corresponding lists (article.dat not used)
cl = new CitationList(argv[2]);
al = new AuthorList(argv[3]);
try {
AuthorCitationList *acl;
acl->createAuthorCitationList(al,cl);
acl->printAuthorCitationList2File("authorcitation.dat");
} catch (EOFException){
cerr << "something went wrong while writing to file";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
All files:
https://drive.google.com/file/d/0B734gx5Q_mVAV0xWRG1KX0JuYW8/view?usp=sharing
I am willing to bet that the problem is caused by the following lines of code:
AuthorCitationList *acl;
acl->createAuthorCitationList(al,cl);
You are calling a member function using an uninitialized pointer. I suggest changing the first line to:
AuthorCitationList *acl = new AuthorCitationList;
Add any necessary arguments to the constructor.
While you are at it, change the loop for reading the data also. You have:
while (true){
s >> idTemp >> nrTemp >> nameTemp;
cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
if (!s){
cout << "IF-CLAUSE";
throw EOFException();
}
cout << "WHILE-LOOP_END" << endl;
}
When you do that, you end up adding data once after the end of line has been reached. Also, you seem to have the last line in the wrong place. It seems to me that it should be outside the while loop.
You can use:
while (true){
s >> idTemp >> nrTemp >> nameTemp;
// Break out of the loop when reading the
// data is not successful.
if (!s){
cout << "IF-CLAUSE";
throw EOFException();
}
cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
}
cout << "WHILE-LOOP_END" << endl;
You can simplify it further by using:
while (s >> idTemp >> nrTemp >> nameTemp){
cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
}
cout << "WHILE-LOOP_END" << endl;
The code below does not work under Windows and GNU C++, VS10,VS12, Intel C++ 14.0. The code below does work under Linux and GNU C++ 4.7, 4.8, Intel C++ 14, Open64 5.0.Replacing DIMEN with DIMEN-256 in the inner test for-loop ... works!? Any idea?
//============================//
// Read and Write binary file //
// using buffers //
//============================//
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
// 1. variables and parameters
const long int DIMEN = static_cast<long int>(pow(10.0,8.0));
const long int I_DO_MAX = 100;
const string fileName = "my_file.bin";
ofstream fileOUT;
ifstream fileIN;
double* myArrayAlpha = new double [DIMEN];
double* myArrayBeta = new double [DIMEN];
long int i;
long int j;
// 2. build the array with some data
cout << " 1 --> Build the array with some data" << endl;
for (i = 0; i < DIMEN; i++)
{ myArrayAlpha[i] = static_cast<double>(i); }
for (i = 0; i < I_DO_MAX; i++)
{
// 3. open the file stream
cout << "-------------->>> " << i << endl;
cout << " 2 --> Open the file stream" << endl;
fileOUT.open(fileName.c_str(), ios::out | ios::binary | ios::trunc);
fileIN.open(fileName.c_str(), ios::in | ios::binary);
// 4. test if the file stream is opened
cout << " 3 --> Test if the file stream is opened with success" << endl;
if (!fileOUT.is_open())
{ cout << "Error! The output file stream is not opened. Exit."
<< endl; return -1; }
if (!fileIN.is_open())
{ cout << "Error! The input file stream is not opened. Exit."
<< endl; return -1; }
// 5. write the contents of myArrayAlpha[] to a file
cout << " 4 --> Write and then Read to the file" << endl;
fileIN.seekg(0, fileIN.beg);
fileOUT.seekp(0);
fileOUT.write(reinterpret_cast<char*>(&myArrayAlpha[0]),
DIMEN * sizeof(double));
fileIN.read(reinterpret_cast<char*>(&myArrayBeta[0]),
DIMEN * sizeof(double));
// 6. test that I am writting and reading correctly
for (j = 0; j < DIMEN; j++) // replace DIMEN
{ // with DIMEN-256 to work under Windows
if (myArrayAlpha[j] != myArrayBeta[j])
{ cout << myArrayAlpha[j] << endl;
cout << myArrayBeta[j] << endl;
cout << "j = " << j << endl;
cout << "Error!"; return -1; }
}
cout << " 5 --> Read and Write with success" << endl;
cout << " 6 --> Close the I/O streams" << endl;
// 7. close the file stream
fileIN.close();
fileOUT.close();
}
// 8. free up the RAM
delete [] myArrayAlpha;
delete [] myArrayBeta;
return 0;
}
The problem is that your data is not being flushed to the external sequence after the write call, so it is still positioned in the internal buffer. Add this line after write():
fileOUT << std::flush;
I am trying to load a text file and import the contents into a vector of structs.
Here are my definitions
typedef struct
{
string pcName, pcUsername, pcPassword, pcMessage, pcAdvertisement; //I know that
//this is incorrect convention. It was originally a char*
}
ENTRY;
vector<ENTRY> entries;
fstream data;
Here is my display data function
void DisplayData()
{
std::cout << (int)(entries.size() / 5) <<" entries" << endl;
for(int i = 1; i <=(int)entries.size()/5; i++)
{
cout << endl << "Entry " << i << ":" << endl
<< "Name: " << entries[i].pcName << endl
<< "Username: " << entries[i].pcUsername << endl
<< "Password: " << entries[i].pcPassword << endl
<< "Message: " << entries[i].pcMessage << endl
<< "Advertisement: " << entries[i].pcAdvertisement << endl;
}
}
and here is my Load Data function
bool LoadData(const char* filepath)
{
std::string lineData ;
int linenumber = 1 ;
data.open(filepath, ios::in);
ENTRY entry_temp;
if(!data.is_open())
{
cerr << "Error loading file" << endl;
return false;
}
while(getline(data, lineData))
{
if(linenumber==1) {entry_temp.pcName = lineData;}
else if(linenumber==2) {entry_temp.pcUsername = lineData;}
else if(linenumber==3) {entry_temp.pcPassword = lineData;}
else if(linenumber==4) {entry_temp.pcMessage = lineData;}
else if(linenumber==5) {entry_temp.pcAdvertisement = lineData;}
entries.push_back(entry_temp);
if(linenumber == 5)
{
linenumber = 0;
}
linenumber++;
}
data.close();
puts("Database Loaded");
return true;
}
Here is the text file I am loading:
Name1
Username1
Password1
Message1
Ad1
And here is the result of the display data function after calling load data:
1 entries
Entry 1:
Name: Name1
Username Username1
Password:
Message:
Advertisement:
As you can see, the first two load but the last three don't. When I did this with an array instead of a vector, it worked fine so I don't know what I'm doing wrong. Thanks.
I suggest that you read each line directly into the data field where it goes:
getline(data, entry_temp.pcName);
getline(data, entry_temp.pcUsername);
getline(data, entry_temp.pcPassword);
getline(data, entry_temp.pcMessage);
getline(data, entry_temp.pcAdvertisement);
entries.push_back(entry_temp);
This makes your intent much clearer than your current while loop. It also creates a single entry for all 4 input lines rather than one for each input line (with the other three blank). Now you can read several "entries" by using a while loop that checks if you have reached the end of the file.
Doing this will also make printing out the data much easier since the vector will have exactly the number of entries rather than five times as many as you expect (which also eats up a lot more memory than you need to).
Your DisplayData function is a little weird, and so is your LoadData.
Your LoadData pushes back a new copy of the current ENTRIES entry with every line. Your DisplayData starts at 1 (which is not the beginning of any vector or array), and iterates only up to the 1/5th entry of the entire vector.
This needs a heavy rework.
First, the size() member of any standard container returns the number of elements that it contains, and will not take the number of fields in a contained struct into account.
For future reference, you'll want to post your question in a complete, standalone example that we can immediately compile to help. (see http://sscce.org/)
Try this modified data, which runs correctly, and see if you can tell what is being done differently:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
typedef struct
{
string pcName, pcUsername, pcPassword, pcMessage, pcAdvertisement;
}
ENTRY;
vector<ENTRY> entries;
fstream data;
bool LoadData(const char* filepath)
{
std::string lineData ;
int linenumber = 1 ;
data.open(filepath, ios::in);
ENTRY entry_temp;
if(!data.is_open())
{
cerr << "Error loading file" << endl;
return false;
}
while(getline(data, lineData))
{
if(linenumber==1) {entry_temp.pcName = lineData;}
else if(linenumber==2) {entry_temp.pcUsername = lineData;}
else if(linenumber==3) {entry_temp.pcPassword = lineData;}
else if(linenumber==4) {entry_temp.pcMessage = lineData;}
else if(linenumber==5) {entry_temp.pcAdvertisement = lineData;}
if(linenumber == 5)
{
entries.push_back(entry_temp);
linenumber = 0;
}
linenumber++;
}
data.close();
puts("Database Loaded");
return true;
}
void DisplayData()
{
std::cout << entries.size() <<" entries" << endl;
for(int i = 0; i < entries.size(); i++)
{
cout << endl << "Entry " << i << ":" << endl
<< "Name: " << entries[i].pcName << endl
<< "Username: " << entries[i].pcUsername << endl
<< "Password: " << entries[i].pcPassword << endl
<< "Message: " << entries[i].pcMessage << endl
<< "Advertisement: " << entries[i].pcAdvertisement << endl;
}
}
int main()
{
LoadData("/tmp/testdata");
DisplayData();
return (0);
}
While I think #code-guru has the right idea, I'd take the same idea just a little further, and make your code work a little more closely with the standard library. I'd do that by reading a data item with a stream extractor, and displaying it with stream inserter. So, the extractor would look something like this:
std::istream &operator>>(std::istream &is, ENTRY &e) {
getline(is, e.pcName);
getline(is, e.pcUsername);
getline(is, e.pcPassword);
getline(is, e.pcMessage);
getline(is, e.pcAdvertisement);
return is;
}
..and the inserter would look something like this:
std::ostream &operator<<(std::ostream &os, ENTRY const &e) {
os << e.pcName << "\n";
os << e.pcUsername << "\n";
os << e.pcPassword << "\n";
os << e.pcMessage << "\n";
os << e.pcAdvertisement << "\n";
return os;
}
With those in place, loading and displaying the data becomes fairly straightforward.
Load the data:
std::ifstream in("yourfile.txt");
std::vector<ENTRY> data((std::istream_iterator<ENTRY>(in)),
std::istream_iterator<ENTRY>());
Display the data:
for (auto const & e: data)
std::cout << e << "\n";
For the moment, I haven't tried to duplicate the format you were using to display the data -- presumably the modifications for that should be fairly obvious.