Networks Project fopen (input filename)

This is a discussion on Networks Project fopen (input filename) within the C Programming forums, part of the General Programming Boards category; Hi there sorry if this is a repost, I am in a networking course and have a question (with many ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    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.");

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    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.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    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)

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    bzero()? Let me guess, you're using some coding tutorial written by an El1t3 H4kkx0r?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    Quote Originally Posted by brewbuck View Post
    bzero()? Let me guess, you're using some coding tutorial written by an El1t3 H4kkx0r?
    yup

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    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?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,260
    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().

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Check the value of int file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 04-05-2009, 11:16 AM
  2. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  3. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 06:05 AM
  4. Problem with Printing message
    By robert_sun in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 03:09 PM
  5. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 07:37 AM

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