LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 10-08-2009, 02:16 AM   #1
badwl24
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
read string after specific string from a text file using C++ programing language


Hello everybody...
I am very new to C++ programming... I have some text files from where i have to extract some string followed by specific string.

Here is the text file(input text file "My Assembly.txt")

"File = E:\Files\SWwork\Point Assembly.SLDASM"
" Mates"
" Coincident1"
""
" Coincident1"
" Type = 0"
" AlignFlag = 2"
" CanBeFlipped = False"
""
" Component = base-1"
" MateEntType = 0"
" (x,y,z) = (5.57589607891816, 1.65404008879722, -0.144701889346493)"
" (i,j,k) = (1, 0, 0)"
" Radius 1 = 0"
" Radius 2 = 0"

" Coincident1"
" Type = 0"
" AlignFlag = 2"
" CanBeFlipped = False"
""
" Component = cylinder-1"
" MateEntType = 0"
" (x,y,z) = (5.57589607891816, 1.65404008879722, -0.144701889346493)"
" (i,j,k) = (1, 0, 0)"
" Radius 1 = 0"
" Radius 2 = 0"

" ------------------------------------------------"
" Coincident2"
""
" Coincident2"
" Type = 0"
" AlignFlag = 1"
" CanBeFlipped = False"
""
" Component = base-1"
" MateEntType = 3"
" (x,y,z) = (-5.42410392108184, -1.34595991120278, -0.144701889346493)"
" (i,j,k) = (0, 0, -1)"
" Radius 1 = 0"
" Radius 2 = 0"

" Coincident2"
" Type = 0"
" AlignFlag = 1"
" CanBeFlipped = False"
""
" Component = cylinder-1"
" MateEntType = 3"
" (x,y,z) = (4.07589607891816, 1.65404008879722, -0.144701889346493)"
" (i,j,k) = (-8.83369898710739E-32, 3.35432798647444E-33, 1)"
" Radius 1 = 0"
" Radius 2 = 0"

" ------------------------------------------------"
" Parallel1"
""
" Parallel1"
" Type = 3"
" AlignFlag = 1"
" CanBeFlipped = False"
""
" Component = base-1"
" MateEntType = 1"
" (x,y,z) = (5.57589607891816, -1.34595991120278, 0.230298110653507)"
" (i,j,k) = (2.96059473233375E-16, 1, 0)"
" Radius 1 = 0"
" Radius 2 = 0"

" Parallel1"
" Type = 3"
" AlignFlag = 1"
" CanBeFlipped = False"
""
" Component = cylinder-1"
" MateEntType = 1"
" (x,y,z) = (5.57589607891816, 1.65404008879722, -0.144701889346493)"
" (i,j,k) = (-2.9620403352304E-16, -1, 3.2632012271689E-33)"
" Radius 1 = 0"
" Radius 2 = 0"

" ------------------------------------------------"

" MASS-PROPERTY"
" ------------------------------------------------"
"Part Name : cylinder-1"
""
"
Mass = 4767.97785335121"
"
Volume = 4.76797785335121"
"
Density = 1000"
"
Surface Area = 31.0065250621291"
"
Center of Mass : "
"
X = 1.24302021626616E-16"
"
Y = 0.209239388437286"
"
Z = 1.5"
"------------------------------------------------"
"Part Name : base-1"
""
"
Mass = 12281.4590787394"
"
Volume = 12.2814590787394"
"
Density = 1000"
"
Surface Area = 77.7918228991562"
"
Center of Mass : "
"
X = 2.30939100915037E-03"
"
Y = -2.04810538914209E-16"
"
Z = 0.1875"
"------------------------------------------------"



I have tried a program to read this text file....but i donot know how to read "base-1" and "cylinder-1" a string followed by a specific string for example.. i need to read the string after two consecutive "component =" and the mean while i need to read the strings like "Coincident1","Coincident2", "Parallel1".


Any help would be really helpfull !

Regards,
Borad
NIT Warangal


// FileInput - read blocks of data from a file
#include <fstream>
#include <iostream>
using namespace std;
ifstream* openFile(istream& input)
{
for(int i=0;i<10;i++)
{
// open the file specified by the user
char fileName[80];
cout << "Enter the name of a file" << endl;
// read input from the user in such a way
// that the input canít overflow the buffer
input.getline(fileName, 80);
//open file for reading; donít create the file
//if it isnít there
ifstream* pFileStream = new ifstream(fileName);
if (pFileStream->good())
{
return pFileStream;
}
cerr << "Couldnít find " << fileName << endl;
}
return 0;
}
int main(int nNumberofArgs, char* pszArgs[])
{
// get a file stream
ifstream* pFileStream = openFile(cin);
// read blocks of data 80 bytes at a time
char buffer[80];
while (!pFileStream->eof() && pFileStream->good())
{
// read a block - 80 is the max but gcount() returns
// the actual number of bytes read
pFileStream->read(buffer, 80);
int noBytes = pFileStream->gcount();
// do something with the block

for(int i = 0; i < noBytes; i++)

{
cout << buffer[i];
}
}
system("PAUSE");
return 0;
}


Thanks in advance who ever reply...

Last edited by badwl24; 10-08-2009 at 02:23 AM.
 
Old 10-08-2009, 02:27 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,208

Rep: Reputation: 147Reputation: 147
A:what you are doing is called parsing
B:your code should not even compile
i corrected the big errors:
Code:
// FileInput - read blocks of data from a file
#include <fstream>
#include <iostream>
using namespace std;
ifstream* openFile(istream& input)
{
for(int i=0;i<10;i++)
{
// open the file specified by the user
char fileName[80];
cout << "Enter the name of a file" << endl;
// read input from the user in such a way
// that the input can’t overflow the buffer
input.getline(fileName, 80);
//open file for reading; don’t create the file
//if it isn’t there
ifstream* pFileStream = new ifstream(fileName); //WTF?
if (pFileStream->good())
{
return pFileStream;
}
cerr << "Couldn’t find " << fileName << endl;
}
return 0;
}
int main(int nNumberofArgs, char* pszArgs[])
{
// get a file stream
ifstream* pFileStream = openFile(cin);
// read blocks of data 80 bytes at a time
char buffer[80];
while (!pFileStream->eof() && pFileStream->good())
{
// read a block - 80 is the max but gcount() returns
// the actual number of bytes read
pFileStream->getline(buffer, 80);  //getline is better then read
int noBytes = pFileStream->gcount(); //WTF? that does not look right
// do something with the block

for(int i = 0; i < noBytes; i++)

{
cout << (char)buffer[i]; //typecast to char to makesure std::cout gets it right
}
}
system("PAUSE"); //this should not be necessary if you run from the console
return 0;
}
 
Old 10-08-2009, 02:44 AM   #3
badwl24
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for your reply

but Sir, i need to extract only specific string..
means the output should come like this...

base-1
cylinder-1

Coincident1
Coincident2
Parallel1







more over if you can please post the code for this result also(matrix table)

...............base-1........cylinder-1

base-1..........0..............1

cylinder-1......1..............0
 
Old 10-08-2009, 02:50 AM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,208

Rep: Reputation: 147Reputation: 147
homework?
anyway i am sorry i cannot help you because i suck at parsing
i reread your post and its more complicated that i thought.
good luck!
 
Old 10-08-2009, 03:10 AM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by badwl24 View Post
Thanks for your reply

but Sir, i need to extract only specific string..
means the output should come like this...

base-1
cylinder-1

Coincident1
Coincident2
Parallel1







more over if you can please post the code for this result also(matrix table)

...............base-1........cylinder-1

base-1..........0..............1

cylinder-1......1..............0
Isn't the point of this exercise that you learn how to code? How does asking "please post the code for this result" count toward your learning how to program a computer?

Please post the name of your professor so we can send the result directly to him. That would have the advantage of being honest.

Do you think there will always be a discussion group available to solve all your programming problems, through your entire career? Do you know what the term "programming by newsgroup" means?
 
Old 10-08-2009, 05:41 AM   #6
badwl24
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
ok...Sir...
by the way thanks for reply and suggestion...
Actually i am doing M. Tech in Mechanical Engineering and right now doing project, for that i need those C++ code...

thanks once again...i will try myself to find out the code..
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace a string in a text file jpan Linux - General 3 10-14-2012 06:17 PM
search string in text and print string wolfipa Linux - Software 4 09-17-2009 08:50 AM
read string in c language dedexes Programming 5 03-05-2008 01:17 AM
generate new string from a text file walterwaston Programming 2 10-08-2007 02:14 AM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM


All times are GMT -5. The time now is 07:52 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration