Re: [LIP] C file read

"Vikas Saurabh" <[email protected]>
Newsgroups gmane.user-groups.linux.india.programmers
Message-ID <[email protected]>
Just throwing a hunch here...but may be the read is failing and hence
len might be getting -1. But, the condition still succeeds
(len<sizeof(Buf)). Thereby, your code assumes that you have read the
data correctly; although it still contains the old data.

--regards,
vikas

PS: I assume you are not using len for the processing. My assumptions
is based on the fact that you are reading an XML file, thereby any
functions that would be reading the buffer as an XML might continue to
do so by matching the start and end tags, etc. Ignore the post if the
assumption is wrong. Although it would be nice to put a check on len
anyways.

On 5/17/07, alok bharti <alok_mact-/[email protected]> wrote:
> Hi,
>
> I have function which is callled to read from a file.
> it opens the file, once the data is read closes the
> file and returns.
>
> after some time the same function is called.
> and in between the file content is manually changed.
>
> still the function read the old data. may be somewhere
> from some old buffer. I expect my function to read the
> new data in the file.
>
> here is the code snippet.
>
> ...
> ...
> read_file()
> {
>     FILE           *in;
>
>     char           *confpath;
>     char           *filename = "test2.xml";
>     char            filepath[256];
>     int rc=0;
>
>     /*
>      *
>      * * check for ENV variable XMLFILEPATH
>      */
>     confpath = getenv("XMLFILEPATH");
>     if (NULL == confpath) {
>         fprintf(stderr, "%s:XMLFILEPATH not set",
> __FUNCTION__);
>         return (-1);
>     }
>     sprintf(filepath, "%s/%s", confpath, filename);
>     if (!(in = fopen(filepath, "r"))) {
>         printf("Unable to open input file %s\n",
> filepath);
>         return (-1);
>     }
>
>     printf("%s: file opened \n", __FUNCTION__);
>
>     for (;;) {
>         int             done = 0;
>         int             len = 0;
>         len = fread(Buff, 1, sizeof(Buff), in);
>         done = len < sizeof(Buff);
>
>         //....
>         //some code to deal with the data just read
>         //....
>
>         if (done)
>             break;
>     }
>
>     printf("%s: file closed \n", __FUNCTION__);
>     rc = fclose(in);
>     if(0!=rc)
>         printf("%s \n", strerror(errno));
>
>     return 0;
> }
> ...
> ...
>
> Please help and suggest where is the mistake.
>
> For the first call this function works fine.
> when recalled after sometime when the file content are
> changed, it still reads the old data. strange that the
> data in the file is now changed.
>
>
> Regards,
> Alok
>
>
>
>
> ____________________________________________________________________________________Pinpoint customers who are looking for what you sell.
> http://searchmarketing.yahoo.com/
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> linux-india-programmers mailing list
> linux-india-programmers-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/linux-india-programmers
>



-- 
____________________________________________________________________
Vikas Saurabh
____________________________________________________________________
Morpheus:  If real is what you can feel, smell, taste and see,
                 then 'real' is simply electrical signals interpreted
by your brain
____________________________________________________________________

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.