| | |
Working in Visual C++ to parse some text files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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!

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!
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
Look at std::ifstream. E.g.:
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <string> //... std::ifstream infile("somefile"); if (!infile.good()) { //failed to open } std::string line; std::getline(line,infile); //read a line into std::string
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
out of this stony rubbish?"
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!
nothing too incredibly fancy, im learning though
and...err disregard some of the comments...
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!
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
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?"
out of this stony rubbish?"
•
•
•
•
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
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!
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.
•
•
•
•
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.
*Voted best profile in the world*
•
•
•
•
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.
Last edited by Killer_Typo; Sep 23rd, 2006 at 2:50 pm.
Dont forget to spread the reputation to those that deserve!
![]() |
Similar Threads
- Making arrays from text files (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: Help finding C++ Projects
- Next Thread: Using the ANSI driver to display screen colors
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






