RE: [Dev-C++] passing the content of a string to a variable from
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-04-09 06:02:55
|
See below.
/Per W
On Wed, 9 Apr 2003, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of jose isaias cabrera
> > Sent: Wednesday, April 09, 2003 4:52 AM
> > To: dev...@li...
> > Subject: [Dev-C++] passing the content of a string to a variable from
> >
> >
> >
> > Greetings O SmartOnes!
> >
> > I have the following c code:
> >
> > char FileContent(char *file)
// Need to return a pointer to text, not just a single character.
char* FileContent(char *file)
> > {
> > char fcontent(3000);
// Array size should be in []
// You may never return address of a variable declared on stack.
// As soon as the FileContent() returns, the fcontent buffer would
// be deleted unless it's declared static, which will store
// fcontent as a global variable but only available within name
// space of FileContent().
static char fcontent[3000];
> > ...
> > /*in here I grab the content of a text file */
> > ...
> > return(fcontent);
> > }
>
>
> I assume C:
>
>
>
> >
> > int main(int argc, char *argv[])
> > {
> > char text[3000];
// If FileContent already keeps the full text content, no need for one
// more buffer. Just keep track of a pointer to the text.
// Besides, it isn't possible to assign an array to an array. As Ioannis
// shows below, if you really wants to copy the contents of an array to
// another array, you have to call strcpy(), strncpy(), memcpy(),
// memmove() or similar.
// char *text;
> > ...
> > ...
> > ...
> > text = FileContent("temp.txt");
>
>
>
> FileContent returns a char. You assign it to an array which is not proper.
> If you intend it to return a char, you have to do something like:
>
> text[0]=FileContent("temp.txt");
>
>
> If you made a mistake and intend to return a char * you have either to do:
>
>
> char *text;
>
> /* ... */
>
> text=FileContent("temp.txt");
>
>
> or
>
>
> char text[3000];
> char *p;
>
> /* ... */
>
> p=FileContent("temp.txt");
>
> strncpy(text, p, 3000);
>
> text[2999]='\0';
>
>
>
>
> > }
> >
> > I can't get this to work. I've a bunch of places and I even
> > tried to get
> > the pointers to no avail. I am new to c, but I am an expert
> > programmer.
> > Any c help will be greatly appreciated.
>
>
>
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL 1: http://run.to/noicys
> * Alternative URL 2: http://www.noicys.cjb.net
>
>
> Anti-racism movement: http://www.positive-youthfoundation.org
> Iraq Peace Pledge: http://www.peacepledge.org
>
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ValueWeb:
> Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
> No other company gives more support or power for your dedicated server
> http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|