First "large" project

So i'm learning C++ for about 4 months now and school had me wite my first project.
Now it does what it has to do so far, but i need to be able to save the progress from the running programm and have it load the next time.


this is my struct
1
2
3
4
5
6
7
8
9
10
11
  struct	Verein
{
	char name[NAME_LEN];
	//wie oft gewonnen, unentschieden, verloren
		
		
		int guv[3];
	    int	tore;
    	int gegentore;
	    int punkte;
};


this is he array from that struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Verein  bundesliga[] = {
	{ "Werder Bremen", { 0, 0, 0 }, 0, 0, 0 },
	{ "Schalke 04", { 0, 0, 0 }, 0, 0, 0 },
	{ "VfB Stuttgart", { 0, 0, 0 }, 0, 0, 0 },
	{ "Bayern Muenchen", { 0, 0, 0 }, 0, 0, 0 },
	{ "1. FC Nuernberg", { 0, 0, 0 }, 0, 0, 0 },
	{ "Bayer Leverkusen", { 0, 0, 0 }, 0, 0, 0 },
	{ "Hertha BSC Berlin", { 0, 0, 0 }, 0, 0, 0 },
	{ "Hannover 96", { 0, 0, 0 }, 0, 0, 0 },
	{ "VFL Wolfsburg", { 0, 0, 0 }, 0, 0, 0 },
	{ "Borussia Dortmund", { 0, 0, 0 }, 0, 0, 0 },
	{ "Energie Cottbus", { 0, 0, 0 }, 0, 0, 0 },
	{ "Hamburger SV", { 0, 0, 0 }, 0, 0, 0 },
	{ "Alemania Aachen", {0, 0, 0 }, 0, 0, 0 },
	{ "FSV Mainz 05", { 0, 0, 0 }, 0, 0, 0 },
	{ "Arminia Bielefeld", { 0, 0, 0 }, 0, 0, 0 },
	{ "Eintracht Frankfurt", { 0, 0, 0 }, 0, 0, 0 },
	{ "VFL Bochum", { 0, 0, 0 }, 0, 0, 0 },
	{ "Borussia Moenchengladbach", { 0, 0, 0 }, 0, 0, 0 }
};


So the problem now is:
I can save the data from that Array, formatted and unformatted, in a .txt and a .csv file respectively. I can also load it, but i can not however assign the data from the csv-file to the array. In code it looks something 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

std::ifstream csvread;
	csvread.open("BuLiTabelleRichterReussDATA.csv", std::ios::in);
	if (csvread){
		//Datei bis Ende einlesen und bei ';' strings trennen 
		std::string s = "A";
		char *cstr = new char[s.length() + 1];
		do
		{
			getline(csvread, s, ';');
			//name
			strcpy_s(bundesliga[buli].name, s.c_str());
			//strcpy_s has char and const char*_Source
                        getline(csvread, s, ';');
			//guv[0]
			//char *cstr = new char[str.length() + 1];
			strcpy_s(cstr, s.c_str()); 
                        //strcpy_s has char and const char*
			// do stuff
			dummy = (int)cstr- (int)'0';
			bundesliga[buli].guv[0] = dummy;
			delete[] cstr;

		} while (csvread);

		csvread.close();
	}
	else{
		std::cerr << "Fehler beim Lesen!" << std::endl; //Error
	}


1.: Why does it tell me on the second 'strcpy_s' that it doesn't accept two arguments but on the first it uns fine?
2.: Why is "Werder Bremen" assigned as 0?

Hope the problem is clear enough and the included code is enough?
It would be helpful, if you tried translating the code(or at least comments) to english, before asking on english forum.

Anyway. Why do you even use strcpy? You've got string; this should be more then enough. When in C++, avoid using C-style strings. They are often problematic.

Why is string 's' assigned to "A"? You do realize you can just use the default constructor, creating an empty string?

Also, your code is inconsistent. getline and ifstream both belong to std, yet you only use std on the latter and not the former.

Moreover, your separator is wrong - csv stands for Comma Separated Values, and you provide semicolon as a delimiter.

And if that wasn't enough, your csv file is wrong too! Or, at least, you can't parse it the trivial way you wanted to use. See this example to get a feeling of what's wrong with your code: http://ideone.com/8wmqH8

So, to sum things up: get rid of C string, fix the style, and fix the way you save the data. Then come back with results, if you still have any problems.
Last edited on
Can't make too many comments without seeing more code, for example:

If this were a true CSV, i.e. separated by comma "," then your first getline, in line 10 would read the entire file, since you are using a semi-colon ";" as delimiter.

cstr looks like a length of 2, but you read the entire file into it - I know you only mean to read a line, but it's still too small.

Actually, strcpy_s() has 3 parameters not 2

But all this is begging the question of why you're using character arrays, raw pointers, no constructor, etc. These are features that would save you many of the problems you're facing now.
Topic archived. No new replies allowed.