LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   read string after specific string from a text file using C++ programing language (https://www.linuxquestions.org/questions/programming-9/read-string-after-specific-string-from-a-text-file-using-c-programing-language-760479/)

badwl24 10-08-2009 02:16 AM

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...

smeezekitty 10-08-2009 02:27 AM

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;
}


badwl24 10-08-2009 02:44 AM

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

smeezekitty 10-08-2009 02:50 AM

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!

lutusp 10-08-2009 03:10 AM

Quote:

Originally Posted by badwl24 (Post 3711959)
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?

badwl24 10-08-2009 05:41 AM

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..


All times are GMT -5. The time now is 11:47 AM.