My ifstream is not reading integers correctly. My main is as follows:
#include <fstream>
#include <vector>
#include <string>
int main() {
vector<int> myvec;
Tree myAVL;
ifstream myfile;
string filename;
cout << "Enter the file name you would like to use: ";
cin >> filename;
myfile.open(filename);
while (!myfile) {
cout << "ERROR: INVALID FILENAME -- Please input valid filename: ";
cin >> filename;
myfile.open(filename);
}
while (!myfile.eof()) {
int x;
myfile >> x;
if (myvec.size() == 0) {
myvec.push_back(x);
myAVL.insert(x);
continue;
}
if (checkVecDuplicate(myvec, x))
{
myAVL.insert(x);
myvec.push_back(x);
}
}
for (int i = 0; i < myvec.size(); i++)
{
cout << myvec[i];
}
cout << endl << endl;
Node* temp = myAVL.head;
myAVL.inorder(temp);
cout << endl << endl;
myAVL.preorder(temp);
cout << endl << endl;
system("pause");
return 0;
}
My text file I'm trying to read in is:
14
32
64
55
1
12
3
4
16
72
125
54
Why is this not reading any of the integers? At myfile >> x it's reading in a null value for the integer and I have no clue why. Thank you!
Related
I've been trying to delete an element from my vector using fstream for a while. I made a post before about this and I applied the advice from you guys but still I'm not getting my program to delete the element of my vector.
I can creat and re write my .txt file into another one but I can't make my code to delete the element. I think I've been having problems with ios::truc or ios::out. Any suggestion ? feel free to try my code and check it out.
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include <stdlib.h>
using namespace std;
void Menu()
{
cout << "******************" << endl;
cout << "1 - Add student" << endl;
cout << "2 - Delete student" << endl;
cout << "3 - All students" << endl;
cout << "******************" << endl;
}
struct School
{
string Name;
string remove;
int Ages;
int Option;
};
int main()
{
School Students;
vector <string> Names;
ofstream Myfile("practiceFile.txt", ios::app);
if(!Myfile.is_open())
{
cout << "Error opening file" << endl;
}
if(Myfile.is_open())
{
Menu();
cout << endl;
cout << "Option: ";
cin >> Students.Option;
cin.ignore();
if(Students.Option == 1)
{
cout << "Enter name: ";
getline(cin, Students.Name);
Names.push_back(Students.Name);
for(int i = 0; i < Names.size(); i++)
{
Myfile << Names[i] << endl;
}
}
if(Students.Option == 2)
{
cout << "Enter name: ";
getline(cin, Students.remove);
for(int i = 0; i < Names.size(); i++)
{
if(Students.remove == Names[i])
{
auto itr = find(Names.begin(), Names.end(), Students.remove);
if(itr != Names.end())
{
Names.erase(itr);
Myfile << Names[i] << endl;
}
}
}
}
}
ofstream TempFile("practiceFile_temp.txt", ios::trunc|ios::out);
if(TempFile.is_open())
{
for(int i = 0; i < Names.size(); i++)
{
TempFile << Names[i] << endl;
}
}
TempFile.close();
Myfile.close();
remove("practiceFile.txt");
rename("practiceFile_temp.txt", "practiceFile.txt");
string line;
ifstream NewFile("practiceFile.txt", ios::in);
while(NewFile >> line)
{
if(Students.Option == 3)
{
cout << line << endl;
}
}
}
Actually, if you compile my code you won't be able to save strings into the file. But when I delete this part of my code (here below) At leats I'm able to save strings into the file but not delete them. As I said before, I think I'm not using ios::trunc|ios::out correct. Thanks!
ofstream TempFile("practiceFile_temp.txt", ios::trunc|ios::out);
if(TempFile.is_open())
{
for(int i = 0; i < Names.size(); i++)
{
TempFile << Names[i] << endl;
}
}
TempFile.close();
Myfile.close();
remove("practiceFile.txt");
rename("practiceFile_temp.txt", "practiceFile.txt");
UPDATE:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include <stdlib.h>
using namespace std;
void Menu()
{
cout << "******************" << endl;
cout << "1 - Add student" << endl;
cout << "2 - Delete student" << endl;
cout << "******************" << endl;
}
struct School
{
string Name;
string remove;
int Ages;
int Option;
};
int main()
{
School Students;
vector <string> Names;
ifstream NewFile("practiceFile.txt");
string line;
while(getline(NewFile, line))
{
cout << line << endl;
}
ofstream Myfile("practiceFile.txt", ios::app);
if(!Myfile.is_open())
{
cout << "Error opening file" << endl;
}
if(Myfile.is_open())
{
Menu();
cout << endl;
cout << "Option: ";
cin >> Students.Option;
cin.ignore();
if(Students.Option == 1)
{
cout << "Enter name: ";
getline(cin, Students.Name);
Names.push_back(Students.Name);
}
if(Students.Option == 2)
{
cout << "Enter name: ";
getline(cin, Students.remove);
for(int i = 0; i < Names.size(); i++)
{
if(Students.remove == Names[i])
{
Names.erase(Names.begin() + i);
}
}
}
for(int i = 0; i < Names.size(); i++)
{
Myfile << Names[i] << endl;
}
}
ofstream TempFile("practiceFile_temp.txt", ios::trunc);
if(TempFile.is_open())
{
for(int i = 0; i < Names.size(); i++)
{
TempFile << Names[i] << endl;
}
}
TempFile.close();
Myfile.close();
remove("practiceFile.txt");
rename("practiceFile_temp.txt", "practiceFile.txt");
}
I'm practicing ifstream usage. I want the user to enter the file they want to read, in this example num1.txt specifically. I want the console to read one letter from num1.txt and output it on its own line.
I've ran the code below, and after entering "num1.txt" into the console, I get nothing back. I've tried moving around cout << num << endl; to the inner do statement, but it ends up repeating the number 10 an infinite amount.
What am I doing wrong here?
Contents in num1.txt:
2 4 6 8 10
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string fileName, cont;
ifstream inputFile;
do {
int num = 0;
int total = 0;
cout << "Please enter the file name: ";
cin >> fileName;
inputFile.open(fileName);
if (inputFile.is_open()) {
do {
inputFile >> num;
total += num;
}
while(num > 0);
if (total != 0) {
cout << num << endl;
cout << "Total is: " << total << endl;
}
}
else {
cout << "Failed to open file." << endl;
}
inputFile.close();
cout << "Do you want to continue processing files? (yes or no): " << endl;
cin >> cont;
}
while (cont == "yes");
}
Your inner do loop is not correctly validating that operator>> is actually successful before using num. It should be looking at the stream's error state after each read. The easiest way to do that is to change your do loop into a while loop that uses the result of the read as its loop condition, eg:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string fileName, cont;
ifstream inputFile;
do {
cout << "Please enter the file name: ";
cin >> fileName;
inputFile.open(fileName);
if (inputFile.is_open()) {
int num = 0;
int total = 0;
while (inputFile >> num) {
total += num;
}
inputFile.close();
cout << "Total is: " << total << endl;
}
else {
cout << "Failed to open file." << endl;
}
cout << "Do you want to continue processing files? (yes or no): " << endl;
}
while ((cin >> cont) && (cont == "yes"));
return 0;
}
I have a question in this code. What does fucntion receive when we use cin as a argument.
For example cin stops when he finds a white space or '\n', so if the input is "1 2 3 4 5" the program will return 5 but I can't understand why.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
double max_value(istream& in) // can be called with 'infile' or 'cin'
{
double highest;
double next;
if (in >> next)
highest = next;
else
return 0;
while (in >> next)
{
if (next > highest)
highest = next;
}
return highest;
}
int main()
{
double max;
string input; cout << "Do you want to read from a file? (y/n) ";
cin >> input;
if (input == "y")
{
string filename;
cout << "Please enter the data file name: ";
cin >> filename;
ifstream infile; infile.open(filename);
if (infile.fail())
{
cerr << "Error opening " << filename << "\n";
return 1;
}
max = max_value(infile);
infile.close();
}
else
{
cout << "Insert the numbers. End with CTRL-Z." << endl; max = max_value(cin);
}
cout << "The maximum value is " << max << "\n";
return 0;
}
I'm trying to write a program to read from a file and display the text backwards. - My loop backward loop is not working. Any suggestions?
- Also if I'm reading a file that only contain floats integers or floats, how would I display them all as float?
Thanks,
#include <iostream>
#include <fstream>
using namespace std;
void seeReverseText(char fileName[])
{
ifstream fin(fileName);
if (fin.fail())
{
cout << "Error opening file " << fileName << endl;
return;
}
cout.setf(ios::showpoint);
cout.precision(2);
cout.setf(ios::fixed);
int i = 0;
cout << "New order:\n";
while (!fin.eof())
{
// this is what I was trying to do
// i++;
// for (i--; i >= 0; i--)
// fin >> fileName[i];
// cout << "' " << fileName[i] << "'";
fin >> fileName;
cout << fileName << endl;
}
fin.close();
}
int main()
{
char fileName[256];
cout << "Enter the filename: ";
cin >> fileName;
seeReverseText(fileName);
return 0;
}
Try something more like this instead:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
void seeReverseText(const std::string &fileName)
{
std::ifstream fin(fileName);
if (!fin)
{
std::cout << "Error opening file " << fileName << std::endl;
return;
}
std::cout.setf(std::ios::showpoint);
std::cout.precision(2);
std::cout.setf(std::ios::fixed);
std::cout << "New order:\n";
std::string line;
while (std::getline(fin, line))
{
std::reverse(line.begin(), line.end());
std::cout << line << std::endl;
}
}
int main()
{
std::string fileName;
std::cout << "Enter the filename: ";
if (std::cin >> fileName)
seeReverseText(fileName);
return 0;
}
For example i have a notepad for numbers.
1 2 3 4 5
Then i want to add 4 in the third line which is 3 so that its new value will be
1 2 7 4 5
Question is how can i do that?
Please help me thank you!
string add;
cout<<"Enter value to be added: ";
cin>>add;
fstream file;
file.open("quantity.txt");
You have not been accurate enough with your question, but I think this is something like what you expect:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main(int argc, const char *argv[]) {
std::string filename;
std::cout << "Enter name of file to modify: ";
std::cin >> filename;
std::ifstream inputFile;
inputFile.open(filename);
if(inputFile.fail()) {
std::cout << "Unable to open file \"" << filename << "\" for reading\n";
return 1;
}
unsigned offset;
std::cout << "Enter offset to modify: ";
std::cin >> offset;
int toAdd;
std::cout << "Enter value to be added to line #" << lineNumber << ": ";
std::cin >> toAdd;
std::vector<int> nums;
for(;;) {
int num;
inputFile >> num;
if(inputFile.eof())
break;
nums.push_back(num);
}
inputFile.close();
if(offset >= nums.length()) {
std::cout << "Offset " << offset << " out of bounds!\n";
return 1;
}
nums[offset] += toAdd;
std::ofstream outputFile;
outputFile.open(filename);
if(outputFile.fail()) {
std::cout << "Unable to open file \"" << filename << "\" for writing\n";
return 1;
}
for(int num : nums) // Warning: C++11
outputFile << num << ' ';
outputFile.close();
}