Computer Forums Easy C Question

 02-10-2006, 05:41 PM #1 Baseband Member     Join Date: Dec 2005 Posts: 56 Easy C Question How can i open a file for reading and writing in a directory other than "C:\"?? All I know is this... Code: if((fp = fopen("C:\hello.txt", "w")) != NULL) { fprintf(fp, "Hello."); fclose(fp); } else puts("Write Failed."); So when I do that all i can read and write are files in C:\ and no other directory...like C:\whatever...if i try that it fails. So to clarify...if i were to write C:\directory\text.txt it fails...i tried putting C:\\directory\\text.txt but that fails too. Thanks. __________________ __________________ Dave ----- Computer Systems Tech Durham College
 02-12-2006, 08:27 AM #2 Fully Optimized     Join Date: Feb 2005 Posts: 2,776 Re: Easy C Question You could try passing in the filename and path as a parameter, that might work... but I'm not 100% sure. But this type of problem is notorious, I've had VERY similar problems to you in that regard. You think it should work, and it won't. Hope I've helped, try Googling it if I've not been of much use. __________________ __________________ Master of common sense. If you don't like it, stop reading.
 02-12-2006, 05:49 PM #3 Baseband Member     Join Date: Jun 2004 Posts: 89 Re: Easy C Question I'm not 100% sure, but the problem sounds to me like Windows XP not granting your program access to the directory you are requesting. Just a hunch, but I've seen the same thing on some XP systems. Try using the Windows API calls in console mode to write the file and see what you get. That is my current best guess. I'd have tried it myself before I opened my mouth, but my machine here is currently running Linux and I'm not going to reconfigure it until this project gets done next month. I hope this helps. __________________ /*What does not kill us, makes us drink far too much caffeine*/
 03-17-2006, 08:42 AM #4 Baseband Member   Join Date: Mar 2006 Posts: 39 Re: Easy C Question I would have to agree with griz about what the problem is.
 03-19-2006, 10:19 PM #5 Daemon Poster     Join Date: Dec 2004 Posts: 595 Re: Easy C Question Code: #include #include #include using namespace std; int main (int argc, char * argv[]) { string line; ifstream file (argv[1]); if (file.is_open()) { while (! file.eof() ) { getline (file,line); cout << line << endl; } file.close(); } else cout << "Unable to open file"; return 0; } This is good when you execute your program from a terminal, just put the /path/to/file after you open it for example: c:\open.exe c:\asdf\another_folder\bleh.txt if you want to be able to pick your directory in the program you'd have to do this Code: #include #include #include using namespace std; int main () { string asdf, line; cin >> asdf; ifstream file (asdf.c_str()); if (file.is_open()) { while (! file.eof() ) { getline (file,line); cout << line << endl; } file.close(); } else cout << "Unable to open file"; return 0; } Remember c/c++ is case sensative. so if you were to type in c:\ it wouldn't work. It's C:\ __________________ Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
 03-20-2006, 03:36 AM #6 Site Team     Join Date: Mar 2004 Posts: 8,003 Re: Easy C Question first... don't drag up old threads. someone reported this because you dragged it up from the past, second, that code is c++, not C, so it doesn't even answer the question properly. __________________ I didn’t fight my way to the top of the food chain to be a vegetarian… Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns. "The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
 03-20-2006, 02:26 PM #7 Golden Master     Join Date: Feb 2005 Posts: 7,366 Re: Easy C Question nuculear youve been warned enough times. temp ban for a week, ive warned you on several occasions about dragging up old threads, user Nucular temp banned for a week thread locked __________________ __________________ 'I may be drunk, Miss, but in the morning I will be sober and you will still be ugly.' Winston Churchill, 11/30/1874 - 01/24/1965