How would I modify this function...

My professor (for a c++ class) is using this bit of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	in.get(foo, MAX_CHAR, ';');
	while (!in.eof())
	{
		in.get();							//remove field seperator ';'			
		in.get(bar, MAX_CHAR, '\n');
		in.ignore(100, '\n');				//remove record seperator '\n'

		anEntry.setFoo(foo);
		anEntry.setBar(bar);

		addEntry(anEntry);

		in.get(foo, MAX_CHAR, ';');		//start the next record
	}
	in.close();


to read a file to memory that looks like this...

1
2
foo;bar
foo;bar



So the question is how would I modify the above code block to process a file that looks like

1
2
foo;bar;foobar
foo;bar;foobar



This is not the whole assignment, so this not me asking you to do my home work, I am just stuck here, and I do not know what to do. This seems to be the key element that I seem to be missing. Any help would be great, thank you in advance. Edit; If not can someone help me understand what this piece of code is doing? In systematic literal terms?

updated copy and paste errors.
Last edited on
closed account (o1vk4iN6)
You can find out what everything is doing by reading the documentation for the API he is using: http://www.cplusplus.com/reference/istream/istream/ .

It seems you have copy and paste errors where you tried to convert some things to foo and bar but left others as "name" and "email". Once you know what the code does you should easily be able to finish the assignment as everything you need is basically already written for you.
can you help me understand what this code is doing? I feel like I hitting a brick wall and need alittle more direction than just the documentation (documentation helps) but its not saying much to me at this point. I need a little more help here. Can I answer any other questions? or provide any more code?
closed account (o1vk4iN6)
All the code is doing is reading a file and using the data that was read to set variables for a class. You don't even need to look at the code to see that, just the format of the text file gives huge hint to what the code will have to do. He is using ";" to separate fields and new lines to separate records. In C++ a new line character can be defined by '\n' (some systems use different line endings though ie windows uses "\r\n"). I don't know how much simpler that can be explained, if you've never worked with files or don't know how they are represented I'd suggest looking into that.
Last edited on

Small digression: professor is a she not a he.

I think I need a little more explanation, here is my understanding of what is happening here. Maybe you or anyone else, can point out the wholes in my logic?

Breaking down the code into smaller sections with my out look on it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	in.get(foo, MAX_CHAR, ';');
	while (!in.eof())
	{
		in.get();							//remove field seperator ';'			
		in.get(bar, MAX_CHAR, '\n');
		in.ignore(100, '\n');				//remove record seperator '\n'

		anEntry.setFoo(foo);
		anEntry.setBar(bar);

		addEntry(anEntry);

		in.get(foo, MAX_CHAR, ';');		//start the next record
	}
	in.close();



Below, This is saying: get char until, MAX_CHAR(100 in this case), OR you hit a semi colon.

 
	in.get(foo, MAX_CHAR, ';');

so this (I am assuming) is taking
foo; and storing it into foo.

then a while loop happens that runs while eof is not true.
while (!in.eof())

The above two statements make the least sense to me. As in I do not understand the logic.

Then the code goes on to gobble up the remaining ";"
 
in.get();							//remove field seperator ';'			 


Then
its reading the rest of the line up to the new line char

in.get(bar, MAX_CHAR, '\n');

setters -- applying the data to variables.
1
2
		anEntry.setFoo(foo);
		anEntry.setBar(bar);


adding -- data to file

 
		addEntry(anEntry);


says its starting a new record - but I am not understanding how...
the creates a "new record "

 
		in.get(foo, MAX_CHAR, ';');		//start the next record 



and over all I just don't understand what is happening systematically. based on this above code and how to apply that understanding to modify this code to read

1
2
foo;bar;foobar
foo;bar;foobar


can some one walk me through this. I really want to understand.
Last edited on
Topic archived. No new replies allowed.