473,671 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding fopen( ) function

61 New Member
Hi,

I have a question regarding the fopen( ) function. I wrote this piece of code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6.    FILE *fp;
  7.    char file[] = "data.txt";
  8.    fp = fopen(file, "r");
  9.  
  10.    if(fp == NULL)
  11.        printf("failure\n");
  12.    else
  13.       printf("Success\n");
  14.  
  15. }
  16.  
  17.  
When I run this program, I only get success when data.txt is in the same directory I am working in (writing out this source code). If I move out the data.txt into another directory, I get failure.

Is this normal to happen or is there another way to read use fopen( ) to open text files in another directory.

Regards.

Alien
Oct 18 '07 #1
8 9622
Banfa
9,065 Recognized Expert Moderator Expert
Is this normal to happen or is there another way to read use fopen( ) to open text files in another directory.
This is normal, to open a file you have to tell the system where to look. It defaults to the current working directory which from an IDE is the directory that the exe exists in and from a command prompt is the directory it is showing as the current directory.

If you want to open a file located elsewhere you will need to use an absolute or relative path to the file.
Oct 18 '07 #2
Alien
61 New Member
This is normal, to open a file you have to tell the system where to look. It defaults to the current working directory which from an IDE is the directory that the exe exists in and from a command prompt is the directory it is showing as the current directory.

If you want to open a file located elsewhere you will need to use an absolute or relative path to the file.
You mean something like this?

Expand|Select|Wrap|Line Numbers
  1. FILE *fp = "/home/mydirectory/data.txt";
  2.  
  3. fopen(fp, "r");
  4.  
  5.  
Oct 18 '07 #3
mohaakilla51
39 New Member
yes,or like this:
Expand|Select|Wrap|Line Numbers
  1. FILE *fp = "C:\Crazy\random\directory\that\is\absolute\data.txt";
  2.  
  3. fopen(fp, "r");
  4.  
Oct 18 '07 #4
Banfa
9,065 Recognized Expert Moderator Expert
You mean something like this?

Expand|Select|Wrap|Line Numbers
  1. FILE *fp = "/home/mydirectory/data.txt";
  2.  
  3. fopen(fp, "r");
  4.  
  5.  
no more like this

Expand|Select|Wrap|Line Numbers
  1. char filename[] = "/home/mydirectory/data.txt";
  2. FILE *fp;
  3.  
  4. fp = fopen(filename, "r");
  5.  
Oct 18 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
yes,or like this:

Code: ( text )
FILE *fp = "C:\Crazy\rando m\directory\tha t\is\absolute\d ata.tx t";

fopen(fp, "r");
Actually, you meant to say:
Expand|Select|Wrap|Line Numbers
  1. yes,or like this:
  2.  
  3. Code: ( text )
  4. FILE *fp = "C:\\Crazy\\random\\directory\\that\\is\\absolute\\data.txt";
  5.  
  6. fopen(fp, "r");
  7.  
Remember, the \ is an escape sequence and not a character.
Oct 18 '07 #6
mohaakilla51
39 New Member
woops... I knew that all along, I was Just testing you... yeah... that's it...
Oct 19 '07 #7
earth
1 New Member
This is normal, to open a file you have to tell the system where to look. It defaults to the current working directory which from an IDE is the directory that the exe exists in and from a command prompt is the directory it is showing as the current directory.

If you want to open a file located elsewhere you will need to use an absolute or relative path to the file.
From the beginning i thought that "current working directory" means the courrent directory of the "exe" file! But now i realize it is not true!!!
for example: the code in Alien's first post. suppose the .exe file and the data.txt file are in "c:\xyz" folder.
If i run the exe in this way: C:\xyz>Alien.exe it works ok.(here command prompt current directory is c:\xyz)
but if i try to run it in this way: C:\>xyz\Alien.exe it does not work!!(here command prompt current directory is c:\, but the exe and the txt file are in same directory)
Now this is not that problem if someone is working in command prompt (one can simply go to that directory....). But it is really a problem when I want to run that Alien.exe file from another code using system("xyz\\Al ien.exe"); or system("c:\\xyz \\Alien.exe"); command. It does not work!! when the Alien.exe runs it does not looks for the data.txt file in C:\xyz folder instead it looks for the current directory where the main exe (in which i m calling the system command) is!
Here my question is : is there anything i can do in the main code so that when i call the system(); command and run Alien.exe, The Alien.exe will get C:\xyz as its current working directory?
Nov 7 '07 #8
Banfa
9,065 Recognized Expert Moderator Expert
The only really platform independent way to achieve this is to pass the directory of the input file (or its entire path) on the command line of the program

instead of calling

C:\>\xyz\progra m.exe

and program.exe assuming that text.txt is in the current working directory you call it like this

C:\>\xyz\progra m.exe C:\xyz\

or

C:\>\xyz\progra m.exe C:\xyz\text.txt

and program.exe uses the information provided to open text.txt from the correct location.

Alternitively you could temporarily change the current working directory of the calling program but I don't think this is a good idea.


If you are happy to use non-portable code then it rather depends on your platform. For instance on Windows you can call GetModuleFileNa me to retrieve the full path of the exe from which you could construct the path to text.txt.
Nov 7 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
5527
by: doctorhardik | last post by:
hai all when i try create new file using fopen function in php in "W" mode. it dose not create file. Create test.php file in /var/www/html/ directory on linux machine. past this simple code: <?php
3
22043
by: siyaverma | last post by:
i am trying to upload csv file from user's computer to main server the code i am using is if(((isset($_GET)) && ($_GET=="yes")) ) { $typefield = $_GET; echo "<form enctype=\"multipart/form-data\" action=\"$PHP_SELF\" method=\"POST\">
6
1990
by: chazzy69 | last post by:
So the function fopen() works most of the time heres the line im using- $file = fopen($nextPage, "r") or exit("Unable to open file!"); But for some reason that i haven't been able to discern yet im getting "Unable to open file!" on specific urls this wouldn't be so much of a problem but it ends up hanging the rest of the program up. Anyway so far i cant see any reason for the errors to occur becuase when theres a error i...
1
2121
by: jeddiki | last post by:
Hi, I am confused about whether I should be using just the file() function or both fopen() function and the file() function. I have a file that is a list of words in a text fle. Now I want to be able to use it with a php script so I want to get it into a mysql database. The file not comma separated it is just a simple list
0
8481
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8400
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8823
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8602
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6234
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.