For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > Text file output









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Text file output
nah

2005-06-08, 8:59 pm

Hello.

I have a C++ function that ouputs a set of data text files. I pass the
directory path of where I want the files to be stored to the function through
a BSTR variable. I then need the function to take this path string and
append it to the front of the name of each text file. For example:

DirPath = "C:\MyDocs\NewFolder"
Filename1 = "Textfile1.txt"
Filename2 = "Textfile2.txt"

Add the above to give:
Filename1 = "C:\MyDocs\NewFolder\Textfile1.txt"
Filename2 = "C:\MyDocs\NewFolder\Textfile2.txt"

Then output data into each of these files using ofstream.

Here is the code I have so far, however, nothing gets saved in the
appropriate folder at all, and I don't know why. Where am I going wrong? It
must be a pointer problem or something. Any help would be greatly
appreciated. Thanks.

LPSTR DirPath;

void MyFunction(BSTR DirPathArg)
{
DirPath = (LPSTR)DirPathArg;
OutputFiles();
}

void OutputFiles()
{
char filename1=*DirPath;
char *buffer1=&filename1;
strcat(buffer1,"\\Textfile1.txt");

char filename2=*DirPath;
char *buffer2=&filename2;
strcat(buffer2,"\\Textfile2.txt");

ofstream outfile1(buffer1);
outfile1<< "Data1";
outfile1<< ... etc ...

ofstream outfile2(buffer2);
outfile2<< "Data2";
outfile2<< ... etc ...
}
William DePalo [MVP VC++]

2005-06-08, 8:59 pm

"nah" <nah@discussions.microsoft.com> wrote in message
news:A202B6BF-41D3-4629-9085-BEAFE2631DBE@microsoft.com...
> For example:
>
> DirPath = "C:\MyDocs\NewFolder"
> Filename1 = "Textfile1.txt"
> Filename2 = "Textfile2.txt"
>
> Add the above to give:
> Filename1 = "C:\MyDocs\NewFolder\Textfile1.txt"
> Filename2 = "C:\MyDocs\NewFolder\Textfile2.txt"


Check the docs for _splitpath() which decomposes a file specifier into its
constituent parts and _makepath() which recomposes the whole from the parts.

Regards,
Will


Scott McPhillips [MVP]

2005-06-08, 8:59 pm

nah wrote:

> Hello.
>
> I have a C++ function that ouputs a set of data text files. I pass the
> directory path of where I want the files to be stored to the function through
> a BSTR variable. I then need the function to take this path string and
> append it to the front of the name of each text file. For example:
>
> DirPath = "C:\MyDocs\NewFolder"
> Filename1 = "Textfile1.txt"
> Filename2 = "Textfile2.txt"
>
> Add the above to give:
> Filename1 = "C:\MyDocs\NewFolder\Textfile1.txt"
> Filename2 = "C:\MyDocs\NewFolder\Textfile2.txt"
>
> Then output data into each of these files using ofstream.
>
> Here is the code I have so far, however, nothing gets saved in the
> appropriate folder at all, and I don't know why. Where am I going wrong? It
> must be a pointer problem or something. Any help would be greatly
> appreciated. Thanks.
>
> LPSTR DirPath;
>
> void MyFunction(BSTR DirPathArg)
> {
> DirPath = (LPSTR)DirPathArg;
> OutputFiles();
> }
>
> void OutputFiles()
> {
> char filename1=*DirPath;
> char *buffer1=&filename1;
> strcat(buffer1,"\\Textfile1.txt");
>
> char filename2=*DirPath;
> char *buffer2=&filename2;
> strcat(buffer2,"\\Textfile2.txt");
>
> ofstream outfile1(buffer1);
> outfile1<< "Data1";
> outfile1<< ... etc ...
>
> ofstream outfile2(buffer2);
> outfile2<< "Data2";
> outfile2<< ... etc ...
> }


OK, you're right. It's several pointer problems, or rather memory
management problems.

0. I don't know if your casting of the BSTR is legitimate. Not my
subject area.

1. How many characters do you think will fit in the filename1 variable?
Answer: 1, because it is a char variable.

2. If you had allocated a roomy char array, so there is enough memory in
the buffer to do this work, the assignment filename1 = *DirPath does not
copy the string. When using the primitive C char array functions that
you are using you must copy your own strings.

char buf[_MAX_PATH];
strcpy(buf, DirPath);
strcat(buf, "\\Textfile1.txt");

C++ does have a string class that does provide the assignment operation,
and eliminates the need to be allocating buffers that may or may not be
big enough. So you are going into areas here that you don't need to
understand yet and you appear to be experimenting with things without
the basic orderly progression and understanding that would be provided
by a textbook or course.

--
Scott McPhillips [VC++ MVP]

nah

2005-06-09, 3:59 am

Yes, you are right. I rather foolishly forgot to use arrays. I am used to
VB where strings don't need to be considered as arrays of characters!

Thank you also for pointing me towards the strcpy function. My code works
now. Here's the modified version, for others' interest:

void OutputFiles()
{
char filename1[MAXCHARS];
strcpy(filename1,DirPath);
strcat(filename1,"\\Textfile1.txt");

char filename2[MAXCHARS];
strcpy(filename2,DirPath);
strcat(filename2,"\\Textfile2.txt");

ofstream outfile1(filename1);
outfile1<< "Data1";
outfile1<< ... etc ...

ofstream outfile2(filename2);
outfile2<< "Data2";
outfile2<< ... etc ...
}
Tim Roberts

2005-06-09, 3:59 am

nah <nah@discussions.microsoft.com> wrote:
>
>Thank you also for pointing me towards the strcpy function. My code works
>now. Here's the modified version, for others' interest:
>
> void OutputFiles()
> {
> char filename1[MAXCHARS];
> strcpy(filename1,DirPath);
> strcat(filename1,"\\Textfile1.txt");
>
> char filename2[MAXCHARS];
> strcpy(filename2,DirPath);
> strcat(filename2,"\\Textfile2.txt");
>
> ofstream outfile1(filename1);
> outfile1<< "Data1";
> outfile1<< ... etc ...
>
> ofstream outfile2(filename2);
> outfile2<< "Data2";
> outfile2<< ... etc ...
> }


Since you are already using C++, why wouldn't you do this the easy way:

#include <string>
...
string filename1 = string(DirPath) + "\\Textfile1.txt";
string filename2 = string(DirPath) + "\\Textfile2.txt";
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc
Alexander Grigoriev

2005-06-09, 3:59 am

You need a real buffer for strcat, not a character constant like DirPath.

> char filename1=*DirPath;
> char *buffer1=&filename1;
> strcat(buffer1,"\\Textfile1.txt");


You cannot add a string to a one char buffer filename1.

I'd suggest to use std::string or CString instead of legacy C strings.

"nah" <nah@discussions.microsoft.com> wrote in message
news:A202B6BF-41D3-4629-9085-BEAFE2631DBE@microsoft.com...
> Hello.
>
> I have a C++ function that ouputs a set of data text files. I pass the
> directory path of where I want the files to be stored to the function
> through
> a BSTR variable. I then need the function to take this path string and
> append it to the front of the name of each text file. For example:
>
> DirPath = "C:\MyDocs\NewFolder"
> Filename1 = "Textfile1.txt"
> Filename2 = "Textfile2.txt"
>
> Add the above to give:
> Filename1 = "C:\MyDocs\NewFolder\Textfile1.txt"
> Filename2 = "C:\MyDocs\NewFolder\Textfile2.txt"
>
> Then output data into each of these files using ofstream.
>
> Here is the code I have so far, however, nothing gets saved in the
> appropriate folder at all, and I don't know why. Where am I going wrong?
> It
> must be a pointer problem or something. Any help would be greatly
> appreciated. Thanks.
>
> LPSTR DirPath;
>
> void MyFunction(BSTR DirPathArg)
> {
> DirPath = (LPSTR)DirPathArg;
> OutputFiles();
> }
>
> void OutputFiles()
> {
> char filename1=*DirPath;
> char *buffer1=&filename1;
> strcat(buffer1,"\\Textfile1.txt");
>
> char filename2=*DirPath;
> char *buffer2=&filename2;
> strcat(buffer2,"\\Textfile2.txt");
>
> ofstream outfile1(buffer1);
> outfile1<< "Data1";
> outfile1<< ... etc ...
>
> ofstream outfile2(buffer2);
> outfile2<< "Data2";
> outfile2<< ... etc ...
> }



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com