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;
}
Related
Here is the minimal reproducible code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const string TEST_FILE_PATH = "output.txt";
void write_to_file(string path, string line);
void generate_passwords(string file);
int main()
{
generate_passwords(TEST_FILE_PATH);
return 0;
}
void generate_passwords(string file)
{
int count = 1;
for (int len = 1; len <= 100; len++)
{
for (int i = 0; i < 100; i++)
{
write_to_file(file, to_string(count) + " : ");
count++;
}
}
}
void write_to_file(string path, string line)
{
ofstream file;
file.open(path, ios::out | ios::app);
cout << "writing line : " << line << endl;
if (file.is_open())
{
cout << "file is open " << endl;
file << line << '\n';
cout << "written..." << endl;
}
else {
cout << "FILE NOT OPEN" << endl;
}
file.close();
}
This is a stripped-down version of my full code, and it reproduces the same error.
With this code, I expect exactly 10,000 lines to be written to output.txt. However, it sometimes skips a random number of lines:
See how it skips numbers 6466 to 6509.
The console prints "FILE NOT OPEN" for these numbers.
This rarely happens with fewer iterations, but occurs very often with large numbers of iterations.
What could be the issue?
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 am trying to read from a .csv file. There are two functions below, one for writing and one for reading.
The file contains a simple table:
date,first,second
1 a one
2 b two
3 c three
4 c four
For some reason, the statement while(file_stream.read(&c,1)); does not read anything. It stops at the first character and I'm dumbfounded as to why. Any clues?
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
std::string filename;
std::string line_string;
ifstream file_stream;
stringstream ss;
vector< vector<string> > vec;
char c;
void read_file()
{
filename = "test.csv";
cout << filename << endl;
file_stream.open(filename.c_str(),ios::out|ios::binary);
if(file_stream.fail())
{
cout << "File didn't open" << endl;
return;
}
if(file_stream.is_open())
cout << "file opened" << endl;
while(file_stream.read(&c,1)); // this isn't working
{
cout <<"char c is: " << c;
ss << noskipws << c;
}
file_stream.close();
cout << "string is: " << ss.str() << endl;
//get each line
int counter = 0;
vector<string> invec;
while(getline(ss,line_string,'\n'))
{
string header_string;
stringstream header_stream;
header_stream << line_string;
while(getline(header_stream, header_string,','))
{
invec.push_back(header_string);
}
invec.push_back(header_string);
vec.push_back(invec);
invec.clear();
counter++;
}
}
void test_output()
{
for(int i = 0; i < vec.size();i++)
{
for(int in = 0; in < vec[0].size(); in++)
cout << vec[i][in] << " ";
cout << endl;
}
}
int main()
{
read_file();
test_output();
}
Look very very carefully at the line that is not working:
while(file_stream.read(&c,1)); // this isn't working
{
cout <<"char c is: " << c;
ss << noskipws << c;
}
The ; character at the end of the while statement does NOT belong! You are running a no-body loop that does not terminate until read() fails, and THEN your code enters the bracketed block to output the last character that was successfully read (if any).
You need to remove that erroneous ; character:
while(file_stream.read(&c,1)) // this works
{
cout <<"char c is: " << c;
ss << noskipws << c;
}
Now, the real question is - why are you reading the input file character-by-character into a std::stringstream in the first place? You can use std::getline() with the input std::ifstream directly:
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
std::vector< std::vector<std::string> > vec;
void read_file()
{
std::string filename = "test.csv";
std::cout << filename << std::endl;
std::ifstream file_stream;
file_stream.open(filename.c_str(), ios::binary);
if (!file_stream)
{
std::cout << "File didn't open" << std::endl;
return;
}
std::cout << "file opened" << std::endl;
//get each line
std::vector<std::string> invec;
std::string line;
int counter = 0;
if (std::getline(file_stream, line))
{
std::istringstream iss(line);
while (std::getline(iss, line, ','))
invec.push_back(line);
vec.push_back(invec);
invec.clear();
++counter;
while (std::getline(file_stream, line))
{
iss.str(line);
while (iss >> line)
invec.push_back(line);
vec.push_back(invec);
invec.clear();
++counter;
}
}
}
void test_output()
{
if (!vec.empty())
{
for(int in = 0; in < vec[0].size(); ++in)
std::cout << vec[0][in] << ",";
std::cout << std::endl;
for(int i = 1; i < vec.size(); ++i)
{
for(int in = 0; in < vec[i].size(); ++in)
std::cout << vec[i][in] << " ";
std::cout << std::endl;
}
}
}
int main()
{
read_file();
test_output();
}
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 have a program that I need to be able to search a file with regex epressions and delete what regex has found. Here is the code I have been working on:
#include <boost/regex.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "time.h"
using namespace std;
class application{
private:
//Variables
boost::regex expression;
boost::smatch matches;
string line;
string pat;
int lineNumber;
string replace;
char time[9];
char date[9];
//Functions
void getExpression(){
cout << "Expression: ";
cin >> pat;
try{
expression = pat;
}
catch(boost::bad_expression){
cout << pat << " is not a valid regular expression\n";
exit(1);
}
}
void boostMatch(){
//Files to open
//Input Files
ifstream in("files/trff292010.csv");
if(!in) cerr << "no file\n";
//Output Files
ofstream out("files/ORIGtrff292010.csv");
ofstream newFile("files/NEWtrff292010.csv");
ofstream record("files/record.dat");
//time
_strdate_s(date);
_strtime_s(time);
lineNumber = 0;
while(in.peek() != EOF){
getline(in, line, '\n');
lineNumber++;
out << line << "\n";
if (regex_search(line, matches, expression)){
for (int i = 0; i<matches.size(); ++i){
record << "Date: "<< date << "Time: " << time << "\tmatches[" << i << "]: " << matches[i] << "\n\tLine Number: "<< lineNumber<< '\n\t\t' << line << '\n';
boost::regex_replace(line, expression, "");
newFile << line << "\n";
}
}else{
newFile << line << "\n";
}
}
}
public:
void run(){
replace = "";
getExpression();
boostMatch();
}
};
As you can see I was trying to use boost::regex_replace to just replace what was found with a blank space, but this did not work. The test I have been running is using [*] to find all the asterisks before some names in a list. Example *alice. The program does find the star but does not remove is to just alice
It seems like boost::regex_replace is returning a string instead of modifying the input. See the documentation for this method.
Try this instead:
newFile << boost::regex_replace(line, expression, "") << "\n";
Escape the * with a \ .
This is a fairly common issue,
http://bytes.com/topic/c/answers/166133-problem-boost-regex_replace
Maybe the above link helps