Files

Greetins all,
I had an idea for a program project but not sure how to get started. The idea is to create a program that when the user inputs a name of a golfer (or some other athlete haven't decided what sport I want to use) and output that golfers stats. I dont think i want to actually want to perform any calculations just output the data. maybe even go far enough to do this per course and overall. What would be the best way to do this? Should I type out all the stats in my program or can I use a file? Or something else entirely? Just looking for suggestions and tips.

Thanks everyone
Last edited on
Hi @g8whitebuffalo,
There are multiple ways I would do what you requested, and here are some:

Use files. Basically, whenever you introduce a name, you access either:
1. a file with the golfer's name, or
2. a file which contains multiple golfers and their stats.

I would highly recommend using files, and so I will help you with it:
For 2. you might want to also use objects. If you don't want to or don't know them, then it's not a problem.
But at least using structures is pretty much necessary.
Here's a sample:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct golferData
{
    //some data
};
int main()
{
    //create a list of golferData structures, or an array if you want to keep it simple (which is not
//so good)
    fstream file;
    string fileName, golferName;
    int score;
    fileName="Golfers List.txt";
    file.open(fileName.c_str(), ios::in);
    while (getline(file, golferName))
    {
        file>>score;
        golferPlayer->pointsPerSeason=score;
        golferPlayer->playerName=golferName;
    }
}

Note that this is a point of reference.

Anyway, for 1. it's kinda better if you want to access it directly, but it takes some more writing:
1
2
3
4
5
6
7
8
9
10
11
int functionCreateFile(string fileName)
{
    fstream checkFile;
    checkFile.open(fileName.c_str(), ios::in);
    if (checkFile.is_open())
    {
        return 0;
    }
    fstream newFile;
    newFile.open(fileName.c_str(), ios::out);
}

For this you might want to consider adding an extension to the file, if it's needed.
Also it's for the best to first modify the file name, so that if you input "PlaYEr SomeONE STEVe", then it firstly is transformed to "Player Someone Steve".
This is for storing data.

And using the same mechanism for the user will help in reading the file with the name directly.



Now if it's not for using files, you must write everything in the source code.
That is not pleasant for the programmer, and if there's one misspelling, then you must recompile and many problems can emerge.
Hope this helps.
Last edited on
Sit down and write out a flow diagram how you want it to work. Best way to start any new project.
How do I make a file that allows me to call certain sections of it? I am fairly new to c++ I just started with objects and pointers. Iam maybe 40 % confident with using them which the only way to learn is to do in my opinion. But I've never used files I know the just but nothing more. Any certain way I have to make a file or can I just use a normal word doc ? Or do I have to make a code file to call by line? I am finding it difficult to find examples and good info on how to implement files into my code. Thanks for the insight btw @troaat for making it more clear on how to accomplish my idea, the help is very much appreciated.
https://en.wikipedia.org/wiki/Computer_file

You can check the File I/O tutorial on this site for an example:
http://www.cplusplus.com/doc/tutorial/files/

It's not reasonable for you to hand-write all of the data: there's way too much of it.
To do this in real life you would query a database, perhaps from an online service if the data is only available live.
Last edited on
g8whitebuffalo,

NO do not use a Word doc or any other fancy type of file. The extra code in the file will just confuse you. All you need is a plain text file. Thee same type of file that you would save your code in.

Something similar that I do is to take a Microsoft Excel spread sheet I use and save it as a ".csv" (comma separated value) file which is a plain text file. Then when I wrote the program it was very easy to separate each field in the file to work with the data.

Knowing how the data file is laid out gives you better idea of how the program should work.

How do I make a file that allows me to call certain sections of it?

Not sure what you mean here, but I am thinking this might getting into some programming that you may not be ready for yet.

I will mention that each full record that you want to work with will be one line in the file most likely starting wit a name followed be whatever stats are needed. Also a good idea to try and keep the number of fields in each line the same number. Even if you have to use something as a place holder, but not necessary it just makes it easier.

In the beginning files are not hard to use it is just knowing how to work with them.

Hope that helps,

Andy
Alright I have tried looking into using a cvs file but couldnt quite get how to get only what I needed out o it. That being said I also have the same problem with using a text file. All the lines are different length and I cant figure out how to loop through to just get the lines that I need. Also would it be better to make each golfer have their own file ? would this be bad practice ?
dynamic data can be a pain.
the easy way for a beginner is to pad the data.. each column might represent 1 thing, and if that column is empty for someone, that is OK. If you created this file in excel and write a csv, that would look like..

data,other,,third, ...

where ,, means there was an empty column.

one file per entity is not a good design for most programs. "it depends" but usually this is not correct.

another way to do it is to have a type marker on each entity, for example 1 byte can give you 256 types of data, then you know what "column" it belongs to.
That file looks like, if the first letter is a type,..
Tdata,Xother,Ythird, .... and you only have data for the things you have data for, without empty columns. This is doable but its annoying.. you can't "understand" the file by looking at it in excel, its harder to create the file, some bytes can't be used as types unless you read it as binary (all text files are binary, but not all binary files are text) ... and for what gains??


there are other structured and unstructured and hierarchal file formats but this stuff takes a lot of work and is not well suited for a beginner. I suggest using the CSV. Create a test file in excel. Parse by detecting the commas in each line to figure out the column which relates to what variable the data goes into.

Hello g8whitebuffalo,

It would be helpful if you would post the code and a sample of the text file so I can see what you are doing. It is hard to know what is wrong when I do not know what you have done.

Andy
Last edited on
Topic archived. No new replies allowed.