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"
Related
#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.
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;
}
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;
So I've been playing with code and I'm stuck with finding how to replace the line found. I'm able to find the name and add the 'new_gpa' to the section but it outputs the final result in the same file but without replacing the original score and name.
how could I remove the original line found along with the gpa? and also store the new values to the file.
cristian 2.1
rachel 3.0
name search: cristian
new file:
cristian 2.1
rachel 3.0
cristian 4.1
The code is below.
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstring>
using namespace std;
int main()
{
string name;
int offset;
string line;
ifstream read_file;
read_file.open("alpha.dat", std::ios_base::app);
cout << "Please enter your name: \n";
cin>> name;
if (read_file.is_open())
{
while(!read_file.eof())
{
getline(read_file,line);
if((offset = line.find(name)) != string::npos)
{
cout <<"the word has been found: \n";
//cout << line //example to display
//new code
istringstream iss ( line );
string thisname;
double gpa;
double new_gpa = 2.1;
if( iss >> thisname >> gpa)
{
if (thisname == name)
{
cout << name <<endl;
cout << gpa <<endl;
ofstream myfile;
myfile.open("alpha.dat",std::ios_base::app);
myfile << " \n" << name << " " << gpa+ new_gpa;
myfile.close();
read_file.close();
}
}
}
}
}
return 0;
}
You open file with std::ios_base::app, which means all you output operations are performed at the end of file, appending the content to the current file. But what you want to do is modify the data at the original place. So you should open file with std::ios_base::in, and function seekp can help you in the next step.
so i have a code that's supposed to find a string of characters in a certain .txt file, if the input is in the file, it says "yey i found it" but when it isnt, its supposed to say "didnt find anything", but it just skips that step and ends.
I'm a beginner so sorry for any obvious mistakes.
#include <stdio.h>
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void)
{
setlocale(LC_ALL, "");
string hledat;
int offset;
string line;
ifstream Myfile;
cout.flush();
cout << "Welcome, insert the string to find in the file. \n \n \n" << endl;
cin.get();
cout.flush();
Myfile.open("db.txt");
cin >> hledat;
if (Myfile.is_open())
{
while (!Myfile.eof())
{
getline(Myfile, line);
if ((offset = line.find(hledat, 0)) != string::npos)
{
cout.flush();
cout << "Found it ! your input was : " << hledat << endl;
}
}
Myfile.close();
}
else
{
cout.flush();
cout << "Sorry, couldnt find anything. Your input was " << hledat << endl;
}
getchar();
system("PAUSE");
return 0;
}
There are three possible cases.
The file was not successfully opened.
The file was successfully opened, but the string was not found.
The file was successfully opened, and the string was found.
You have a printout for cases 1 and 3, but not 2.
By the way, your loop condition is wrong. Use the result of the call to getline, which is the ostream object itself after the read attempt.
while (getline(MyFile, line))
{
...
}
The loop will terminate upon an unsuccessful read attempt, which will happen after you read the last line. The way you have it, you will try to read after the last line, which will be unsuccessful, but you will still try to process that non-existent line because you don't check eof until the loop starts over.
Just comment out //cin.get(); , you dont need it.
Output:
Welcome, insert the string to find in the file.
apple
Found it ! your input was : apple
Other than that, it works like a charm.
Corrected code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void)
{
setlocale(LC_ALL, "");
string hledat;
int offset;
string line;
ifstream Myfile;
cout.flush();
cout << "Welcome, insert the string to find in the file. \n \n \n" << endl;
//cin.get(); <----- corrected code
cout.flush();
Myfile.open("db.txt");
cin >> hledat;
if (Myfile.is_open())
{
while (!Myfile.eof())
{
getline(Myfile, line);
if ((offset = line.find(hledat, 0)) != string::npos)
{
cout.flush();
cout << "Found it ! your input was : " << hledat << endl;
}
}
Myfile.close();
}
else
{
cout.flush();
cout << "Sorry, couldnt find anything. Your input was " << hledat << endl;
}
getchar();
system("PAUSE");
return 0;
}