08-23-2012 01:45 PM
Anyone Everyone, I could sure use some help. My CVI code won't do a simple. I have used the "FileSelectPopup" function to obtain the "pathName" that it out puts and am using that in the "OpenFile"
function to open the file. This is all being take care of in a callback function after a button is pushed. Can someone explain to me why this simple act is not resulting in the display of the selected file? I
could really use the help and it would be greatly appreciated. Code snipet is provided below. The "puts()" I/O function is used just to prove to myself that I have what I need to open this file. Any help on
this simple, yet frustrating endevour, would be great.
int CVICALLBACK OpenFilesUp (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char UserSelect[MAX_PATHNAME_LEN];//This variable is the buffer for the path of
//the file the user has selected.
//It holds the max value allowed for a path.
int FileHandle = -1;
switch (event)
{
case EVENT_COMMIT:
//The function below opens a file select box for the user to chose from a from a specific directory, type of default file, other leagal files,
//title of the dialog box, etc.
FileSelectPopup("c:\\Users\\vhouston\\Desktop\\2011 Projects", "*.docx", "*.docx, *.doc, *.txt, *.dat", "File Selection", VAL_SELECT_BUTTON, 0, 0, 1, 1, UserSelect);
//The "OpenFile" function uses the buffer information gathered from the "FileSelectPopup" to open the
//user selected file. Currently this is set for read only, and opens with the pointer at the beginning of the file.
FileHandle = OpenFile(UserSelect, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
puts(UserSelect);
break;
case EVENT_RIGHT_CLICK:
break;
}
return 0;
}
08-23-2012 03:00 PM - edited 08-23-2012 03:00 PM
Your code has only gotten you a handle to an open file.
Now use that handle with ReadFile() or ReadLine() to read the contents of the file. I assume that is what you mean by "display of the open file"
Something like this
size_t buffersize = 2048; // may have to use int buffersize
char buffer[buffersize];
int bytes_read;
bytes_read = ReadFile (filehandle, buffer, buffersize -1);
//Insert write function here
Or something like this
char linebuffer[260];
int bytes_read = 0;
while (bytes_read >=0)
{
bytes_read = ReadLine (filehandle, linebuffer, -1);
//Insert some write function here
}
Also don't forget to CloseFile() when you are done.
08-24-2012 01:56 AM
Suggestion from blakney is correct: you must use the file handle for every access to file data after opening it, the file name will not be used any more.
Also, I noted that you allow the user to select several file types: keep in mind that file I/O functions cannot be used to retrieve a WORD document content as that program does: they will retrieve the raw file content including formatting codes and accessory data that words translates into internal commands. To operate on WORD document you could use the ActiveX interface to office instrument that comes with CVI.
08-24-2012 10:23 AM
Outstanding advice , Roberto. I truely appreciate it. This is my first stab at programming with CVI.
As it concerns ActiveX interface to office instrument that comes with CVI, could you provide as much info as you can on where I should go to learn how to use it? I am a beginner with that also.
Thanks, bro.
08-24-2012 10:24 AM
Outstanding advice , Blakey. I truely appreciate it. I will run with your advice and that of Roberto.
08-27-2012 03:13 AM
Hello, sorry for this late response: I was out of office these days.
CVI has the ability to interface with any ActiveX-capable application and comes with some examples to interface with MS Office applications: you should be able to find this example on your PC too. Additionally, there is the word report instrument, which even if aimed to generating reports through Word application should permit to read an existing word document (I'm not sure as I'm not using it actually).
Both instruments are interface to an existing application, i.e. Word must actually be installed on the target systm for these functions to operate. Be warned that Office Starter application is not suitable for this purpose as it lacks the ActiveX facility.