I need to check if a word exists in a dictionary text file, I think I could use strcmp, but I don't actually know how to get a line of text from the document. Here's my current code I'm stuck on.
#include "includes.h"
#include <string>
#include <fstream>
using namespace std;
bool CheckWord(char* str)
{
ifstream file("dictionary.txt");
while (getline(file,s)) {
if (false /* missing code */) {
return true;
}
}
return false;
}
std::string::find does the job.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool CheckWord(char* filename, char* search)
{
int offset;
string line;
ifstream Myfile;
Myfile.open (filename);
if (Myfile.is_open())
{
while (!Myfile.eof())
{
getline(Myfile,line);
if ((offset = line.find(search, 0)) != string::npos)
{
cout << "found '" << search << "' in '" << line << "'" << endl;
Myfile.close();
return true;
}
else
{
cout << "Not found" << endl;
}
}
Myfile.close();
}
else
cout << "Unable to open this file." << endl;
return false;
}
int main ()
{
CheckWord("dictionary.txt", "need");
return 0;
}
char aWord[50];
while (file.good()) {
file>>aWord;
if (file.good() && strcmp(aWord, wordToFind) == 0) {
//found word
}
}
You need to read words with the input operator.
Related
Why can't I get each line in a file printed with this? It outputs nothing.
string line;
ifstream s {"book_list.txt"};
while (getline(s, line)) {
cout << line << endl;
}
I've included fstream, sstream, string, stdio.h, stdlib.h and am using namespace std;
#include<iostream>
#include<fstream>
#include<string>
using namespace std
int main()
{
string line;
ifstream s ("book_list.txt");
if (s.is_open())
{
while ( getline (s,line) )
{
cout << line << endl;
}
s.close(); // Don't forget to close the file
}
else
cout << "Unable to open file";
return 0;
}
I hop it helps.
I guess the file doesnt exist.
Please check with:
if(!s.good()) {
// error
}
or
while (s.good()) {
// process.
}
You should do something like this:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string sLine = "";
ifstream infile;
infile.open("temp.txt");
if(infile.is_open()){
while (getline(infile, sLine))
{
cout << sLine << endl;
}
}
infile.close();
cout << "Read file completed!!" << endl;
}
#include <iostream>
#include <fsstream>
using namespace std;
int main()
{
string data;
int counter = 0;
ifstream infile("test_file.txt");
while (!infile.eof()){
getline(infile , data);
counter += 1;
}
cout << "The number of lines is : " << counter << endl;
return 0;
}
I'm trying to read in a random file (on mac-xcode) and determine the instances of the letter k in the document. Then print the number as an outout file. My problem is that the outfile isn't being written and the nums_k is coming back as 0. I'm not sure if the ifstream is working incorrectly or the ofstream need a different filename established. Here's my source code.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream infile("Users/bryanmichaelnorris/documents/extra credit assignment.docx");
string line;
int numks = 0;
while (getline(infile,line)) {
int x = 0;
for (std::string::iterator it=line.begin(); it!=line.end(); ++it) {
if (line[x] == 'k') {
numks++;
}
x++;
}
}
infile.close();
ofstream outfile("number of k's.docx");
outfile << "There are " << numks << " K's in the file." << endl;
outfile.close();
return 0;
}
Added validations for the opened files.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
const char * csInputFileNane="Users/bryanmichaelnorris/documents/extra credit assignment.docx";
ifstream infile(csInputFileNane);
if (!infile.is_open()) {
cerr << "Cannot open file \""<<csInputFileNane<<'"'<<endl;
return -1;
}
string line;
int numks = 0;
while (getline(infile,line))
{ int x = 0;
for (std::string::iterator it=line.begin(); it!=line.end(); ++it) {
if (line[x] == 'k')
{
numks++;
}
x++;
}
}
infile.close();
const char *csOutFileName="number of k's.docx";
ofstream outfile(csOutFileName);
if (!outfile.is_open()) {
cerr << "Cannot open file \""<<csOutFileName<<'"'<<endl;
return -1;
}
outfile << "There are " << numks << " K's in the file." << endl;
outfile.close();
return 0;
}
I'm having trouble understanding why my code isn't changing the results of the output file. I tried using the "find" member function to check for the incorrect operators, but it doesn't seem change the results. How would I check to see if a string contains the string I'm looking for, then replace it?
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void input(ifstream& inputfile, ofstream& outputfile, string& cpp);
void edit(ifstream& inputfile, ofstream& outputfile, string& line);
int main()
{
ifstream inputfile;
ofstream outputfile;
string line, cpp;
input(inputfile,outputfile,cpp);
edit(inputfile, outputfile, line);
}
void input(ifstream& inputfile, ofstream& outputfile, string& cpp)
{
cout << "Enter a cpp file to modify" << endl;
cin >> cpp;
cout << endl;
inputfile.open(cpp.c_str());
if (inputfile.fail())
{
cout << "File opening failed,did you enter your file name correctly?" << endl;
exit(1);
}
outputfile.open("cpp.txt");
}
void edit(ifstream& inputfile, ofstream& outputfile, string& line)
{
string a = "cin <<";
string b = "cout >>";
getline(inputfile,line);
while (! inputfile.eof())
{
line.find(a);
if (line == a)
{
outputfile << "cin >>";
}
line.find(b);
if (line == b)
{
outputfile << "cout >>";
}
else
outputfile << line << endl;
getline(inputfile,line);
}
}
I think you are intention is:
if (line.find(a) != string::npos)
{
outputfile << "cin >>";
}
if (line.find(b) != string::npos)
{
outputfile << "cout >>";
}
std::string::find() doesn't change the string is called with. It tries to find the string or character posted as an argument and returns the position of the character found. All you have to do is check if the return value it doesn't equal std::string::npos, which is a value which means the substring wasn't found:
if (line.find(a) != std::string::npos) {
...
}
else if (line.find(b) != std::string::npos) {
...
}
Also, while (!eof()) is generally seen as a bad programming practice. Perform the input within the loop parameters instead:
while (std::getline(inputfile, line)) {
if (line.find(a) != std::string::npos) {
outputfile << "cin >>";
}
else if (line.find(b) != std::string::npos) {
outputfile << "cout <<";
}
else
outputfile << line << std::endl;
}
find() searches the string for the specified substring and then returns its position if found, it does not modify the string being searched. You are ignoring the return value of find(). Try this instead:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void input(ifstream& inputfile, ofstream& outputfile, string& cpp);
void edit(ifstream& inputfile, ofstream& outputfile, string& line);
int main()
{
ifstream inputfile;
ofstream outputfile;
string line, cpp;
input(inputfile,outputfile,cpp);
edit(inputfile, outputfile, line);
}
void input(ifstream& inputfile, ofstream& outputfile, string& cpp)
{
cout << "Enter a cpp file to modify" << endl;
cin >> cpp;
cout << endl;
inputfile.open(cpp.c_str());
if (!inputfile)
{
cout << "Input file opening failed, did you enter your file name correctly?" << endl;
exit(1);
}
outputfile.open("cpp.txt");
if (!outputfile)
{
cout << "Output file opening failed" << endl;
exit(1);
}
}
void edit(ifstream& inputfile, ofstream& outputfile, string& line)
{
string a = "cin <<";
string b = "cout >>";
while (getline(inputfile,line))
{
if (line.find(a) != std::string::npos)
{
outputfile << "cin >>";
}
else if (line.find(b) != std::string::npos)
{
outputfile << "cout >>";
}
else
outputfile << line << endl;
}
}
I am wondering how I can do the following in C++ since I am only familiar with Java.
I have a string which we will call line.
string line = "hello how are you";
In C++ I have retrieved that line from getLine(). I want to traverse through this line so I can count the number of words in this line. The result should be 4, in my example.
In Java, I would have imported Scanner. Then do something like this:
//Other scanner called fileName over the file
while(fileName.hasNextLine()) {
line = fileName.nextLine();
Scanner sc = new Scanner(line);
int count=0;
while(sc.hasNext()){
sc.next();
count++;
}
}
I am only using #include<iostream>, fstream and string.
You can use stringstream
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string line;
getline(cin,line);
stringstream ss(line);
string word;
int count=0;
while(ss>>word){//ss is used more like cin
count++;
}
cout<<count<<endl;
return 0;
}
http://ideone.com/Yl25KT
I would avoid ifstream::getline and just use ifstream::get instead. You don't even need to use string.
#include <iostream>
int main()
{
int numwords = 1; //starts at 1 because there will be (numspaces - 1) words.
char character;
std::ifstream file("readfrom.txt");
if (file.fail())
{
std::cout << "Failed to open file!" << std::endl;
std::cin.get();
return 0;
}
while (!file.eof())
{
file >> character;
if (character == ' ')
{
numwords++;
std::cout << std::endl;
}
else if (character == '\n') //endline code
{
std::cout << "End of line" << std::endl;
break;
}
else
std::cout << character;
}
std::cout << "Line contained " << numwords << " words." << std::endl;
std::cin.get(); //pause
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
int main()
{
string temp;
ifstream inFile;
ofstream outFile;
inFile.open("ZRMK Matched - 010513.txt");
outFile.open("second.txt");
while(!inFile.eof()) {
getline(inFile, temp);
if (temp != "") {
getline(inFile, temp);
outFile << temp;
}
}
cout << "Data Transfer Finished" << endl;
return 0;
}
I'm having difficulty getting this to work. When I execute the program it cycles for awhile and then terminates without finishing -- it doesn't output any lines of text to the output file. Any help would be appreciated.
Are you trying to copy every line?
while(std::getline(inFile, temp)) {
outFile << temp << "\n";
}
Are you trying to copy every non-blank line?
while(std::getline(inFile, temp)) {
if(temp != "")
outFile << temp << "\n";
}
Are you trying to copy every 2nd non-blank line?
int count = 0;
while(std::getline(inFile, temp)) {
if(temp == "")
continue;
count++;
if(count % 2)
outFile << temp << "\n";
}
Are you simply trying to copy the entire file?
outFile << inFile.rdbuf();
You should use a mode to open files : see std::ios_base::openmode
And don't forget to close the streams you open !
You can even try catch your code to understand the problem if an exception occurred.
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
try {
fstream inFile;
fstream outFile;
// open to read
inFile.open("ZRMK Matched - 010513.txt", ios_base::in);
if (!inFile.is_open()) {
cerr << "inFile is not open ! " << endl;
return EXIT_FAILURE;
}
// Open to append
outFile.open("second.txt", ios_base::app);
if (!inFile.is_open()) {
cerr << "inFile is not open ! " << endl;
return EXIT_FAILURE;
}
string line;
while(getline(inFile, line)) {
if (!line.empty()) {
outFile << line << endl;
}
}
if (outFile.is_open()) {
outFile.close(); // Close the stream if it's open
}
if (inFile.is_open()) {
inFile.close(); // Close the stream if open
}
cout << "Data Transfer Finished" << endl;
return EXIT_SUCCESS;
} catch (const exception& e) {
cerr << "Exception occurred : " << e.what() << endl;
}
return EXIT_FAILURE;
}