Sign in to follow this  

whitespace in strings

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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");

Share this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites
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' ;)

Share this post


Link to post
Share on other sites
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



Share this post


Link to post
Share on other sites
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[i] = tempfilename[i];


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.

Share this post


Link to post
Share on other sites

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this