[VxW] File I/O question - Conclusion
Piyush Gupta
piyush.gupta at exar.com
Thu Apr 17 15:00:28 PDT 2003
For sake of completeness... I got two suggestions that worked. They are as
below:
1) From Travis:
=========
void fio(char *name) /* Enter file name here. */
{
int fd;
char fullpath[256];
char filechar;
sprintf(fullpath,"/tgtsvr/%s",name);
if((fd=open(fullpath, O_RDONLY,0))==ERROR)
=========
2) From Fred:
=========
Buffer overflow. Remember that strcat() does not allocate memory.
char *fullpath = (char *) malloc(strlen(name) + strlen("/tgtsvr/") + 1);
strcpy(fullpath, "/tgtsvr/");
strcat(fullpath, name);
....
free(fullpath);
========
Thanks.
Piyush Gupta wrote:
> Goal: To open, read (only) and close a text file in the target server
> file system from the shell, any number of times.
>
> Code: Written below, based on sample code provided (by John?) on this
> forum.
> =============
>
> /* This example code demonstrates how to open a file on the Host PC
> in the Read Only mode. This uses the "/tgtsvr" file system of VxWorks.
>
> Requirements:
>
> 1) In the Tornado environment, set the target server directory to
> c:\test
>
> Go to Tools -> Target Server -> Configure.
>
> 2) Place the file to read in this directory in the host PC.
>
> 3) Compile, build and download this example code. Module is named:
> fio().
>
> 4) In the shell: ->fio("/tgtsvr/myjunk.txt") will open and read
> myjunk.txt
> which it expects in the c:\test directory and print
> it out in the shell window.
>
> 5) File I/O is done via read() and write(). FILE* are not to be used.
> Instead, int file descriptors are used. fscanf and fprintf are not used
> here.
>
> 6) Always close the file after use.
>
> */
>
> #include "vxWorks.h"
> #include "ioLib.h"
> #include "errno.h"
> #include "stdio.h"
> #include "string.h"
>
> void fio(char *name) /* Enter file name here. */
> {
> int fd;
> char filechar;
>
> /*name = strcat("/tgtsvr/",name); /*This does not work
> repeatedly???*/
>
> if((fd=open(name, O_RDONLY,0))==ERROR)
> {
> printf("Error opening file %#x\n",errno);
> close(fd);
> return;
> }
>
> printf("File descriptor: %d\n",fd);
>
> while(read(fd,&filechar,1)!=0)
> printf("%c",filechar);
> printf("\n");
>
> close(fd);
> return;
> }
> ==================
>
> Problem: I use this module in the shell as: ->fio("/tgtsvr/myjunk.txt")
> and it works fine. I can keep repeating the shell command and it works
> again and again. However, when I tried using strcat to add the /tgtsvr/
> part in the code so I can just say ->fio("myjunk.txt") in the shell -
> then I can execute the module only once. I have to unload and reload fio
> module if I want to use it the second time in the shell. It seems to
> mess up the file name string in the second invocation in the shell.
>
> I must be doing something silly. The code, shown above works good, with
> the strcat part commented out. If I uncomment the strcat part, it starts
> having this problem.
>
> Any ideas?
>
> -Piyush Gupta
> _______________________________________________
> VxWorks Users Group mailing list
> VxWexplo at lbl.gov
> http://www-csg.lbl.gov/vxworks/
--
-Piyush Gupta
More information about the VxWexplo
mailing list