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!

repeating the folder name... 1

Status
Not open for further replies.

leela74

Programmer
May 11, 2001
26
IN
Hi, SwapSwae,

U have got the problem... and the problem is.. as u explained...

When first time prog runs, the path is "C:\\SwapC" and then we concatenate the name of file to it making it look something like this "C:\\SwapC\\abc.txt", Now when u return bk to the same u already have "C:\\SwapC\\abc.txt" in path and now if u concatenate another name to it, this will make path look like "C:\\SwapC\\abc.txt\xyz.txt". 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="C:\\SwapC";
//accept name;
strcat(path,name);
...
...
}

In the same way I have created the while loop and after while loop I have initialised the variable path and it is giving error....
the code is..

# 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);
fp=fopen(path,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;\nNo such file&quot;);
exit(1);
}
printf(&quot;\nFile opened successfully....&quot;);
cont = 'n';
}
return 1;
}

The error message is...
error C2440: '=' : cannot convert from 'char [7]' to 'char [18]'
There is no context in which this conversion is possible

Thanks in advance

Sridhar
 
do it this way:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
#include <malloc.h>

FILE *fp,*fs;

int main()
{
char path[18]=&quot;&quot;; // I made changes here.
char name[8];
char cont = 'y';
while(cont)
{
strcpy(path,&quot;E:\\abc&quot;); // I made changes here.
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);
fp=fopen(path,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;\nNo such file&quot;);
exit(1);
}
printf(&quot;\nFile opened successfully....&quot;);
cont = 'n';
}
return 1;
}

This was bcause it is not permissible in C the way we were trying to intialize a char array. To be initialized this way the variable should be the pointer to character rather than being an array.

Hope this helps,
Regards,
SwapSawe.
 
Hi, SwapSwae

Once again thanks alot.. these mistakes really making me something to learn and think alot...and u r the one who is saving all the time...

man it really working... I was trying for strcat instead of strcpy....

Once again thanks...

Have a Nice Time

Sridhar

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top