Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Closed Thread
 
Thread Tools Search this Thread Display Modes
 
Old 02-10-2006, 05:41 PM   #1
Baseband Member
 
David Ireland's Avatar
 
Join Date: Dec 2005
Posts: 56
Default 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
David Ireland is offline  
Old 02-12-2006, 08:27 AM   #2
Fully Optimized
 
UK31337's Avatar
 
Join Date: Feb 2005
Posts: 2,776
Default 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.
UK31337 is offline  
Old 02-12-2006, 05:49 PM   #3
Baseband Member
 
Griz803's Avatar
 
Join Date: Jun 2004
Posts: 89
Default 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*/
Griz803 is offline  
Old 03-17-2006, 08:42 AM   #4
Baseband Member
 
Join Date: Mar 2006
Posts: 39
Default Re: Easy C Question

I would have to agree with griz about what the problem is.
Nucular is offline  
Old 03-19-2006, 10:19 PM   #5
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Easy C Question

Code:
#include <iostream>
#include <fstream>
#include <string>

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 <iostream>
#include <fstream>
#include <string>

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.
RewtGuy is offline  
Old 03-20-2006, 03:36 AM   #6
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,003
Default 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."
root is offline  
Old 03-20-2006, 02:26 PM   #7
Golden Master
 
mark thorpe's Avatar
 
Join Date: Feb 2005
Posts: 7,366
Send a message via MSN to mark thorpe Send a message via Yahoo to mark thorpe
Default 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
mark thorpe is offline  
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 08:40 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0