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 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
>
|