FileIO homework

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
HavocVulture
Posts: 16
Joined: October 30th, 2017, 11:50 pm

FileIO homework

Post by HavocVulture » June 13th, 2018, 6:36 pm

I'm getting erratic behavior with the print function shown in the tutorial. Sometimes it overwrites text written to the console previously.

For example:

Code: Select all

struct nvPair {
	char name[10];
	char value[5];
};

nvPair pairs[100];

void printChart( nvPair pairs[], const int nameLength, const int valueLength, int nvIndex ) {
	fio::print( "	Beautiful Chart Bitches!\n" );
	fio::print( "	------------------------\n\n" );

	for ( int j = 0; j < nvIndex; j++ ) {
		fio::print( pairs[j].name );
		fio::print( "|" );

		for ( int i = 0; i < fio::str2int( pairs[i].value ); i++ ) {
			fio::print( "=" );
		}
		fio::print( "\n" );
	}
}
If I add a name value pair, say, "steve" and "3", what's output is:

fio::print( pairs[j].name ); outputs "steve 3"

Not sure where the 3 is coming from.

fio::print( "|" ); overwrites the "s" in "steve"

fio::print( "=" ); overwrites more letters in "steve" for each "=".

Is this another VC++ compiler error or am I using it wrong?

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: FileIO homework

Post by MrGodin » June 15th, 2018, 12:28 am

not sure what the fio::print function is doing, possible not setting a end line call or '\n' at the end of each print ?
Curiosity killed the cat, satisfaction brought him back

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: FileIO homework

Post by chili » June 15th, 2018, 1:32 am

gib the full file an we can take a quick look at er.
Chili

HavocVulture
Posts: 16
Joined: October 30th, 2017, 11:50 pm

Re: FileIO homework

Post by HavocVulture » June 19th, 2018, 1:34 am


albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: FileIO homework

Post by albinopapa » June 19th, 2018, 4:30 am

while (c != 13 && (buf < pEnd + 1))

13? A new line is represented as \n or 10, 13 is carriage return \r. This is why your cursor goes back to the beginning of the current line instead of next line.

Ok, so it does need to be 13 to end the input, but I don't think it should be added to the string.

Code: Select all

	void read(char* buf, int maxSize) {
		const char* const pEnd = buf + maxSize;
		char c = 0;

		while (c != 13 && (buf < pEnd)) {
			c = _getch();
			if (c != 0) {
				_putch(c);
				*buf = c;
				buf++;
			}
		}

		*buf = 0;
	}
Probably should be echoing the characters when you read them. The console prints out the characters as they are typed, so no need. Also, when you press enter, you echo the \r and the cursor gets returned to the beginning of current line.

Another thing is if you mistype and press backspace, it's counted and added to the buffer which is probably not what you want. Perhaps skipping anything that isn't a alpha character in names and numeric character in values ( like tab ( \t ), backspace ( \b ), etc... ).
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

HavocVulture
Posts: 16
Joined: October 30th, 2017, 11:50 pm

Re: FileIO homework

Post by HavocVulture » June 20th, 2018, 2:05 am

Thanks albinopapa. I added a function to limit non alphanumeric characters and fixed readw so the string is properly null terminated. Seems to be functioning a lot better now. You can see the changes on github.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: FileIO homework

Post by albinopapa » June 20th, 2018, 4:56 pm

You're welcome.

It's weird really. In the 6 years I've been doing this, I haven't run into this issue dealing with the console, only when displaying text in a custom GUI. I'm guessing it's because I probably didn't follow along this time around. Using the C library fprint and scanf or the C++ library std::cout and std::cin it deals with tabs and backspaces and only presents you with everything up until the enter key. Using _putch and _getch on the other hand, you must handle every key press.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply