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 :P

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!

Recommended Answers

All 8 Replies

Look at std::ifstream. E.g.:

#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
commented: super fast response and very accurate. THank you very much man! +4
commented: Helps-[Gr] +2

awesome, thank you very much! i will look into that immediately!

Here is a link I find a useful reference.

commented: rep++ from Salem +2
commented: great link +4

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)
{
//fuck yes, i can read the file now!!! FUCK 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 :D and...err disregard some of the comments...

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

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

Member Avatar for 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++?)

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.

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.