Newbie Program: Editing several txt-files and more. Questions.

Hi,

I took up C++ with the dream of that in the future I'll write my own games, you havent heard of that one before i am sure.
Anyway, I thought of writing a program that would familiarize me with the world of programming again. I have learnt some HTML, PHP, SQL before, and touched Java.
So i know some of the thinking behind programming already, even though its a bit rusty.

This program would help me in Modding a game as it would minimize the textediting as the program would have everything up in a neat and easy to understand GUI. I would release this program so that others can use it as well, I am a helper person after all.


A short run down on what the program would do, and after that my question for you.


This game has many, many textfiles that needs editing when you mod it.
For example, adding a new Unit, would need you to edit first a main unit text, then the building txt(so the unit would be recruitable), then there is a model txt where the models and textures are set, and then 2 txts that deal with the display of names and description ingame.

Thats one part of the program I have in mind, I am planning on making the buildings, religions, culture and more, but that will be easy once I know how.

Probably not that hard to imagine it?


Anyway, now to my question;


I have tried to read many tutorials on C++ learning a bit, but found out that they dont contain the stuff I would need to be able to create my program, or they put it in a way that would make it hard for me to think so...

Most of them are 'Yay! Your first Hello Word program! You are now able to become a professional programmer!'


Do you have any easy to understand tutorial that would be of use for me?
Preferably one that focuses on text editing/writing.
One guide I read was the one by Herbert Schildt, chapter 11 talks about I/O, but i couldnt understand how i could make that into my program.




Could anyone give me hints on how to make this possible? Dont write it for me, I want to learn after all. My simple understanding of programming can only do so much at the moment. I thought of using a form for each of the entries in the textfiles, and they are themselves loaded into a Treelist(I believe they are called that?), and their post in that list is taken from the txt files.

Like this:

northern_barracks
barracks_lv1
Recruitable units
barracks_lv2
Recruitable units

Is that possible?

I am using Microsoft Visual C++ 2008 Express Edition.
I chose C++ as it would be the best for my later game programming.
But do you think C# or any other language is better for this?

Thank you


Pether


PS

Sorry for any stupidity that might have slipped in, I am not feeling well so my focus isnt the best.
c++ is the most common language around so keep learning this and it will
be easier to transition. As for creating a form with text file you should learn
to crawl before you can walk. Start with a win32 console application and do some
research on fstream and getline().
Your question is too long winded. You'll have more luck with just a couple of sentences / questions.
Dear god, man. All that just to ask how to open several files at once?

There is no way to do text editing. Well, not any way that is straightforward, that is.
The problem, if you can call it that, with files, particularly text files, is that they're solid. There's no room to insert or move data about.
If you need to do text processing of the kind you'd see in a text editor, what you normally do is load the entire file into memory, perform the changes, and then write back. The best structure I can think of to do this is either a linked list of std::strings (or wstrings), or a vector of pointers to std::strings. Anyway, you'd load the file into this structure by getline()ing into each std::string.
Oh, and files are read using std::ifstream and written using std::ofstream.

You can look up each of the things I mentioned in the reference.


By the way, if you don't have much programming experience -- the fact that you refer to HTML and SQL as programming languages suggests so -- you might want to try an easier language, first. Python is easy to learn, I heard, and has a very rich library.
Last edited on
Learn something lower-level, a bit more assembler based first. I jumped in the deep end after simply learning batch, and it's not working for me.

Good that you're interesting in writing a GUI though. I think it's probably a good idea to learn that first - when I first wrote a GUI app (I got the code of the internet in a tutorial and ended up with a blank white box that did nothing...) I took one look and thought 'Ok... not ready...'.

As helios said, learn something like Perl, Python or Pascal - something a little easier. Pascal is an ok language, but for some strange reason it replaces { } with BEGIN END. which I think is pointless, but whatever...

C++ is probably the best language. C is good too but less efficient I think.

For file editing you can use ifstream and ofstream - #include <fstream>. They are quite annoying to use though... no tutorial really 'agrees'. And they never mention that you can have the extension as whatever you like - I had an ANSI text file that I saved as '.dictionary' for an anagram thing that I forgot about and I was able to read from it. Was cool because windows called it a 'DICTIONARY File'.

Anyway, example of if-/of- stream:

1
2
3
4
5
6
7
8
9
10
11
12
13
// ifstream:
#include <fstream>
using namespace std;

int main () {
// Get a line from a file:
string line;
ifstream file("file.txt");
file.open;
getline(open, line);
cin.get();
return 0;
}


1
2
3
4
5
6
7
8
9
10
11
// ofstream
#include <fstream>
using namespace std;

int main() {
ofstream file("file.txt");
file.open;
ofstream >> "This will be written into the file.";
cin.get();
return 0;
}

By the way, I'm not so sure about the ifstream, but the ofstream I know works.
1
2
3
4
5
6
7
8
9
10
11
// ofstream
#include <fstream>
using namespace std;

int main() {
ofstream file("file.txt");
file.open;
ofstream >> "This will be written into the file.";
cin.get();
return 0;
}


your text output should be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <fstream>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
string name = "";
cout<<"File Name:";
cin>>name;
ofstream myFile;
myFile.open(name.c_str(),ios::app);

string line = "";
if(myFile.is_open())
{
cout<<"Created Succesfully"<<endl;
name<<"I Am Writing This To the Text File Name"<<endl;
getline(name,line,'~');
cout<<line; // Display file contents

}
else
{
cout<<"Error"<<endl;
}

system ("Pause") // Dont hate on this!
return 0;
}




try copy and pasting this into a win32 console application and play around
with it
Last edited on
@jloundy
No need to include fstream 2 times
@Ashishm, i kinda realize that but im to lazy to edit it
Topic archived. No new replies allowed.