|
That is what confuses me, David.
This is what I thought:
I have to put the string I read into somewhere (e.g. into a buffer) before I can use strlen() to get the length of the string.
Is there another way to accomplish this task? E.g. how do I know the length of the string I read before I put it to a buffer?
Thanks
|
|
|
|
|
But where do you allocate memory for *buffer?
What about this:
char* buffer = new char[10];
gets(buffer);
char* st=buffer;
printf("%s %s",st,buffer);
delete buffer;
Press: 1500 to 2,200 messages in just 6 days? How's that possible sir?
Dr.Brad :Well,I just replied to everything Graus did and then argued with Negus for a bit.
|
|
|
|
|
Hi VuNic:
Thank you for your reply.
I am still wondering if there is a way for me somehow get the length of the word I will read without declaring a pointer (buffer) which points to a char array of certain length?
Thank you
|
|
|
|
|
Are you supposed to stick to "C" ? or you have the option of using std::string s?
why not use them? or you need to go for linked lists. like :
typedef struct stringz
{
char c;
struct stringz* next;
}st;
st* str =(st*)malloc(sizeof(stringz));
st* head = str;
char c;
while(c=getch())
{
if(c==13)
break;
str->c=c;
printf("%c",str->c);
str->next=(st*)malloc(sizeof(stringz));
str=str->next;
}
str->next=NULL;
str =head;
while(str->next!=NULL)
{
printf("%c",str->c);
str=str->next;
}
purely dynamic , hey but never mess up trying your own stringz!. If allowed, I suggest you go with std::string.
Press: 1500 to 2,200 messages in just 6 days? How's that possible sir?
Dr.Brad :Well,I just replied to everything Graus did and then argued with Negus for a bit.
|
|
|
|
|
|
Uhmmm – No!
To use standard C functions – you need to provide the size of the buffer. C does not create or expand buffers for you – you have to request them manually. Normally you can get away with allocating a buffer that is as large as or larger than you expect to need and just make the call one time.
Off the top of my head, if you need something that resembles dynamic allocation in C:
typedef struct st_MyStringBuffer
{
size_t length;
char* buffer;
} MyStringBuffer;
int MyBuffer_Init(MyStringBuffer* pMine)
{
if( pMine )
{
memset(pMine, 0, sizeof(MyStringBuffer));
return(1);
}
return(0);
}
void MyBuffer_Free(MyStringBuffer* pMine)
{
if( pMine )
{
free(pMine->buffer);
MyBuffer_Init(pMine);
}
}
char* MyBuffer_AddString(MyStringBuffer* pMine, const char* pStr)
{
size_t len;
if( !pMine || !pStr )
return(NULL);
len = strlen(pStr) + 1;
if( !pMine->buffer )
{
pMine->buffer = malloc(len);
if( !pMine->buffer )
return(NULL);
pMine->buffer[0] = '\0';
pMine->length = len;
}
else
{
len += pMine->length;
if( !realloc(pMine->buffer, len) )
return(NULL);
pMine->length = len;
}
return strcat(pMine->buffer, pStr);
}
char* YourFunction()
{
char temp_buffer[256];
MyStringBuffer my_string_buffer;
MyBuffer_Init(my_string_buffer);
while( fgets(temp_buffer, 256, file_ptr) )
{
if( !MyBuffer_AddString(my_string_buffer, temp_buffer) )
{
MyBuffer_Free(my_string_buffer);
break;
}
}
return( my_string_buffer->buffer );
}
Well that was fun! Of course you could eliminate MyStringBuffer and just do esentially the same thing it does in your function. The advantage of a method like this is that now you can reuse MyStringBuffer any where you need to use that functionality, without having to rewrite it each time.
Any way I did not test the code, so I do not know if it works as expected.
Study the code and try to figure out what it is doing and why.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
|
Hi:
I wanted to read whatever I receive from stdin to stdout and quit the program. Below is my function:
while(scanf("%s", buffer) != EOF) {
for (i = 0; i < strlen(buffer); i++) {
buffer[i] = tolower(buffer[i]);
}
printf("%s ", buffer);
}
The function actually keeps looping forever. It does echo whatever from stdin to stdout, but it does not quit after one go. I am wondering why?
Thank you.
|
|
|
|
|
Question withdrawn. I have found a solution.
|
|
|
|
|
C_Zealot wrote: I have found a solution
what was it?
Press: 1500 to 2,200 messages in just 6 days? How's that possible sir?
Dr.Brad :Well,I just replied to everything Graus did and then argued with Negus for a bit.
|
|
|
|
|
Hi:
Simply another approach,
I wrote:
gets(buffer); /*Read a string into the buffer*/
for (i = 0; i < strlen(buffer); i++) { /*Convert each char in the string to its low case*/
buffer[i] = tolower(buffer[i]);
}
printf("%s", buffer); /*Print it at once*/
It can be confusing because whoever reads this can not access my whole program.
But my problem was solved.
Thanks
|
|
|
|
|
Not sure why noone has mentioned it yet, but if you have it, strlwr(...) is likely a faster/better way to lowercase the string in buffer .
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
C_Zealot wrote: while(scanf("%s", buffer) != EOF) {
scanf() does not operate on stdin . Use fscanf(stdin, ...) for that.
C_Zealot wrote: The function actually keeps looping forever.
So why aren't you checking to see why that is so?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: scanf() does not operate on stdin . Use fscanf(stdin, ...) for that.
Just out of curiosity, which RTL implementation are you using?
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
James R. Twine wrote: Just out of curiosity, which RTL implementation are you using?
I know, it sounds funny, but keeping those two functions straight sometimes requires slightly off-kilter thinking. I've just never considered the keyboard to be standard input. Standard input, to me, has always been something like:
myprog.exe < somefile.dat
or
myprog.exe | someotherprog.exe
I'll go back to sleep now.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hi:
Below is the function I have been trying to write:
What I try to achieve is that:
1: Read one char at a time from a file pointed by fp.
2: A word consists of only alphabetic char or underscore '_'.
3: Store the word in a local_buffer.
4: Copy the word into a global_buffer and return it to the caller function.
The outcome I expect is:
Say, fp pointing to a text file 'Hello World, What is up??".
The outcome I expect is: (My function returns one word at a time.)
Hello
World
What
is
up
But the outcome I got is:
Hello
World
What
is
up
It seems that my function can not deal with successive 'space' properly. I am wondering why?
while((ch_read = fgetc(fp)) != 32) {
if (ch_read == EOF) {
EOF_Reached = TRUE;
strncpy (global_buffer, local_buffer, count);
global_buffer[count] = '\0';
return global_buffer;
}
if ((isalpha(ch_read) != 0)||(ch_read == '_')) {
local_buffer[count] = ch_read;
count++;
}
}
}
strncpy (global_buffer, local_buffer, count);
global_buffer[count] = '\0';
return global_buffer;
Many thanks
|
|
|
|
|
The text file fp pointing to contains:
"Hello world, (space) (space) (space) What is up??".
|
|
|
|
|
C_Zealot wrote: while((ch_read = fgetc(fp)) != 32) {
Your outer loop needs to read until EOF, not a space, is reached.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thank you. I will try it.
|
|
|
|
|
Hi all. Im having trouble with a DNS client. I spent a great deal of time trying to figure out a few little things and now its not even doing what i want.
My code:
<br />
#include <iostream><br />
#include <winsock2.h><br />
using namespace std;<br />
<br />
main(){<br />
const int SIZE=100;<br />
char msg[SIZE];<br />
WORD wVersionRequested;<br />
WSADATA wsa;<br />
struct hostent *host;<br />
printf("Please enter the Host name to look up.\n");<br />
cin.getline(msg, SIZE);<br />
printf("Doing a DNS lookup of '%s'\n",msg);<br />
wVersionRequested=MAKEWORD(2, 0);<br />
WSAStartup(wVersionRequested, &wsa);<br />
host=gethostbyname(msg);<br />
if((host = gethostbyname(msg)) == NULL)<br />
{<br />
printf("DNS Lookup of '%s' failed!\n",msg);<br />
}<br />
else<br />
{<br />
printf("DNS Results are: '%s'\n",host);<br />
}<br />
WSACleanup();<br />
system("pause");<br />
return 0;<br />
}<br />
Took me a half a hour to figure out how to initialize wVersionRequested. Now it compiles with a charm. however it doesnt do a DNS lookup. In fact its only printing out useless information. Whats am i missing? Thanx in advance!
|
|
|
|
|
You can't printf a hostent* like that
Maybe something like this (non-Unicode code):
if((host = gethostbyname(msg)) == NULL)
{
printf("DNS Lookup of '%s' failed!\n",msg);
}
else
{
if (host->h_addrtype == AF_INET)
{
in_addr inaddr = *(in_addr *)(host->h_addr_list[0]);
char *pAddStr = ::inet_ntoa(inaddr);
if (pAddStr)
{
printf("DNS Results are: '%s'\n", pAddStr);
}
}
}
Mark
-- modified at 19:16 Monday 30th April, 2007
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Thanx Mark Salsbery it worked fine. I'll be looking into getaddrinfo() later. I hear its a lot more simpler.
|
|
|
|
|
You're welcome! getaddrinfo() is nice, especially with gethostbyname() being deprecated.
getaddrinfo() is marked as XP and above though, which is why I removed it from my post.
I'm not sure why I did...it made sense yesterday
Have fun,
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
I'm sure this has been asked a billion times already but on a quick search I couldn't find the answer.
I'm having some issues with getting MsFlexgrid to work on VC++ v 7.1.3088 using .NET Framework 1.1 SP1. When I try to insert the microsoft FlexGrid version 6.0 it says it could not be instantiated because it needs a design-time license. I'm not sure I understand why this is coming up. I registered the control, but apparently it needs more.
Any help is appreciated.
Thanks!
|
|
|
|
|
can image processing application b developed with the inbuilt libraries???i mean without using CIMG or other this kind ov libraries???
if anybody has done it...can they gimme the code of how to read and process an image???im doing a project in image processing,the project is conversion of monochrome film to colored!
can v read a video im VC++?how to go abt it???
waitin for a prompt reply...my deadline is round the corner...
thanx...
Evil Bluez
|
|
|
|