Read and replacing text from a text file, C++ - c++

I don't want to change the text inside the file, just the output.
The text in the file reads "C++ is difficult and programming is difficult"
What I want the program to do is to read that, but replace the word "difficult" with the word "easy", so that it reads as "C++ is easy and programming is easy" actually touching or replacing anything in the text file.
This is what I have so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
ifstream myfile("difficult.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}

This would be as simple as:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string line;
ifstream myfile("difficult.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
if(line == "difficult") cout << "easy" << '\n';
else cout << line << '\n';
// OR
if(line == "difficult") line = "easy";
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}

Related

Is there a way to print individual words from a .txt file without leaving out lines?

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string readFileToString(string fileName) {
fstream file;
string word;
string returnMe;
returnMe.resize(200);
file.open(fileName.c_str());
while (file >> word) {
returnMe += word + " ";
}
file.close();
return returnMe;
}
int main() {
string fileName = "example.txt";
cout << readFileToString(fileName);
}
I have this code but I have several lines in my txt file and it completely ignores them.
If you want to print out all the words in the text file then you can use the following program:
#include <iostream>
#include <sstream>
#include <fstream>
int main()
{
std::ifstream inputFile("input.txt");
std::string word, line;
if(inputFile)
{
while(std::getline(inputFile, line)) //go line by line
{
//std::cout<<line<<std::endl; //this prints the line
std::istringstream ss(line);
while(ss >> word) //go word by word
{
std::cout << word << std::endl;
}
}
}
else
{
std::cout << "File cannot be opened" << std::endl;
}
return 0;
}
The output of the above program can be seen here.

C++ Clearing content of text file between runs causes only last line to be written

I am trying to write a few lines into a text file. I would like to empty the file before appending to it, on each run. I am able to clear the previous content, but when I do so, for some reason only the last line of my input file is appended to the output file. I also tried using remove() to erase the file and received the same output.
On the other hand without clearing the file or removing it, everything is appended properly to the output file.
I would be happy to find a way to solve this and perhaps understand why this occurs. I am using C++11.
I looked here: How to clear a file in append mode in C++
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdio.h>
int main() {
std::fstream infile;
std::string line;
infile.open("file.txt" , std::ios::in);
while (std::getline(infile, line)) {
std::istringstream line_buffer(line);
std::string word;
std::fstream outfile;
outfile.open("out.txt", std::ios::out);
outfile.close();
outfile.open("out.txt", std::ios::app);
while (line_buffer >> word) {
std::cout << word << " ";
outfile << word << " ";
}
std::cout << std::endl;
outfile << std::endl;
}
return 0;
}
The problem is that you are clearing the file at each iteration of the while loop, you can just open the outfile before the loop like this:
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdio.h>
int main() {
std::fstream infile;
std::string line;
infile.open("file.txt" , std::ios::in);
std::fstream outfile;
outfile.open("out.txt", std::ios::out);
while (std::getline(infile, line)) {
std::istringstream line_buffer(line);
std::string word;
while (line_buffer >> word) {
std::cout << word << " ";
outfile << word << " ";
}
std::cout << std::endl;
outfile << std::endl;
}
outfile.close();
return 0;
}

How to open any input file whether it contains words or numbers and prints it out. C++

So Lets say this is what the input file contains
12
Hello
45
54
100
Cheese
23
How would I print it out on the screen in that order.
This is what I had but it skips some lines.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int number;
string word;
int loop = 0;
ifstream infile;
infile.open("arraynumbers.txt");
while(infile >> number >> word)
{
if( infile >> number)
{
cout << number << endl;
}
if(infile >> word)
{
cout << word << endl;
}
}
return 0;
}
I suggest using www.cplusplus.com to answer these questions.
However, you are on the right track. Since you are just outputting the contents of the file to stdout, I suggest using readline() and a string. If you need to access the numeric strings as ints, use the atoi() function.
Example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream file("arraynumber.txt");
if (file.is_open()) {
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else cout << "Error opening arraynumber.txt: File not found in current directory\n";
return 0;

c++ file search database file and cout next line

I have 2 codes here, the first one here prompts you for a number, then tells you what is on that line number in the text file "example.txt"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string s;
vector <string> v;
ifstream fileInput;
int qwe = 0;
fileInput.open("example.txt");
while (getline( fileInput, s ))
{
v.push_back( s );
}
cout << "number: " << endl;
cin >> qwe;
cout << "line " << qwe << ": " << v[ qwe ] << endl;
fileInput.close();
}
and a second code here prompts the user for input then adds a "?" at the beginning because it's for my algorithm in the future, it will be used then. But then it searches for that in the text file and gives the user the line number of what the user inputted
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
using namespace std;
int main()
{
ifstream fileInput;
int offset;
string line;
string search;
cout << "Hi" << endl;
getline(cin, search);
search = "?" + search;
// open file to search
fileInput.open("example.txt");
if(fileInput.is_open())
{
while(getline(fileInput, line))
{
for(unsigned int curLine = 2; getline(fileInput, line); curLine++)
{
if (line.find(search) != string::npos)
{
cout << "found: " << search << " line: " << curLine << endl;
}
}
}
fileInput.close();
}
else cout << "Unable to open file.";
}
So my problem is that I need to sort of combine these codes, I need it so that it prompts the user for input and then it figures out the line number, and then it couts the next line, how do I do this?
Like this would do:
#include <fstream>
#include <algorithm>
#include <iostream>
int main()
{
std::string input;
std::cout<<"Enter a line to search for: \n";
std::getline(std::cin, input);
std::fstream File("example.txt", std::ios::in);
if (File.is_open())
{
std::string line;
int line_count = 0;
while(std::getline(File, line))
{
if (line.find(input) != std::string::npos)
{
std::cout<<"The line found was: \""<<line<<"\" at line: "<<line_count<<"\n";
if (std::getline(File, line))
{
std::cout<<"The line after that is: \""<<line<<"\"\n";
++line_count;
}
else
{
std::cout<<"There are no lines after that!\n";
}
}
++line_count;
}
File.close();
}
}
With an example file of:
hello world
I am testing
finding lines
you can search for "hello" and it will return line 0 aka the first line..
However, if you turn on find_approximate_line and searched for "hey world", it will still return line 0 because of the HammingDistance algorithm.
If you don't care about partial/close matches then you can remove the HammingDistance algorithm and keep using the std::string.find.
One example output is:
Enter a line to search for:
> hello world
The line found was: "hello world" at line: 0
The line after that is: "I am testing"

How do I open a .txt file in C++?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile("hey.txt");
myfile >> line;
cout << line;
system("pause");
return 0;
}
Why does this not print out what is in my "hey.txt" file?
This should do the job, If you are new to these things please read http://www.cplusplus.com/doc/tutorial/files/
EDIT: in article above .good() is a bad practice, look here if you need to more detail Testing stream.good() or !stream.eof() reads last line twice
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while(getline(myfile, line)) {
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}