![]() |
| | #1 |
| Registered User Join Date: Oct 2008
Posts: 63
| Networks Project fopen (input filename) 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 | |
| | #2 |
| Registered User 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 | |
| | #3 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 890
| Code: bzero(filename,256);
fgets(filename,255,stdin);
__________________ 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 | |
| | #4 |
| Senior software engineer 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 | |
| | #5 |
| Registered User Join Date: Oct 2008
Posts: 63
| |
| TaiL is offline | |
| | #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 | |
| | #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 | |
| | #8 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 457
| Check the value of int file. |
| slingerland3g is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |