The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Simple Question Involving fopen() and char array
Discuss Simple Question Involving fopen() and char array in the C Programming forum on Dev Shed. Simple Question Involving fopen() and char array C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 26th, 2009, 01:19 PM
|
|
|
|
Simple Question Involving fopen() and char array
I am terrible at C due to lack of experience with it (because it always frustrates me the few times I do use it), so the smallest little things make programming in C very complicated for me.
I read some text into a buffer of size 256. That text is the name of a file to open. How can I get that buffer to be used in fopen() to open the name of the file?
I think the problem is the buffer contains filename in the first N bytes, and then the rest of the 256 bytes in the buffer are also getting passed to fopen, and fopen fails.
Code:
while((len = recv(conn, buff, BUFFSIZE, 0)) > 0) {
//buff contains the filename, BUFFSIZE is 256, but generally
//buffer will not be full
//char filename[] = "file.txt"; if I use filename below
//then it works. Using buff does not
FILE *file = fopen(buff, "r");
//...
What is needed above to get fopen() to open whatever is in buff ?
|

February 26th, 2009, 01:35 PM
|
 |
Contributed User
|
|
|
|
recv() doesn't append a \0, to make it a proper string. As a result, you get whatever junk was in the buffer before hand.
Try something like
Code:
while((len = recv(conn, buff, BUFFSIZE - 1, 0)) > 0) {
buff[len] = '\0';
// now do stuff with buff
}
You need the -1 to allow room for the \0, should the buffer be filled.
|

February 26th, 2009, 03:16 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
|
You are making a dubious assumption that recv will return the complete string is one go. If conn is a stream socket, that may not be the case. Sending and receiving are asynchronous; when recv is called you will get whatever data is available, which may not be complete if the sender's data is still in transit or not yet sent.
The sender should include a delimiter, and you should receive and accumulate data in the buffer until the delimiter is received. An obvious delimiter to use a NUL character perhaps, since that would also serve to terminate the string.
It is one of those thing that might work, until it doesn't.
Clifford
|

February 26th, 2009, 03:59 PM
|
|
|
Thanks for the responses.
The following fixed it.
Code:
char *p_chr = strchr(buff, '\n');
if (p_chr != NULL) *p_chr='\0';
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|