ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do i write a program in C++, to read each line from a file, and then find out and display the line number and word which contains the special character??
How do i write a program in C++, to read each line from a file, and then find out and display the line number and word which contains the special character??
Define in your own mind what "special character" means.
Learn a text editor such as vi. Some people might recommend emacs instead of vi.
Get a good book on C++. I'd recommend one by Bjarne Stroustrup, who invented C++, but others on this forum might recommend other books or online tutorials.
Design and code your program.
Try it out. (Be sure to wear eye protection.)
If it works, you're done. If it doesn't, post the code in this forum (sufficiently complete so we can compile it without having to modify it) and ask specific questions, describing in detail the code's behavior and what you'd like that behavior to be instead.
Create a class and make a counter that increments each time a line is read.
#include <iostream>
#include "stdio.h"
using namespace std;
class Count{
int cnt; // your counter
public:
Count(){
cnt = 0;
count_lines(); // a function in your class constructor
}
void count_lines(){ // the function
string str;
while(cin >> str){
cnt++; // count how many times it happens with the ++
cout << cnt << " " << str << endl;
}}
~Count(){ cout << "Counted " << cnt << " lines"<< endl;} // finally destroy the class and tell how many lines you read.
};
int main(int argc, char ** argv)
{
freopen("file.txt", "r", stdin); // open file for reading
Count(); // count the lines in the file.
return 0;
}
If you want to add a function that reads the string and finds the special characters then you can do it too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.