LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 12-03-2009, 01:23 AM   #1
ajcapri
LQ Newbie
 
Registered: Nov 2009
Posts: 8

Rep: Reputation: 0
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??
 
Old 12-03-2009, 04:36 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by ajcapri View Post
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??
  1. Define in your own mind what "special character" means.
  2. Learn a text editor such as vi. Some people might recommend emacs instead of vi.
  3. 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.
  4. Design and code your program.
  5. Try it out. (Be sure to wear eye protection.)
  6. 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! :)
 
Old 12-03-2009, 04:49 AM   #3
Gutsy_Iain_08
LQ Newbie
 
Registered: Jan 2008
Posts: 25

Rep: Reputation: 16
Lightbulb

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!
 
Old 12-03-2009, 06:51 AM   #4
ajcapri
LQ Newbie
 
Registered: Nov 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 12-03-2009, 02:11 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
@Gutsy_Iain_08 & ajcapri
Your code is impossable to read without [ code ] tags..
 
Old 12-04-2009, 12:47 AM   #6
Gutsy_Iain_08
LQ Newbie
 
Registered: Jan 2008
Posts: 25

Rep: Reputation: 16
@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.
 
  


Reply

Tags
characters, count, file, program, regarding, special


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Special Characters SimeonV SUSE / openSUSE 14 07-07-2006 01:29 PM
Special characters in filenames gmartin Linux - General 2 01-05-2006 08:22 PM
special characters greenbox Linux - Software 9 12-23-2005 07:33 PM
Special characters consty Programming 3 08-07-2005 05:53 AM
using special characters one_ro Mandriva 5 11-04-2004 08:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration