C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 26th, 2009, 01:19 PM
Superman859 Superman859 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 313 Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 53 m
Reputation Power: 29
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 ?

Reply With Quote
  #2  
Old February 26th, 2009, 01:35 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,907 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 39 m 56 sec
Reputation Power: 1774
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old February 26th, 2009, 03:16 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,825 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 7 m 38 sec
Reputation Power: 1800
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

Reply With Quote
  #4  
Old February 26th, 2009, 03:59 PM
Superman859 Superman859 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 313 Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level)Superman859 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 53 m
Reputation Power: 29
Thanks for the responses.

The following fixed it.
Code:
char *p_chr = strchr(buff, '\n');
if (p_chr != NULL) *p_chr='\0';

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Simple Question Involving fopen() and char array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap