473,413 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Open a file in C

Hello All,

I am want to open a file in C language where in i know the location of
the file, but do not know its name.

For this what i have done is: -

1) Used system command like and listed all the files at the location
(using ls) and stored the output in another file at a different
location.
2) Then i open this new file (created after ls) and read it line by
line. All these lines will have the name of the files which needs to
be pened by me.
3) Then I concatenate this file name with the location of the file
(and put it in a variable say absolutefilename) resulting in the
absolute file name i.e. /absolute-path/filename
4) I then try to open this file by passing variable absolutefilename
to fopen function.

But file does not open. When i give the file name statically to th
fopen function at design-time file opens.

Please help on this.

Thanx a ton in advance.

Regards,
Saket
Nov 14 '05 #1
5 2068
Saket <sa*************@yahoo.com> wrote:
Hello All, I am want to open a file in C language where in i know the location of
the file, but do not know its name. For this what i have done is: - 1) Used system command like and listed all the files at the location
(using ls) and stored the output in another file at a different
location.
2) Then i open this new file (created after ls) and read it line by
line. All these lines will have the name of the files which needs to
be pened by me.
3) Then I concatenate this file name with the location of the file
(and put it in a variable say absolutefilename) resulting in the
absolute file name i.e. /absolute-path/filename
4) I then try to open this file by passing variable absolutefilename
to fopen function.
That is a rather complicated operation, probably your system has
some (alas platform-dependent) functions that lets you find out
all the files in a directory.
But file does not open. When i give the file name statically to th
fopen function at design-time file opens.


Without showing the code probably nobody will be able to help
you. My guess is that you read the file names with fgets() and
forget to remove the trailing '\n' from the name or that there
is some problem is with the string concatenations, but without
seeing what you actually do I can't tell...

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
if u know the path but doan know the filename, u might want to #include
<dirent.h> , and then opendir() and readdir() functions might be usefull..
Nov 14 '05 #3
c453___ <mu**@nospamo2.pl> writes:
if u know the path but doan know the filename, u might want to
#include <dirent.h> , and then opendir() and readdir() functions
might be usefull.


But be aware that <dirent.h>, opendir(), and readdir() are not part of
standard C. If you want more information about them (assuming your
system supports them), first try your system's documentation, then a
Google search; if that fails, try comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Mac
On Wed, 08 Sep 2004 03:53:20 -0700, Saket wrote:
Hello All,

I am want to open a file in C language where in i know the location of
the file, but do not know its name.

For this what i have done is: -

1) Used system command like and listed all the files at the location
(using ls) and stored the output in another file at a different
location.
2) Then i open this new file (created after ls) and read it line by
line. All these lines will have the name of the files which needs to
be pened by me.
3) Then I concatenate this file name with the location of the file
(and put it in a variable say absolutefilename) resulting in the
absolute file name i.e. /absolute-path/filename
4) I then try to open this file by passing variable absolutefilename
to fopen function.

But file does not open. When i give the file name statically to th
fopen function at design-time file opens.

Please help on this.

Thanx a ton in advance.

Regards,
Saket


If the file opens when the name is given "statically," but not when you
construct it via the process described above, there is only one
explanation: you are not constructing the name correctly.

You can check this by re-working the code and, instead of trying to
open the file, just print the "static" filename, the constructed
filename, and the result of strcmp() when you call it with both
filenames.

#include<string.h> for strcmp().

--Mac

Nov 14 '05 #5
On 8 Sep 2004 03:53:20 -0700, sa*************@yahoo.com (Saket) wrote:
Hello All,

I am want to open a file in C language where in i know the location of
the file, but do not know its name.

For this what i have done is: -

1) Used system command like and listed all the files at the location
(using ls) and stored the output in another file at a different
location.
2) Then i open this new file (created after ls) and read it line by
line. All these lines will have the name of the files which needs to
be pened by me.
3) Then I concatenate this file name with the location of the file
(and put it in a variable say absolutefilename) resulting in the
absolute file name i.e. /absolute-path/filename
4) I then try to open this file by passing variable absolutefilename
to fopen function.

But file does not open. When i give the file name statically to th
fopen function at design-time file opens.

Please help on this.

printf("'%s'", absolutefilename);
should tell you what the problem is.

Thanx a ton in advance.

Regards,
Saket


Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yanivmad | last post by:
hi I like to ask if there any option to control the html files?!? I have some HTML files at my web site that use one "Help.htm" file for all the pages {I use the "window.open(.... " option at...
9
by: Charles F McDevitt | last post by:
I'm trying to upgrade some old code that used old iostreams. At one place in the code, I have a path/filename in a wchar_t string (unicode utf-16). I need to open an ifstream to that file. ...
6
by: Dino Buljubasic | last post by:
My application creates some temporary files that are deleted when my application terminates. However, if a temp file is open, it will not be deleted and application will crash. How can I...
2
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a mouseup event. I also have an open file dialog for...
6
by: qysbc | last post by:
I have a web page and there is a link to open a TIFF file. The way I do it is to have the server code open a binary stream, set the content type to "image/tiff" and call Response.BinaryWrite. On...
2
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take...
6
by: Moumen VB.NET 2003/2005 Developer | last post by:
How can I detect if a file sitting on a network drive is still open by another application? This application resides on another machine on the network? I am using VB.NET 2003 Your help is...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
6
by: Ros | last post by:
There are 10 files in the folder. I wish to process all the files one by one. But if the files are open or some processing is going on them then I do not want to disturb that process. In that case...
0
by: Ofelia | last post by:
Hi, I'm new to this forum and to Perl language but I would like to ask for your help. I'm working in Linux and the files I need to process are in the format “file.gz”. I created a script which...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.