Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
I want to read all files in specific directory.
for example
In directory a there are file1.text file2.txt a.txt c.txt
I want to know how many words are included in each file.
I made a code for a file.
But I don't know how to automatically move on next file at same directory.
int EBook::get_total_words()
{
ifstream ifs("inputs//a.txt");
int words = 0;
string word;
if (!ifs)
{
std::cout << "Unable to open file " << '\n';
exit(1);
}
while (ifs >> word) {
++words;
}
return words;
}
With std::filesystem you can do this:
std::string path_to_dir = '/some/path/';
for( const auto & entry : std::filesystem::directory_iterator( path_to_dir ) ){
std::cout << entry.path( ) << std::endl;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 days ago.
Improve this question
int main()
{
string array[4000];
short loop = 0;
string line;
ifstream myfile("out0.txt");
if (myfile.is_open())
{
while (!myfile.eof())////
{
getline(myfile, line);
array[loop] = line;
cout << array[loop] << endl;
loop++;
}
myfile.close();
}
else cout << "can't open the file";
}
I have a txt file which contains data as:
12.4
45.2
65.2
44.3
...
...
...
I want to read this column in an array. Above is the code but its not giving the desired output kindly help
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have been asked to develop program to count no. of lines and words in the file, this is my trial, my teacher said that I cant use >> operator for counting words and comparing but I could not handle it.
#include <iostream>
#include <fstream>
using namespace std;
int numberLines = 0;
int numberWords = 0;
void numberOfLines(){
cout<<"number of lines is " << numberLines << endl;
}
void numberWords(){
cout << "number of words is " << numberWords <<endl;
}
int main(){
string line;
char a = '';
ifstream myfile("files.txt");
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line);
cout<< line << endl;
numberLines++;
}
if ( a == ' '){
NumberWords++;
}
}
myfile.close();
}
numberOfLines();
numberOfWords ();
}
What you can do is add a 3rd argument to getline(). This lets it pull data from the stream until it hits a character. Doing getline(cin, line, ' ')takes all the data until the next and puts it into line. Your code might look like:
while(getline(inFile, line))
{
++numlines;
stringstream lineStream(line);
while(getline(lineStream, line, ' '))
{
++numWords;
}
}
The outer loop goes through the file and stores each line into line, then the inner goes through that line and counts each space. which correlates to a word.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm want to write a program that will read from a file line by line. For each line read, I will perform a specific function and move to the next line.
What will be a general syntax for this operation in c++
int main(){
string line;
string instruction;
string input;
ifstream file("file.txt");
if (file.is_open())
{
while (getline(file, line))
{
// perform a function with info from first line
//den move to second line
}
file.close();
}
else cout << "Unable to open file";
return 0;
}
You're quite there, what you are missing is just the function that would process the line.
void processLine(const string& line) {
cout << line << '\n';
}
Then just as you already do:
while (getline(file, line))
{
processLine(line);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How do you make a c++ program search for a string?
I'm new to the programming language c++.
Is there a way so that my program will look for a string of a .txt file and do something if the program found it?
First open an ifstream to open your file then check for the string:
#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) )
{
if(line.find("the string to find") != string::npos)
{
//line found, do something
}
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Say I have a textfile "Employees.txt" with Employee name and ID.
Like so:
John:d4250
Sarah:s5355
Alan:r4350
If I have a very very basic Employee class which has a constructor for Name and ID
and I wish to read from this textfile and insert them into a vector
Am I best to use something like:
void GenericProgram::loadEmployees()
{
string line;
ifstream empFile("employees.txt");
if(empFile.fail())
{
cout << "input file opening failed\n" << endl;
exit(EXIT_FAILURE);
}
while (!empFile.eof())
{
string empName;
string empID;
while (getline(empFile, line, '\n'))
{
// this will give me the line on its own
// now how to delimit again using ':'
// then do something like
Employee e(empName, empID)
employeeVector.push_back(e);
}
empFile.close();
}
}
I apologise that this is so basic. Brain is failing on me. I was wondering if there are better ways to read from files to populate objects with streams.
Just add to your code
int pos = line.find(":", 0);
if (pos != std::string::npos)
{
std::string empName(line.begin(), line.begin() + pos);
std::string empID(line.begin() + pos + 2, line.end());
}
For multiple ":"
std::vector<std::string> str;
std::vector<size_t> positions;
positions.push_back(0);
do
{
positions.push_back(line.find(":", positions.size() - 1));
}
while(positions[positions.size() - 1] != std::string::npos);
for (int i = 0; i < positions.size(); i+=2)
{
str.push_back((line.begin() + positions[i], line.begin() + positions[i+1]));
}
But i haven`t tested it.