Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

going to a particular folder... 1

Status
Not open for further replies.

leela74

Programmer
May 11, 2001
26
IN
Hi,

I am facing a problem to go to a particular folder ad access a file..

The code goes like this...

# include <stdio.h>
# include <string.h>
# include <stdlib.h>

FILE *fp,*fs;

int main()
{
char name[8];
char command[20]=&quot;cd &quot;;

printf(&quot;Enter absolute path for Directory to be changed :&quot;);
scanf(&quot;%s&quot;,name);
strcat(command,name);
fflush(stdin);
system(command);
fp=fopen(&quot;abc.txt&quot;,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;\nNo such file&quot;);
exit(1);
}
printf(&quot;\nFile opened successfully....&quot;);
return 1;
}

I am unable :-( to access file abc.txt in c:\abc\abc.txt.:-(

Thanks in advance

Sridhar
 
Havent realy used the &quot;system()&quot; function so not sure how it works, but maybe this variation might help.

# include <stdio.h>
# include <string.h>
# include <stdlib.h>

FILE *fp,*fs;

int main()
{
char name[8] = &quot;abc&quot;;
char command[20]=&quot;/abc.txt&quot;;
printf(&quot;Enter absolute path for Directory to be changed :&quot;);
strcat(name,command);
fflush(stdin);
fp=fopen(name,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;\nNo such file&quot;);
exit(1);
}

printf(&quot;\nFile opened successfully....&quot;);
return 1;
}

 
oops! took the scanf() out while I tried it out and forgot to put it back in.

# include <stdio.h>
# include <string.h>
# include <stdlib.h>

FILE *fp,*fs;

int main()
{
char name[8];
char command[20]=&quot;/abc.txt&quot;;
printf(&quot;Enter absolute path for Directory to be changed :&quot;);
scanf(&quot;%s&quot;,name);
strcat(name,command);
fflush(stdin);
fp=fopen(name,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;\nNo such file&quot;);
exit(1);
}
 
C has a specific meaning for backslash which can be removed using two back slashes and then ur program will run fine. Say it this way-
System(&quot;cd c:\\abc\\abc.txt&quot;);

C that works, :)
SwapSawe.
 
Hi, SwapSawe

Bit confused with the code. I am unable to move to that folder where this abc.txt exists...

The code is just exiting with out any message.

To recap what I need is...

I want to open a file in a particular folder..For eg. there is a folder &quot;abc&quot; in c drive in that there is a file called abc.txt I want to open that with c code... help is required in that...

I tried the above code and it is not working...

Thanks in advance

Sridhar
 
Yes SwapSawe is right. While using the \ in the directory path we should use \\. so write some statement to replace the / with // in read path.

Copy all the characters in the name array to an another array, if / is present in the name array copy double // in the destination array and do remaining work.

Regards
Maniraja S



 
Use the following code.

#include <stdio.h>
#include <string.h>
int main()
{
char fname[15], folder[50], afolder[60];
int i, j;
scanf(&quot;%[^'\n']&quot;, fname);
fflush(stdin);
scanf(&quot;%[^'\n']&quot;, folder);
for(i = 0, j = 0; folder != '\0'; i++, j++)
{
afolder[j] = folder;
if(folder == '\\') afolder[++j] = '\\';
}
afolder[j++] = '\\';
afolder[j++] = '\\';
afolder[j] = 0;
strcat(afolder, fname);
if( (fp = fopen(afolder, &quot;r&quot;)) == NULL) printf(&quot;\nCheck the file Name : %s : Unable to open...&quot;, afolder);
return 0;
}


Regards
Maniraja S
 
leela74,
Cannot understand the need of using system command if the same you can do without doing a change of directory.

concatenate your file name abc.txt to full directory path and use fopen,it will try to open the file in that directory only,secondly I think you do not require to replace \ with \\ if you are using it in variable name but you do require if you are using \ in string literals.
 
Hi Sridhar,

I tried out ur program and found certain problems with using cd command for changing directory with system so rather than going for changing the directory, direclty concat the path with the file name and open the file without changing the path. And ya I was wrong at one more place if u enter a backslash '\' in a string variable then C automatically puts '\\' there and u need not do this implictly. I will try to find out why cd does not work however u open the file without cahnging the directory till then.

Regards and Thanx for this question which taught me something I didnot know.
s-)
SwapSawe.
 
Hi, SwapSwae

Nice to see u r reply... It is even teaching me alot and I am learning after I started this program.

To recap I will tell u what I need....

I want to open a file which is existing in a specific folder. Say for eg. in C drive I have a file abc.txt under the folder abc.. I want to open that file through my program.... i.e

file path = c:\abc\abc.txt
program path = c:\file.exe..

So how my program access this file. I have the above code and still implementing new ideas...

It would be helpful to me if I get it....

Thanks in advance

Sridhar
 
fp = fopen(&quot;c:\\abc\\abc.txt&quot;,&quot;r&quot;);
or
strcat(name,&quot;\\abc.txt&quot;);
fp=fopen(name,&quot;r&quot;);

Regards,
SwapSawe.
 
This is fine but I want this to be dynamic i.e everytime the folder name changes and so as the file name.

How can I say dynamically to open a file in a particular folder...

One way is dynamically pass the folder name and file name into a temp variable and concatenate and open a file.

Till concatinating it is working fine. I am confused how to say fopen();

Thanks in advance

Sridhar
 
Hey Sridhar!!
Why is it that every time u reach 99% near to the logic and get exhausted till u make it 100%. Well prob bcause of u get exhausted by the time u reach round the corner. well here it is. Check out these are two logics I exactly couldnot get whether it is always the abc.txt file or is it a .txt file with the same name as the innermost directory.
Well we will check both logic.
Say u accepted the absolute pathname in a variable path.
say, path=&quot;C:\\SwapC&quot;
Variation 1.
The file name is always abc.txt.
strcat(path,&quot;\\abc.txt&quot;);
fp = fopen(path,&quot;r&quot;);
and the file is open.

If it is the other one do that. Sorry I didnot complete the second logic right now I m getting call on duty. I don't think that u'll need any help with that and hey start another thread now it has run too long.
Happy programming.
:)SwapSawe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top