Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-03-2009, 01:23 AM
|
#1
|
|
LQ Newbie
Registered: Nov 2009
Posts: 8
Rep:
|
program regarding special characters in c++
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??
|
|
|
|
12-03-2009, 04:36 AM
|
#2
|
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 806
Rep: 
|
Quote:
Originally Posted by ajcapri
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.
Good luck! We're cheering you on! :)
|
|
|
|
12-03-2009, 04:49 AM
|
#3
|
|
LQ Newbie
Registered: Jan 2008
Posts: 25
Rep:
|
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.
void special_characters(string str){ // manipulate str }
// and put it in the count_lines function!
|
|
|
|
12-03-2009, 06:51 AM
|
#4
|
|
LQ Newbie
Registered: Nov 2009
Posts: 8
Original Poster
Rep:
|
Thanks for the replies...
this is what i've tried...de code is not fully correct..
#include<conio.h>
#include <fstream.h>
#include <iostream.h>
#include <string.h>
#include <stdio.h>
int main()
{
char GetFileName[30],*line;
int linecount=0,i=0,x;
cout << "System >> Enter file to read: \n";
cout << "User >> ";
cin >> GetFileName;
ifstream filename;
filename.open( GetFileName,ios::in);
if(! filename )
{
cout << "Unable to open file: " << GetFileName << endl;
return 1;
}
else
{
while (filename.getline(line,100,'\n')) //Loop through lines
{
char str[] = filename.getline(line,100,'\n');
linecount++;
char * pch;
pch = strtok (str," ");
while (pch != NULL)
{
do
{
str[i]<-getc(pch);
x=int(str[i]);
if( !(x>47 & x<58) || !(x>65 && x< 93)|| !(x>96 && x<123) || (x!=32 || x!=10 || x!=13) )
{
cout<<"\nline number is : "<<linecount<<endl;
cout<<"\nWord containing special character is : "<<pch<<endl;
}
}while( (str[i++]<-pch) !='\0');
//printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
}
filename.close();
}
return 0;
}
can any1 solve the problem???
many thnks
|
|
|
|
12-03-2009, 02:11 PM
|
#5
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,136
Rep: 
|
@Gutsy_Iain_08 & ajcapri
Your code is impossable to read without [ code ] tags..
|
|
|
|
12-04-2009, 12:47 AM
|
#6
|
|
LQ Newbie
Registered: Jan 2008
Posts: 25
Rep:
|
@ajcapri, your code seems like it can make it if you keep at it, but put it into a class because it will work better that way, just use your line
Quote:
|
if( !(x>47 & x<58) || !(x>65 && x< 93)|| !(x>96 && x<123) || (x!=32 || x!=10 || x!=13) )
|
inside a function called something that indicates it will find the special characters.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:48 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|