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;
}
Related
if I have a file like this, say called file.txt
20
25 97
97 5
How would i properly read it given that the second column has missing the second element in the first row?I attempted this
int main()
{
ifstream readFile;
string filename;
cout << "Please enter file name and extention: " << endl;
cin >> filename;
readFile.open(filename);
int row = 0;
int column = 0;
while (readFile >> row >> column)
{
//does something
}
However I get segmenation fault
I would like using std::getline to read lines and std::stringstream to parse the lines.
#include <iostream>
#include <fstream>
#include <sstream>
int main(void) {
std::string filename;
std::string line;
std::ifstream readFile;
std::cout << "Please enter file name and extention: " << std::endl;
std::cin >> filename;
readFile.open(filename);
if (!readFile) {
std::cout << "open error\n";
return 1;
}
while (std::getline(readFile, line)) {
std::stringstream ss(line);
int row = 0, column = 0;
if (ss >> row >> column) {
std::cout << "row = " << row << ", column = " << column << '\n';
} else {
std::cout << "invalid line\n";
}
}
readFile.close();
return 0;
}
Another version (also print partial line):
#include <iostream>
#include <fstream>
#include <sstream>
int main(void) {
std::string filename;
std::string line;
std::ifstream readFile;
std::cout << "Please enter file name and extention: " << std::endl;
std::cin >> filename;
readFile.open(filename);
if (!readFile) {
std::cout << "open error\n";
return 1;
}
while (std::getline(readFile, line)) {
std::stringstream ss(line);
int row = 0, column = 0;
bool row_valid = false, column_valid = false;
if (ss >> row) row_valid = true;
if (ss >> column) column_valid = true;
std::cout << "row = ";
if (row_valid) std::cout << row; else std::cout << "(none)";
std::cout << ", column = ";
if (column_valid) std::cout << column; else std::cout << "(none)";
std::cout << '\n';
}
readFile.close();
return 0;
}
Yet another version (support lines with 3 integers and more):
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
int main(void) {
std::string filename;
std::string line;
std::ifstream readFile;
std::cout << "Please enter file name and extention: " << std::endl;
std::cin >> filename;
readFile.open(filename);
if (!readFile) {
std::cout << "open error\n";
return 1;
}
while (std::getline(readFile, line)) {
std::stringstream ss(line);
std::vector<int> lineInts;
int data;
while (ss >> data) lineInts.push_back(data);
std::cout << lineInts.size() << " elements:";
for (int d : lineInts) std::cout << ' ' << d;
std::cout << '\n';
}
readFile.close();
return 0;
}
>
Code:
int main ()
{
ifstream inStream;
ofstream outStream;
getInputOutputStreams(inStream, outStream);
numberFile(inStream, outStream);
return EXIT_SUCCESS;
}
I have been stuck with an issue where the code I wrote compiles, but when running it has to be terminated because it just lags. I believe my issue may have to deal with not correctly returning the names of the input and output files, but I cannot figure out where I'm going wrong. I'm only a beginner in C++ (we are just now learning about arrays), so if you think this is a dumb question I'm sorry!
Here is the code I have written:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
string getInputOutputStreams(ifstream &inStream, ofstream &outStream);
void numberFile(ifstream &inStream, ofstream &outStream);
int main ()
{
ifstream inStream;
ofstream outStream;
getInputOutputStreams(inStream, outStream);
numberFile(inStream, outStream);
return EXIT_SUCCESS;
}
string getInputOutputStreams(ifstream &inStream, ofstream &outStream)
{
string inputFile;
string outputFile;
cout << "Enter the name of the input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
while (inStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
}
inStream.close();
cout << "Enter the name of the output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
while (outStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
}
outStream.close();
return inputFile;
return outputFile;
}
void numberFile(ifstream &inStream, ofstream &outStream)
{
string inputFile;
string outputFile;
inStream.open(inputFile);
outStream.open(outputFile);
int lineNumber = 0;
string line;
while(!inStream.eof()) {
if(line == " ") {
}
else {
lineNumber++;
outStream << lineNumber << ": " << line << endl;
}
getline(inStream, line);
}
cout << lineNumber << " lines processed" << endl;
inStream.close();
outStream.close();
}
I see a number of mistakes:
getInputOutputStreams() is closing the streams that it opens.
incorrect return statements in getInputOutputStreams().
numberFile() is re-opening the streams, using filename strings that have no values.
using while(!inStream.eof()) is bad.
not ignoring blank lines, as the instructions ask for.
Try this instead
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void getInputOutputStreams(ifstream &inStream, ofstream &outStream);
void numberFile(ifstream &inStream, ofstream &outStream);
int main ()
{
ifstream inStream;
ofstream outStream;
getInputOutputStreams(inStream, outStream);
numberFile(inStream, outStream);
return EXIT_SUCCESS;
}
void getInputOutputStreams(ifstream &inStream, ofstream &outStream)
{
string inputFile;
string outputFile;
cout << "Enter the name of the input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
while (inStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
}
cout << "Enter the name of the output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
while (outStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
}
}
void numberFile(ifstream &inStream, ofstream &outStream)
{
int lineNumber = 0;
string line;
while (getline(inStream, line)) {
if (line != "") {
++lineNumber;
outStream << lineNumber << ": " << line << endl;
}
}
cout << lineNumber << " lines processed" << endl;
}
#include <iostream>
#include <string>
#include <fstream>
#include "Encrypt.h"
int main() {
std::string Choose;
std::cout << "Please enter write or read(Lower Case Only): ";
std::getline(std::cin, Choose);
if (Choose == "write") {
std::string Sentence;
std::string SecurityKey;
std::string TextFile;
std::cout << "Enter Your sentence that you wish to be encrupted" << std::endl << "Sentence: ";
std::getline(std::cin, Sentence);
std::cout << std::endl << "Secuirty Key Disclaimer Never forget what secruity \nkey you used otherwise your message will be lost forever" << std::endl;
std::cout << "Secuirty Key: ";
std::getline(std::cin, SecurityKey);
std::string message = encrypt(Sentence, SecurityKey);
std::cout << "Encrypted: " << std::endl << message;
std::cout << "\nDecrypted: " << decrypt(message, SecurityKey) << std::endl;
std::cout << "Enter a title for text Document: ";
std::getline(std::cin, TextFile);
TextFile = TextFile + ".txt";
std::ofstream out(TextFile);
out << message;
out.close();
}
else if (Choose == "read") {
std::string NameOfFile;
std::getline(std::cin, NameOfFile);
NameOfFile = NameOfFile + ".txt";
std::string STRING;
std::ifstream infile;
infile.open(NameOfFile);
while (!infile.eof)
{
getline(infile, STRING);
}
infile.close();
std::string S_Key;
std::getline(std::cin, S_Key);
std::cout << "\nDecrypted: " << decrypt(STRING, S_Key) << std::endl;
}
else {
std::cout << "There are only 2 Options.... (lower Case Only)" << std::endl;
}
system("PAUSE");
}
My problem is its not reading the file how it is Encrypting it could this be because of the .eof() reading the file differently to how the program is Encrypting it, .. Its not reading the file Correctly how do i work around this?
If anyone is Interested here is the Encrypt.h
std::string encrypt(std::string msg, std::string key) {
std::string tmp(key);
while (key.size() < msg.size())
key += tmp;
for (std::string::size_type i = 0; i < msg.size(); ++i)
msg[i] ^= key[i];
return msg;
}
std::string decrypt(std::string msg, std::string key) {
return encrypt(msg, key);
}
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();
}
I'm working on a simple c++ script and wanted to put the whole process of opening up a file inside a function. However, when I try, I get errors in my main function. Can anyone help me? This is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
string openFile(string fileName);
int main(void)
{
string fileName;
cout << "Please input the file name (including the extension) for this code to read from." << endl;
cin >> fileName;
openFile(fileName);
fout << "File has been opened" << endl;
return 0;
}
string openFile(string fileName)
{
ifstream fin(fileName);
if (fin.good())
{
ofstream fout("Output");
cout << fixed << setprecision(1);
fout << fixed << setprecision(1);
//Set the output to console and file to be to two decimal places and
//not in scientific notation
}
else
{
exit(0);
}
}
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
ofstream fout;
string openFile(string fileName);
void closeFile();
int main(void)
{
string fileName;
cout << "Please input the file name (including the extension) for this code to read from." << endl;
cin >> fileName;
openFile(fileName);
if (fout.good()) //use fout in any way in this file by cheking .good()
cout << "File has been opened" << endl;
closeFile();
return 0;
}
string openFile(string fileName)
{
cout << fixed << setprecision(1);
fout.open(fileName.c_str());
if (fout.good()) {
fout << fixed << setprecision(1);
cout<<"Output file opened";
}
}
void closeFile()
{
fout.close();
}
Your code has lot many flaws,
fout << "File has been opened" << endl;, should be,
cout << "File has been opened" << endl;
You cannot redifine same varible again.
ofstream fout("Output");// first
cout << fixed << setprecision(1);
fout << fixed << setprecision(1);
//Set the output to console and file to be to two decimal places and
//not in scientific notation
ofstream fout("Tax Output.txt");//second
Give some other name to variable in last line.
You are passing std::string, where you should pass const char *,
ifstream fin(fileName);
should be,
ifstream fin(fileName.c_str());