C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2009, 04:38 PM   #1
Registered User
 
Join Date: Oct 2008
Posts: 63
Networks Project fopen (input filename)

Hi there sorry if this is a repost, I am in a networking course and have a question (with many more to come).

First off a little bit about the proj, its an FTP stop and wait program.
As of right now I have created the sockets and have been able to connect and send txt. No I am trying to send data. My first step is taken a file name inputted by the user. Question is do i ask for filename or filepath

Code:
  
printf("Please enter the name of the file: ");
    
    bzero(filename,256);
    fgets(filename,255,stdin);
    
   
      if ( (in=fopen(filename,"rb")) == NULL )
        {
          printf("Could not open %s", filename);
          exit(1);
        }
      else
    	  printf("File exists.");
TaiL is offline   Reply With Quote
Old 07-08-2009, 04:47 PM   #2
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 460
It depends...If you know your process will not change its working directory, and the files will be relative to that location, then ask for a filename. Other then that you should probably get the full path to the file. You can also ask for a filename if all file selections will be relative to some pre known directory.
valaris is offline   Reply With Quote
Old 07-08-2009, 05:49 PM   #3
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 890
Code:
    bzero(filename,256);
    fgets(filename,255,stdin);
fgets() will terminate the string with a \0 -- there is no need to bzero() the buffer here. Furthermore, you can pass 256, if that's the actual size of filename.
__________________
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
Cactus_Hugger is offline   Reply With Quote
Old 07-08-2009, 06:59 PM   #4
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,357
bzero()? Let me guess, you're using some coding tutorial written by an El1t3 H4kkx0r?
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 07-13-2009, 03:54 PM   #5
Registered User
 
Join Date: Oct 2008
Posts: 63
Quote:
Originally Posted by brewbuck View Post
bzero()? Let me guess, you're using some coding tutorial written by an El1t3 H4kkx0r?
yup
TaiL is offline   Reply With Quote
Old 07-13-2009, 04:53 PM   #6
Registered User
 
Join Date: Oct 2008
Posts: 63
ok so i still don't get whats going on here

Code:
int main()
{
	char c;
	unsigned char filename[256];
	FILE *file;

	printf("Please enter the name of the file: ");
	fgets(filename,255,stdin);


	file = fopen(filename, "r");

	if(file == NULL){

		printf("Error: can't open file: %s.\n", filename);
		return 1;
	}
	else{
		printf("File opened successfully. Contents: \n\n ");

		while(1){
			c = fgetc(file);
			if(c!=EOF){
				printf("%c", c);
			}

			else{
				break;
			}
		}

		printf("\n\n Now closing file . . . \n");
		fclose(file);
		return 0;

	}
}

if i pass in "text.txt" into the command line (same folder) shouldn't it be able to open it?
TaiL is offline   Reply With Quote
Old 07-13-2009, 05:02 PM   #7
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,805
No. The problem is that when you type in "text.txt", what actually goes into your buffer is "text.txt\n". A newline character is at the end of your filename string. You need to remove this newline character before you call fopen().
bithub is offline   Reply With Quote
Old 07-13-2009, 05:05 PM   #8
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 457
Check the value of int file.
slingerland3g is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a Input File and outputing desired information using if-else,loop,and if-else m3rc3n4y C Programming 13 04-05-2009 10:16 AM
Trouble with a lab michael- C Programming 18 12-06-2005 11:28 PM
Message printing problem robert_sun C Programming 1 05-18-2004 05:05 AM
Problem with Printing message robert_sun C Programming 2 05-16-2004 02:09 PM
Getting user input for filename loop jpchand C++ Programming 1 09-16-2003 06:37 AM


All times are GMT -6. The time now is 02:59 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22