Working in Visual C++ to parse some text files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Working in Visual C++ to parse some text files

 
1
  #1
Sep 12th, 2006
Howdy there all again. I have been working in C# for some time and think i am ready to graduate to a real language that can actually work hand in hand with memory managment

so my first venture is to write a piece of software that will open a file and allow me to make edits to it (it will be an xml file that contains information for my customers)

My current problem is that i do not know how to open a file for reading.

i have no problem calling the file reading ability of C++!! but it is very confusing to me at best. This was easily done in C# but then again im not supprised, my dad calls that language a sissy language :lol: (he's been programming since pascal and fortran! punchcards for the win)

so basically ive looked online at some tutorials but im not quite sure what to do.

i just want to open the file for reading and read through it line by line parsing out the input from the file looking for certain strings of text that need to be edited.

im not worried about the former part, im just interested in learning out to open files for reading!

thanks to all that can help, if any more information is needed just say so!
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Working in Visual C++ to parse some text files

 
3
  #2
Sep 12th, 2006
Look at std::ifstream. E.g.:
  1. #include <fstream>
  2. #include <string>
  3. //...
  4. std::ifstream infile("somefile");
  5. if (!infile.good())
  6. {
  7. //failed to open
  8. }
  9. std::string line;
  10. std::getline(line,infile); //read a line into std::string
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Working in Visual C++ to parse some text files

 
1
  #3
Sep 12th, 2006
awesome, thank you very much! i will look into that immediately!
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Working in Visual C++ to parse some text files

 
1
  #4
Sep 12th, 2006
Here is a link I find a useful reference.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Working in Visual C++ to parse some text files

 
0
  #5
Sep 22nd, 2006
short little update!

i have been working on a personal web file optimizer. it does nothing more to optimize than remove the newlines and return cariges along with the tabs from the document to reduce its file size.

this of course would be what you did when it came time to upload it. now how you would work with it on your machine.

right now its not all that smart but its something right!

private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
fldBrowse->ShowDialog();
textBox1->Text = fldBrowse->SelectedPath;
}
private: System::Void button2_Click(System::Object * sender, System::EventArgs * e)
{
//**** yes, i can read the file now!!! **** YOU BILLYGATES
fleDialog->set_InitialDirectory(textBox1->Text);
fleDialog->ShowDialog();
textBox2->Text = fleDialog->get_FileName();
 
//if they didnt chose a file then dont do anything
if(fleDialog->get_FileName() != S"")
{
//read out the file
StreamReader* objReader = new StreamReader(fleDialog->get_FileName());
 
 
//read it to a string
System::String __gc* strReader = objReader->ReadToEnd();
objReader->Close();
//cut the file up
String* delimStr = S"\r,\t,\n";//pull out the return,tabs,newlins
Char delimiter[] = delimStr->ToCharArray();//transform into an array
String* strSplit[] = strReader->Split(delimiter);//split up the file
IEnumerator* myenum = strSplit->GetEnumerator();//get enumerators
lblString->Text = strReader;
//output the file
while (myenum->MoveNext())//enumerate through it
{
txtString->AppendText(Convert::ToString(myenum->Current));//output currentenum
}
//clean up memeory
delete delimiter;
delete strSplit;
 
//save the newly created file
String* newFile = txtString->Text;
StreamWriter* objWriter = new StreamWriter(fleDialog->get_FileName());
objWriter->Write(newFile);
objWriter->Close();
}
}

nothing too incredibly fancy, im learning though and...err disregard some of the comments...
Last edited by Killer_Typo; Sep 22nd, 2006 at 8:47 pm.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Working in Visual C++ to parse some text files

 
1
  #6
Sep 23rd, 2006
Are you using microsoft's managed extensions for C++ (.NET in VS 2003)? You might want to consider using C++/CLI instead for .NET programming (you can download Visual C++ 2005 Express for free). Keep in mind though, that C++/CLI is really its own language
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Working in Visual C++ to parse some text files

 
1
  #7
Sep 23rd, 2006
Originally Posted by GloriousEremite View Post
Are you using microsoft's managed extensions for C++ (.NET in VS 2003)? You might want to consider using C++/CLI instead for .NET programming (you can download Visual C++ 2005 Express for free). Keep in mind though, that C++/CLI is really its own language
Already using Microsoft Visual C++ (in .NET 2003)

my school has an academic alliance with MS so i can get access to all of the tools and OS's they offer for free. so to be honest im using what is offered on their site.

the version is
Microsoft Visual C++ .NET 69462-335-0000007-18585
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Working in Visual C++ to parse some text files

 
1
  #8
Sep 23rd, 2006
If you want to learn c++ properly, try coding your problem from the command line only- i.e no GUI. (I know it may be hard coming from c# or a java background but if you do it that way you'll pick up better habbits regarding program speed and efficiency. After all isn't that why you wish to learn c++?)

i just want to open the file for reading and read through it line by line parsing out the input from the file looking for certain strings of text that need to be edited.
There are of course better suited languaged you can use to parse xml files than c++. However, if you want to do that look at the methods availabe for the std::string class.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Working in Visual C++ to parse some text files

 
1
  #9
Sep 23rd, 2006
Originally Posted by iamthwee View Post
If you want to learn c++ properly, try coding your problem from the command line only- i.e no GUI. (I know it may be hard coming from c# or a java background but if you do it that way you'll pick up better habbits regarding program speed and efficiency. After all isn't that why you wish to learn c++?)



There are of course better suited languaged you can use to parse xml files than c++. However, if you want to do that look at the methods availabe for the std::string class.
cool, i will take that into consideration. Most of what i got to work came straight from the MS website;although, the first language i have ever worked in was C and that was a long time ago. Then i tried out C++, didnt like it, and moved to just HTML (not a language i know!) then to PHP and then onto MySQL; i am currently dabbling in JavaScript and decided it was about time to come back to a real mans language :lol:
Last edited by Killer_Typo; Sep 23rd, 2006 at 2:50 pm.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC