Avoiding the use of eof in this case - c++

I have posted the following code where I am reading from an input file -- storing information in a structure -- and then writing to an output file. I know that the eof function is not safe and hence one must use the getline function to check whether the end of file has been detected or not; however, in this particular code, I have not been able to use the getline function and hence has finally relied on the eof function. Hence, can you please suggest an alternative to the eof function or let me know how I can use the getline function when I am trying to initialize an array of structures . I have used two asterisk symbols to indicate where I want to use the getline function.
#include <iostream>
#include <fstream>
using namespace std;
//student structure
struct student
{
char name[30];
char course[15];
int age;
float GPA;
};
ifstream inFile;
ofstream outFile;
student getData();
void writeData(student writeStudent);
void openFile();
int main (void)
{
const int noOfStudents = 3; // Total no of students
openFile(); // opening input and output files
student students[noOfStudents]; // array of students
// Reading the data from the file and populating the array
for(int i = 0; i < noOfStudents; i++)
{
if (!inFile.eof()) // ** This where I am trying to use a getline function.
students[i] = getData();
else
break ;
}
for(int i = 0; i < noOfStudents; i++)
writeData(students[i]);
// Closing the input and output files
inFile.close ( ) ;
outFile.close ( ) ;
}
void openFile()
{
inFile.open("input.txt", ios::in);
inFile.seekg(0L, ios::beg);
outFile.open("output.txt", ios::out | ios::app);
outFile.seekp(0L, ios::end);
if(!inFile || !outFile)
{
cout << "Error in opening the file" << endl;
exit(1);
}
}
student getData()
{
student tempStudent;
// temp variables for reading the data from file
char tempAge[2];
char tempGPA[5];
// Reading a line from the file and assigning to the variables
inFile.getline(tempStudent.name, '\n');
inFile.getline(tempStudent.course, '\n');
inFile.getline(tempAge, '\n');
tempStudent.age = atoi(tempAge);
inFile.getline(tempGPA, '\n');
tempStudent.GPA = atof(tempGPA);
// Returning the tempStudent structure
return tempStudent;
}
void writeData(student writeStudent)
{
outFile << writeStudent.name << endl;
outFile << writeStudent.course << endl;
outFile << writeStudent.age << endl;
outFile << writeStudent.GPA << endl;
}

You want to write an operator>> for your student type. Something like:
std::istream& operator>>(std::istream& in, student& s) {
in >> s.age; // etc.
return in;
}
Which then allows you to write:
int studentNo = 0;
students[maxStudents];
while (studentNo < maxStudents && (in >> students[studentNo]))
++studentNo;

Why not write this way?
instead of
inFile.getline(tempStudent.name, '\n');
inFile.getline(tempStudent.course, '\n');
inFile.getline(tempAge, '\n');
You may
while(inFile.getline(tempStudent.name, '\n'))
{
inFile.getline(tempStudent.course, '\n');
inFile.getline(tempAge, '\n');
//do stuffs
}

Related

I need to write a program that reads a file which will output all unique integers, if the integer has been seen already it will be dismissed

#include<iostream>
#include<fstream>
using namespace std;
void read_file(fstream &file);
int main()
{
fstream inFile;
inFile.open("Data.txt");
if (inFile.fail())
{
cerr << "Error with opening file";
exit(1);
}
else
{
read_file(inFile);
}
inFile.close();
return 0;
}
void read_file(fstream &file)
{
int arr[100];
fstream inFile;
int number;
int number_trash;
int number_hold;
while (!inFile.eof())
{
for (int i = 0; i < 101; i++)
{
inFile >> number;
number_hold = number;
if (number != number_hold)
{
arr[i] = number;
cout << arr[i] << endl;
}
else
{
number_trash = number;
}
}
}
}
In your read_file() function, you're passing an fstream instance of an already open file, which is correct, however, later in the same function, you declare a new instance of fstream called inFile which is not open and you're trying to read from this file stream.
Remove the fstream inFile and read from the file which your function takes as an argument.
Also, your algorithm is not correct - the first if statement condition will be always evaluated to false. You're assigning number to number_hold and then you're checking for their non-equality.
As a solution, consider something like this:
void read_file(fstream &file)
{
set<int> arr; // storage for your unique numbers
while (!file.eof())
{
int number;
file >> number; // read the number
// check if this number is already in your unique list
if (arr.find(number) == arr.end()) { // If it isn't, print it out...
cout << number << endl;
arr.insert(number); // ...and put it to your unique list
}
}
}
Note that for this to work you have to include another header file called set
#include <set>

How do I get an input file to read into a string array in C++?

For some reason the full lines from my input file are not reading into the array, only the first word in each line. I am currently using the getline call, but I am not sure why it is not working. Here is the what I have for the call to populate the array. The txt file is a list of songs.
const int numTracks = 25;
string tracks[numTracks];
int count = 0, results;
string track, END;
cout << "Reading SetList.txt into array" << endl;
ifstream inputFile;
inputFile.open("SetList.txt");
while (count < numTracks && inputFile >> tracks[count])
{
count++;
getline(inputFile, track);
}
inputFile.close();
while (count < numTracks && inputFile >> tracks[count])
The >> operator reads a single word. And this code reads this single word into the vector in question.
getline(inputFile, track);
True, you're using getline(). To read the rest of the line, after the initial word, into some unrelated variable called track. track appears to be a very bored std::string that, apparently, gets overwritten on every iteration of the loop, and is otherwise completely ignored.
Your loop is using the operator>> to read the file into the array. That operator reads one word at a time. You need to remove that operator completely and use std::getline() to fill the array, eg:
const int numTracks = 25;
std::string tracks[numTracks];
int count = 0;
std::cout << "Reading SetList.txt into array" << std::endl;
std::ifstream inputFile;
inputFile.open("SetList.txt");
while (count < numTracks)
{
if (!std::getline(inputFile, tracks[count])) break;
count++;
}
inputFile.close();
Or:
const int numTracks = 25;
std::string tracks[numTracks];
int count = 0;
std::cout << "Reading SetList.txt into array" << std::endl;
std::ifstream inputFile;
inputFile.open("SetList.txt");
while ((count < numTracks) && (std::getline(inputFile, tracks[count]))
{
count++;
}
inputFile.close();
Alternatively, consider using a std::vector instead of a fixed array, then you can use std::istream_iterator and std::back_inserter to get rid of the manual loop completely:
class line : public std::string {}
std::istream& operator>>(std::istream &is, line &l)
{
return std::getline(is, l);
}
...
std::vector<std::string> tracks;
std::cout << "Reading SetList.txt into array" << std::endl;
std::ifstream inputFile;
inputFile.open("SetList.txt");
std::copy(
std::istream_iterator<line>(inputFile),
std::istream_iterator<line>(),
std::back_inserter(tracks)
);
inputFile.close();

How to tell your program which file you want to read C++

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void read();
int main() {
read();
return 0;
}
void read () {
string file("");
string nameOfFile("");
cin >> nameOfFile;
ifstream in (nameOfFile);
while ( !in.eof() ) {
getline(in, file);
cout << file;
cout << endl;
}
cout << file;
in.close();
}
How come this isn't working, I'm trying to make it so i can type in which file i want to read?
I'm really new to C++, sorry if this is an obvious fix.
You have to change
ifstream in (nameOfFile);
with
ifstream in (nameOfFile.c_str());
because the default constructor for ifstream does not accept a std::string as an argument, it needs a char *. Hence, use the function std::string::c_str() to convert a std::string into a char *.
A little feedback:
void read () {
string file(""); // you don't need the ("") bit; empty by default,
// and "file" is a terrible choice of identifier as
// it sounds more like an ifstream than a string
// used to hold one line from the file.
// I tend to use "string line;" for this.
string nameOfFile(""); // ditto
cin >> nameOfFile; // you should test for success of input, like this:
// if (!cin >> nameOfFile) {
// std::cerr << "error reading filename from stdin\n";
// exit(1);
// }
ifstream in (nameOfFile); // test for success getting file open like this:
// if (ifstream in(nameofFile))
// {
while ( !in.eof() ) { // NEVER check eof before attempting input, instead:
getline(in, file); // while (getline(in, file))
cout << file; // cout << file << endl; // can "chain"
cout << endl; // }
// else
// std::cerr << "couldn't open " << nameOfFile
// << '\n';
} // no need for extra cout nor explicit close, as
cout << file; // the ifstream destructor closes anyway.
in.close();
}
You need to open the ifstream usign in.open(), and hendle the case where file does not exist as well. here is the function:
void read() {
string file("");
string fileContent = "";
string nameOfFile("");
cin >> nameOfFile;
ifstream in(nameOfFile.c_str());
in.open(nameOfFile, ios::in);
if (in){
while (!in.eof()) {
getline(in, file);
fileContent += file;
}
cout << fileContent;
in.close();
}
else {
cout << "Could not open file.";
}
}

error reading data from input file to array

The input file contains 14 state initials (TN,CA,NB,FL,etc..) that is to be rad into the array. The code below clears compiler but when i tell the program the filename it shoots out a bunch of blank spaces with two spaces containing some fuzz and a third contain a '#' symbol. i assume the problem is with my function not entirely sure what specifically though any help greatly appreciated!
input file set up with state initials one on top of the other:
TN
PA
KY
MN
CA
and so on
void readstate( ifstream& input, string []);
int main()
{
string stateInitials[14];
char filename[256];
ifstream input;
cout << "Enter file name: ";
cin >> filename;
input.open( filename );
if ( input.fail())
{
cout << " file open fail" << endl;
}
readstate ( input, stateInitials);
input.close();
return (0);
}
void readstate ( ifstream& input, string stateInitials[])
{
int count;
for ( count = 0; count <= MAX_ENTRIES; count++)
{
input >> stateInitials[count];
cout << stateInitials[count] << endl;
}
}
You are treating a character array as though it were a string array.
While you can hack put the strings next to each other inside the same char array, that is not the standard way it is done. Here is a modified version of your code, which creates one char[] to hold each initial.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <string.h>
#define MAX_ENTRIES 14
using namespace std;
void readstate( ifstream& input, char* []);
int main()
{
char** stateInitials = new char*[14];
char filename[256];
ifstream input;
cout << "Enter file name: ";
cin >> filename;
input.open( filename );
if ( input.fail())
{
cout << " file open fail" << endl;
}
readstate ( input, stateInitials);
// After you are done, you should clean up
for ( int i = 0; i <= MAX_ENTRIES; i++) delete stateInitials[i];
delete stateInitials;
return (0);
}
void readstate ( ifstream& input, char* stateInitials[])
{
int count;
string temp_buf;
for ( count = 0; count <= MAX_ENTRIES; count++)
{
stateInitials[count] = new char[3];
input >> temp_buf;
memcpy(stateInitials[count], temp_buf.c_str(), 3);
cout << stateInitials[count] << endl;
}
}

Is it possible to use an outputfile as an inputfile later on in code (C++ I/O files)

So what I did was this;
ifstream infile("warehouse.txt"); ffile("updated.txt");
ofstream outfile("updated.txt");
basically what I want to do is read from the inputfile 'warehouse.txt' and store contents in an array and then add this array and an extra array to outputfile 'updated.txt'.
Then I want to use 'updated.txt' as an input file as shown in the code above is this allowed, I basically want to store all the data on updated.txt into one big array i.e combine the two arrays, is this allowed? I tried it and my compiler seemed to screw up and I was reading about using vectors instead but am struggling to understand them. thanks.
here is my overall code what I wanted to do was basically take from an input file the 'fruitname' and its corresponding quantity from an input file. store extra entries in an extraarray and then put both these arrays in an output file as stated above and then use that output as an input file so I can aggreagte the data.
THE PROBLEM:
When I try to store from the updated.txt to array my cout's show that I get random numbers in place of what should be the fruitname and its number.
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
typedef struct items {
string name;
int quantity;
} items_t;
void fileopenchecker (ifstream &FILE);
int readfromfile (ifstream &FILE, items_t fruit[]);
int extrarray (items_t fruit[]);
void writetooutputfile (ofstream &OFILE, items_t fruit[], int size);
void combinearrays (ifstream &final, items_t overallfruit[], int total);
int main()
{
const int MAX_SIZE = 150;
int Nfruit = 0;
int Nextrafruit = 0;
int total;
std::ifstream infile("warehouse.txt");
std::ofstream outfile("updated.txt");
std::ifstream ffile("updated.txt");
items_t extrafruit[MAX_SIZE], fruit[MAX_SIZE], overallfruit[MAX_SIZE];
fileopenchecker(infile);
Nextrafruit = extrarray(extrafruit);
Nfruit = readfromfile(infile, fruit);
total = Nextrafruit + Nfruit;
infile.close();
writetooutputfile(outfile, fruit, Nfruit);
writetooutputfile(outfile, extrafruit, Nextrafruit);
combinearrays (ffile, overallfruit, total);
ffile.close();
return 0;
}
void combinearrays (ifstream &final, items_t overallfruit[], int total){
int i;
for(i=0; i<total; i++){
final >> overallfruit[i].name >> overallfruit[i].quantity;
cout << overallfruit[i].name << overallfruit[i].quantity << endl;
}
}
void fileopenchecker (ifstream &FILE){
if(!FILE.is_open()){
cout << "Your file was NOT detected!" << endl;
exit(1);
}
else{
cout << "Your file was detected" << endl;
}
}
int readfromfile (ifstream &FILE, items_t fruit[]){
int entries = 0;
while(!FILE.eof()){
FILE >> fruit[entries].name >> fruit[entries].quantity;
cout << fruit[entries].name << fruit[entries].quantity << endl;
entries++;
}
return entries;
}
int extrarray (items_t fruit[]){
int runner=1, exentries =0;
while(runner==1){
cout << "Would you like to add entries to your file? (YES-->1 NO-->0)" << endl;
cin >> runner;
if(runner==0){
break;
}
//take the itemname and quantity and stores it in the array.
cout << "Enter the name of the fruit and its quantity" << endl;
cin >> fruit[exentries].name >> fruit[exentries].quantity;
//debugging:
cout << fruit[exentries].name << fruit[exentries].quantity << endl;
exentries++;
}
return exentries;
}
void writetooutputfile (ofstream &OFILE, items_t fruit[], int size){
int entries = 0;
while(entries < size){
cout << fruit[entries].name << fruit[entries].quantity << endl;
OFILE << fruit[entries].name << fruit[entries].quantity << endl;
entries++;
}
}
"I want to do is read from the inputfile 'warehouse.txt'"
{
std::ifstream ifs("warehouse.txt");
// reading from ifs ...
... "to outputfile 'updated.txt'"
std::ofstream ofs("updated.txt");
// writing to ofs ...
}
... "Then I want to use 'updated.txt' as an input file" ~> create another instance of ifstream:
{
std::ifstream ifs2("updated.txt");
// reading from ifs2 ...
}
Yes, and it's possible to to use just two file streams if you use std::fstream. For example:
#include <fstream>
#include <iostream>
#include <string>
int main(void)
{
std::ifstream infile("a.txt");
// create file for both reading and writing
std::fstream ffile("b.txt", std::fstream::in | std::fstream::out | std::fstream::trunc);
// read contents of file a and write to file b
std::string line;
while (std::getline(infile, line))
{
std::cout << line << std::endl;
ffile << line << std::endl;
}
// flush the output to disk
ffile.flush();
// go back to the start of the output file before reading from it
ffile.seekg(0);
// read contents of output file again.
while (std::getline(ffile, line))
{
std::cout << line << std::endl;
}
return 0;
}