Thread: [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: jose i. c. <ji...@fr...> - 2003-04-09 01:51:47
|
Greetings O SmartOnes!
I have the following c code:
char FileContent(char *file)
{
char fcontent(3000);
...
/*in here I grab the content of a text file */
...
return(fcontent);
}
int main(int argc, char *argv[])
{
char text[3000];
...
...
...
text = FileContent("temp.txt");
}
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.
thanks.
josé
|
|
From: Daniel K. O. <dan...@ya...> - 2003-04-09 02:21:55
|
A good start is to learn the C sintax. Try this tutorial: http://www.geocities.com/tom_torfs/cintro.html (the best C-quick-tutorial I've ever seen) Daniel K. O. Only ask questions with yes/no answers if you want "yes" or "no" as the answer. http://homepages.tesco.net/~J.deBoynePollard/FGA/questions-with-yes-or-no-an swers.html > -----Mensagem original----- > De: dev...@li... > [mailto:dev...@li...]Em nome de jose isaias > cabrera > Enviada em: terça-feira, 8 de abril de 2003 22:52 > Para: dev...@li... > Assunto: [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) > { > char fcontent(3000); > ... > /*in here I grab the content of a text file */ > ... > return(fcontent); > } > > int main(int argc, char *argv[]) > { > char text[3000]; > ... > ... > ... > text = FileContent("temp.txt"); > } > > 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. > > thanks. > > josé > |
|
From: Ioannis V. <iv...@at...> - 2003-04-09 04:00:54
|
> -----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)
> {
> 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];
> ...
> ...
> ...
> 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
|
|
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
>
|
|
From: Ioannis V. <iv...@at...> - 2003-04-09 11:04:17
|
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: Wednesday, April 09, 2003 10:02 AM
> To: Ioannis Vranos
> Cc: Dev-C++ Mailing List
> Subject: RE: [Dev-C++] passing the content of a string to a
> variable from
>
>
> 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)
Just for accuracy, Ioannis Vranos had not written the above.
>
> > > {
> > > 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];
The same apply for your comments here.
:)
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
|
|
From: jose i. c. <ji...@fr...> - 2003-04-10 01:21:00
|
Per, > // Need to return a pointer to text, not just a single character. > char* FileContent(char *file) this worked! Thanks! I had the code correct, but I was returning a string instead of a string pointer. thanks, jose |
|
From: Ioannis V. <iv...@at...> - 2003-04-10 08:16:46
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of jose isaias cabrera > Sent: Thursday, April 10, 2003 4:21 AM > To: Per Westermark; Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: Re: [Dev-C++] passing the content of a string to a > variable from > > > > Per, > > > // Need to return a pointer to text, not just a single character. > > char* FileContent(char *file) > > this worked! Thanks! I had the code correct, but I was > returning a string > instead of a string pointer. For accuracy you were returning a char. 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 |
|
From: jose i. c. <ji...@fr...> - 2003-04-11 02:26:04
|
Yes I was... Thanks Ioannis. thanks. > > Per, > > > > > // Need to return a pointer to text, not just a single character. > > > char* FileContent(char *file) > > > > this worked! Thanks! I had the code correct, but I was > > returning a string > > instead of a string pointer. > > > > For accuracy you were returning a char. |
|
From: David M. <dem...@uw...> - 2003-04-09 14:40:57
|
Looks like what you actually want is something like this:
char *FileContent(file *fp)
{
char *buf;
// Open the file...
int i =3D fseek(fp, SEEK_END) + 1;
buf =3D malloc(sizeof(i));
fseek(fp, SEEK_SET);
for(char ch =3D fgetc(fp); ch !=3D EOF; buf++ =3D ch);
return buf;
}
I think this should work at least. The code is off the top of my head and
untested.
At 09:51 PM 08/04/2003 -0400, jose isaias cabrera wrote:
>
>Greetings O SmartOnes!
>
>I have the following c code:
>
>char FileContent(char *file)
>{
> char fcontent(3000);
> ...
> /*in here I grab the content of a text file */
> ...
> return(fcontent);
>}
>
>int main(int argc, char *argv[])
>{
> char text[3000];
> ...
> ...
> ...
> text =3D FileContent("temp.txt");
>}
>
>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.
>
>thanks.
>
>jos=E9
>
>
>
>
>-------------------------------------------------------
>This SF.net email is sponsored by: ValueWeb:=20
>Dedicated Hosting for just $79/mo with 500 GB of bandwidth!=20
>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
>
|
|
From: Per W. <pw...@ia...> - 2003-04-09 15:06:52
|
See below:
/Per W
On Wed, 9 Apr 2003, David Matthews wrote:
> Looks like what you actually want is something like this:
>
> char *FileContent(file *fp)
> {
> char *buf;
> // Open the file...
> int i =3D fseek(fp, SEEK_END) + 1;
// Note - will return incorrect size for IS with end-of-line translation.
// cr+lf -> lf. However, not too big a problem unless large file with lot
// of short lines.
i =3D fseek(fp,0,SEEK_END)+1;
> buf =3D malloc(sizeof(i));
> fseek(fp, SEEK_SET);
fseek(fp,0,SEEK_SET);
> for(char ch =3D fgetc(fp); ch !=3D EOF; buf++ =3D ch);
// No use to do fgetc() in pre-loop part, since then only one
// character will be read, followed by a wild loop.
// Also, need *buf++ for post-assign.
for (char ch; (ch =3D fgetc(fp)) !=3D EOF; *buf++ =3D ch);
// Have to zero-terminate since calloc() wasn't used.
*buf =3D '\0'';
// One alternative that is a bit quicker:
size_t count =3D fread(buf,1,i-1,fp);
if (count =3D=3D i)
buf[count] =3D '\0';
else
buf[0] =3D '\0';
> return buf;
> }
>
> I think this should work at least. The code is off the top of my head an=
d
> untested.
>
> At 09:51 PM 08/04/2003 -0400, jose isaias cabrera wrote:
> >
> >Greetings O SmartOnes!
> >
> >I have the following c code:
> >
> >char FileContent(char *file)
> >{
> > char fcontent(3000);
> > ...
> > /*in here I grab the content of a text file */
> > ...
> > return(fcontent);
> >}
> >
> >int main(int argc, char *argv[])
> >{
> > char text[3000];
> > ...
> > ...
> > ...
> > text =3D FileContent("temp.txt");
> >}
> >
> >I can't get this to work. I've a bunch of places and I even tried to ge=
t
> >the pointers to no avail. I am new to c, but I am an expert programmer.
> >Any c help will be greatly appreciated.
> >
> >thanks.
> >
> >jos=E9
> >
> >
> >
> >
> >-------------------------------------------------------
> >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
> >
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Etnus, makers of TotalView, The debugg=
er
> for complex code. Debugging C/C++ programs can leave you feeling lost and
> disoriented. TotalView can help you find your way. Available on major UNI=
X
> and Linux platforms. Try it free. www.etnus.com
> _______________________________________________
> 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
>
|
|
From: David M. <dem...@uw...> - 2003-04-09 17:00:20
|
Gahh, stupid stupid.
At 09:27 AM 09/04/2003 -0400, David Matthews wrote:
>Looks like what you actually want is something like this:
>
>char *FileContent(file *fp)
>{
> char *buf;
> // Open the file...
> int i =3D fseek(fp, SEEK_END) + 1;
> buf =3D malloc(sizeof(i));
Should be malloc(i); instead :\
> fseek(fp, SEEK_SET);
> for(char ch =3D fgetc(fp); ch !=3D EOF; buf++ =3D ch);
> return buf;
>}
>
>I think this should work at least. The code is off the top of my head and
>untested.
>
>At 09:51 PM 08/04/2003 -0400, jose isaias cabrera wrote:
>>
>>Greetings O SmartOnes!
>>
>>I have the following c code:
>>
>>char FileContent(char *file)
>>{
>> char fcontent(3000);
>> ...
>> /*in here I grab the content of a text file */
>> ...
>> return(fcontent);
>>}
>>
>>int main(int argc, char *argv[])
>>{
>> char text[3000];
>> ...
>> ...
>> ...
>> text =3D FileContent("temp.txt");
>>}
>>
>>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.
>>
>>thanks.
>>
>>jos=E9
>>
>>
>>
>>
>>-------------------------------------------------------
>>This SF.net email is sponsored by: ValueWeb:=20
>>Dedicated Hosting for just $79/mo with 500 GB of bandwidth!=20
>>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
>>
>
>
>-------------------------------------------------------
>This SF.net email is sponsored by: Etnus, makers of TotalView, The debugger=
=20
>for complex code. Debugging C/C++ programs can leave you feeling lost and=
=20
>disoriented. TotalView can help you find your way. Available on major UNIX=
=20
>and Linux platforms. Try it free. www.etnus.com
>_______________________________________________
>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
>
|
|
From: jose i. c. <ji...@fr...> - 2003-04-10 01:29:57
|
Yep. I had the other stuff, but was missing the char *FileContent....
thanks,
jose
----- Original Message -----
From: "David Matthews" <dem...@uw...>
To: "jose isaias cabrera" <ji...@fr...>;
<dev...@li...>
Sent: Wednesday, April 09, 2003 9:27 AM
Subject: Re: [Dev-C++] passing the content of a string to a variable from
Looks like what you actually want is something like this:
char *FileContent(file *fp)
{
char *buf;
// Open the file...
int i = fseek(fp, SEEK_END) + 1;
buf = malloc(sizeof(i));
fseek(fp, SEEK_SET);
for(char ch = fgetc(fp); ch != EOF; buf++ = ch);
return buf;
}
I think this should work at least. The code is off the top of my head and
untested.
At 09:51 PM 08/04/2003 -0400, jose isaias cabrera wrote:
>
>Greetings O SmartOnes!
>
>I have the following c code:
>
>char FileContent(char *file)
>{
> char fcontent(3000);
> ...
> /*in here I grab the content of a text file */
> ...
> return(fcontent);
>}
>
>int main(int argc, char *argv[])
>{
> char text[3000];
> ...
> ...
> ...
> text = FileContent("temp.txt");
>}
>
>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.
>
>thanks.
>
>josé
>
>
>
>
>-------------------------------------------------------
>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
>
|