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!

folder and accessing file..

Status
Not open for further replies.

leela74

Programmer
May 11, 2001
26
IN
Hi, SwapSwae

Nice to see u r reply in the thread "going to particular folder".

Yeah u r right I am getting 99% every time and unable to get what I want...

Now today I got the code as u suggested and only one error in this code is it couldn't able to allocate sufficient memory allocation... How to give or know how much space it occupies..

The code goes like this...

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

FILE *fp,*fs;

int main()
{
//char name[8], command[20]=&quot;md &quot;;
char path[8] =&quot;E:\\abc&quot;;
char name[8];
// char drv;
// char abc[20] = &quot;\&quot;;

printf(&quot;Enter absolute path for Directory to be created :&quot;);
scanf(&quot;%s&quot;,name);
strcat(name, &quot;.txt&quot;);
strcat(path,&quot;\\&quot;);
strcat(path, name);
printf (&quot;The folder is.... %s\n&quot;, path);
fflush(stdin);
//system(command1);
//system(command);
fp = (char *) malloc(sizeof(char) * (strlen(path) + 5));
fp=fopen(path,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;\nNo such file&quot;);
exit(1);
}
printf(&quot;\nFile opened successfully....&quot;);
return 1;

}

The above code is opeing the file successfully... but in in running the compiler is saying &quot;unable to rea memory....&quot; though I have given the malloc..

What could be the error...

Thanks in advance

Sridhar
 
First of all u never malloc a FILE pointer.
And the second problem is the length of ur path variable is too less. Increase it to say 18 not necessary I just used an arbitary value.
following is a working code.
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
#include <malloc.h>

FILE *fp,*fs;

int main()
{
//char name[8], command[20]=&quot;md &quot;;
char path[18] =&quot;C:\\SwapC&quot;;//8] =&quot;E:\\abc&quot;;
char name[8];
// char drv;
// char abc[20] = &quot;\&quot;;

printf(&quot;Enter absolute path for Directory to be created :&quot;);
scanf(&quot;%s&quot;,name);
strcat(name, &quot;.txt&quot;);
strcat(path,&quot;\\&quot;);
strcat(path, name);
printf (&quot;The folder is.... %s\n&quot;, path);
fflush(stdin);
//system(command1);
//system(command);
//fp = (char *) malloc(sizeof(char) * (strlen(path) + 5));
fp=fopen(path,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;\nNo such file&quot;);
exit(1);
}
printf(&quot;\nFile opened successfully....&quot;);
return 1;

}

Regards,
SwapSawe.s-)
 
Hi,

Once again...

Yeah u r right. I have not declared enough space for the path...

Now the code is working fine.. sometimes small things gives lot of problems...

Now I have one more doubt...

I am keeping my main() loop never ending.. in the sense the main keeps on working...

when this happens the first time code is working fine.. the loop is working for the second time the same variables are existing... and subsequently the path, file name all getting errors..

to overcome this what I have to do...

Thanks in advance...

Sridhar
 
Hi Sridhar,
First of all thanx for making me feel special.
Now back to work.
Use a while(true) loop and declare all the variables above this loop. But assigning values to them should be done inside the while loop.

Hope that helps.
Regards,
SwapSawe.
 
Hi, SwapSawe

Really u r special for me... As I have made some mistakes which is almost unrecognisable and in a confusing position and u r the one who showed me the way. Thanks for that.

Now again the same as u said I have created a while loop and I am saying false in the end so that the loop never ends.. but the problem is the folder name and path are still in memory and when again some different person folder should open it is giving error as the path is error...

Thanks in advance

Sridhar:)
 
Hi! Sridhar,
Thanx 4 all that. Here is what I could make out about what ur question is. Pardon me if i m wrong. I think ur question is >>
When first time prog runs, the path is &quot;C:\\SwapC&quot; and then we concatenate the name of file to it making it look something like this &quot;C:\\SwapC\\abc.txt&quot;, Now when u return bk to the same u already have &quot;C:\\SwapC\\abc.txt&quot; in path and now if u concatenate another name to it, this will make path look like &quot;C:\\SwapC\\abc.txt\xyz.txt&quot;. If this is what u r asking then I'll say u have not read my mail clearly, I said declare them before while and assign values inside while what i meant is :
char path[18];
char name[8];
while(true)
{
path=&quot;C:\\SwapC&quot;;
//accept name;
strcat(path,name);
...
...
}
Hope this helps if not pls post question with example.
Regards,:)
SwapSawe.
 
Hi, SwapSwae

Morning!. I understood corrctly of what u said. I have declared the same way as u said. The error message I am getting when I am assigning path is..

E:\Sridhar\Opening Device\devopen.cpp(279) : error C2440: '=' : cannot convert from 'char [7]' to 'char [18]'
There is no context in which this conversion is possible

before while(true) loop I have declared path[18];
and after the while loop I have assigned the value...

Thanks in advance

Sridhar
 
Hi, SwapSwae

The following iis the code...

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

FILE *fp,*fs;

int main()
{
//char name[8], command[20]=&quot;md &quot;;
char path[18];
char name[8];
char cont = 'y';
// char drv;
// char abc[20] = &quot;\&quot;;
while(cont)
{
path = &quot;E:\\abc&quot;;
printf(&quot;Enter absolute path for Directory to be created :&quot;);
scanf(&quot;%s&quot;,name);
strcat(name, &quot;.txt&quot;);
strcat(path,&quot;\\&quot;);
strcat(path, name);
printf (&quot;The folder is.... %s\n&quot;, path);
fflush(stdin);
//system(command1);
//system(command);
name = (char *) malloc(sizeof(char) * (strlen(path) + 5));
fp=fopen(path,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;\nNo such file&quot;);
exit(1);
}
printf(&quot;\nFile opened successfully....&quot;);
return 1;
cont = 'n';
}

}

the following is the error messages...

error C2440: '=' : cannot convert from 'char [7]' to 'char [18]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert from 'char *' to 'char [8]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
Error executing cl.exe.

Thanks in advance

Sridhar
 
SO I think I got ur problem the proble lies in this line

name = (char *) malloc(sizeof(char) * (strlen(path) + 5));

when the name var contains only 8 elements in declaration how the malloc can allocate a memory string of sizeof path which is 18.

Hope u find ur answer.

Regards,
SwapSawe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top