944,050 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 17777
  • C++ RSS
Sep 12th, 2006
1

Working in Visual C++ to parse some text files

Expand Post »
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!
Similar Threads
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Sep 12th, 2006
3

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

Look at std::ifstream. E.g.:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Sep 12th, 2006
1

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

awesome, thank you very much! i will look into that immediately!
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Sep 12th, 2006
1

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

Here is a link I find a useful reference.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Sep 22nd, 2006
0

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

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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Sep 23rd, 2006
1

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

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
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Sep 23rd, 2006
1

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

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
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Sep 23rd, 2006
1

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

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++?)

Quote ...
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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 23rd, 2006
1

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

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help finding C++ Projects
Next Thread in C++ Forum Timeline: Using the ANSI driver to display screen colors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC