whitespace in strings

Started by
7 comments, last by Fade Ethereal 16 years, 4 months ago
Im trying to use fopen() to open a file. Obviously. Well, Im using ReadConsole() to get a string, the filename that I want to open. So, I do this...

char* filename = new char[20];
int read;
ReadConsole(hKeyboard, filename, 20, &read, NULL);

fopen(Filename,"rb");
That only works if the whole file name is exactly 20 chars long. So, my question is, is there a way to get rid of the extra chars in the string? Or something else to use aside from ReadConsole? Or fopen? Thanks
Advertisement
Assuming you are strictly using C you can first create a buffer for the filename string and make sure that it is zereo'd.

char filename[20] = {0};

int read;

ReadConsole(hKeyboard, &filename, 20, &read, NULL);


fopen(Filename,"rb");
In other situations you could also do something like this:

filename[read] = 0;

That will put a 'null terminator' at the end of the string. Characters after that won't matter because all standard string operations will stop at the 0.

Also, just FYI: 'whitespace' means characters like space, tab, newline. The problem you were having is actually an 'uninitialized buffer'.
Also: char* filename = new char[20]();
Quote:Original post by raz0r
Also: char* filename = new char[20]();


That is an option, however I see no need for that buffer to reside on the heap in the first place, thus the suggestion I gave .... just sayin' ;)
I assume this is what you want to do; with complete source:

#include <iostream>#include <windows.h>using namespace std;int main(int argc, char **argv){	const int MAX_CHARS = 20; // Total number of characters to read	char *filename = NULL; // memory for string	DWORD read = 0; // total bytes read	HANDLE hKeyboard = 0; // handle to console input buffer			// obtain console input buffer handle	hKeyboard = GetStdHandle(STD_INPUT_HANDLE);	// error getting console input buffer handle	if (hKeyboard == INVALID_HANDLE_VALUE)	{		cout << "Could not get handle to console input buffer!" << endl;		return 1;		}//if			// allocate memory	filename = new char[MAX_CHARS]	if (filename)	{			// clear allocated string of garbage		memset(filename, 0, MAX_CHARS);		// console carrot		cout << ">" << flush;		// read console input from the console input buffer		ReadConsole(hKeyboard, filename, MAX_CHARS, &read, NULL);				// affix a null terminator to the read string; don't forget zero '-1'		filename[read-1] = '\0';		// confirm/write out what was read		cout << "result: \"" << filename << "\"" << endl;		// free memory		delete filename;		filename = NULL;	}//if	else	{		cout << "Could not allocate " << MAX_CHARS << " of memory for buffer." << endl;		return 2;		}//else		// done!	return 0;}//main
Hey thanks you all. I managed to get it to work now.

	char* tempfilename = new char[MAX_CHARS]();	DWORD read;	ReadConsole(m_Keyboard, tempfilename, MAX_CHARS, &read, NULL);	char* filename = new char[read - 2]();	for(int i = 0; i < (read - 2); i++)		filename = tempfilename;


For some reason when using ReadConsole, it actually reads in 2 more chars than I type. I assume thats the termination characters, '\0', right? So now what I wanted works fine.
I got a small side effect, if the file name is over 11+3 long, fopen cant open the file. Is that just a shortcoming of fopen?

Thanks again.

Those two "extra" characters that you speak of are probably '\r' and '\n'. Don't let fopen see them...
Carriage return and Newline? Hmmmmm. Something more to keep an eye on. Thanks raz0r

This topic is closed to new replies.

Advertisement